PageRenderTime 34ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/fabpot/php-cs-fixer/Symfony/CS/Utils.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 117 lines | 45 code | 13 blank | 59 comment | 3 complexity | 42525480fbbe1c154efdbc5528d29344 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the PHP CS utility.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\CS;
  11. use Symfony\CS\Tokenizer\Token;
  12. /**
  13. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  14. * @author Graham Campbell <graham@mineuk.com>
  15. * @author Odín del Río <odin.drp@gmail.com>
  16. */
  17. class Utils
  18. {
  19. /**
  20. * Calculate a bitmask for given constant names.
  21. *
  22. * @param string[] $options constant names
  23. *
  24. * @return int
  25. */
  26. public static function calculateBitmask(array $options)
  27. {
  28. $bitmask = 0;
  29. foreach ($options as $optionName) {
  30. if (defined($optionName)) {
  31. $bitmask |= constant($optionName);
  32. }
  33. }
  34. return $bitmask;
  35. }
  36. /**
  37. * Converts a camel cased string to an snake cased string.
  38. *
  39. * @param string $string
  40. *
  41. * @return string
  42. */
  43. public static function camelCaseToUnderscore($string)
  44. {
  45. return preg_replace_callback(
  46. '/(^|[a-z0-9])([A-Z])/',
  47. function (array $matches) {
  48. return strtolower(strlen($matches[1]) ? $matches[1].'_'.$matches[2] : $matches[2]);
  49. },
  50. $string
  51. );
  52. }
  53. /**
  54. * Compare two integers for equality.
  55. *
  56. * We'll return 0 if they're equal, 1 if the first is bigger than the
  57. * second, and -1 if the second is bigger than the first.
  58. *
  59. * @param int $a
  60. * @param int $b
  61. *
  62. * @return int
  63. */
  64. public static function cmpInt($a, $b)
  65. {
  66. if ($a === $b) {
  67. return 0;
  68. }
  69. return $a < $b ? -1 : 1;
  70. }
  71. /**
  72. * Split a multi-line string up into an array of strings.
  73. *
  74. * We're retaining a newline character at the end of non-blank lines, and
  75. * discarding other lines, so this function is unsuitable for anyone for
  76. * wishing to retain the exact number of line endings. If a single-line
  77. * string is passed, we'll just return an array with a element.
  78. *
  79. * @param string $content
  80. *
  81. * @return string[]
  82. */
  83. public static function splitLines($content)
  84. {
  85. preg_match_all("/[^\n\r]+[\r\n]*/", $content, $matches);
  86. return $matches[0];
  87. }
  88. /**
  89. * Calculate the trailing whitespace indentation.
  90. *
  91. * What we're doing here is grabbing everything after the final newline.
  92. *
  93. * @param Token $token
  94. *
  95. * @return string
  96. */
  97. public static function calculateTrailingWhitespaceIndent(Token $token)
  98. {
  99. if (!$token->isWhitespace()) {
  100. throw new \InvalidArgumentException('The given token must be whitespace.');
  101. }
  102. return ltrim(strrchr(str_replace(array("\r\n", "\r"), "\n", $token->getContent()), 10), "\n");
  103. }
  104. }