PageRenderTime 35ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Symfony/CS/Utils.php

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