Utils.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 PhpCsFixer;
  12. use PhpCsFixer\Fixer\FixerInterface;
  13. use PhpCsFixer\Tokenizer\Token;
  14. /**
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. * @author Graham Campbell <graham@alt-three.com>
  17. * @author Odín del Río <odin.drp@gmail.com>
  18. *
  19. * @internal
  20. */
  21. final class Utils
  22. {
  23. /**
  24. * Calculate a bitmask for given constant names.
  25. *
  26. * @param string[] $options constant names
  27. *
  28. * @return int
  29. */
  30. public static function calculateBitmask(array $options)
  31. {
  32. $bitmask = 0;
  33. foreach ($options as $optionName) {
  34. if (\defined($optionName)) {
  35. $bitmask |= \constant($optionName);
  36. }
  37. }
  38. return $bitmask;
  39. }
  40. /**
  41. * Converts a camel cased string to a snake cased string.
  42. *
  43. * @param string $string
  44. *
  45. * @return string
  46. */
  47. public static function camelCaseToUnderscore($string)
  48. {
  49. return strtolower(Preg::replace('/(?<!^)((?=[A-Z][^A-Z])|(?<![A-Z])(?=[A-Z]))/', '_', $string));
  50. }
  51. /**
  52. * Compare two integers for equality.
  53. *
  54. * We'll return 0 if they're equal, 1 if the first is bigger than the
  55. * second, and -1 if the second is bigger than the first.
  56. *
  57. * @param int $a
  58. * @param int $b
  59. *
  60. * @return int
  61. */
  62. public static function cmpInt($a, $b)
  63. {
  64. if ($a === $b) {
  65. return 0;
  66. }
  67. return $a < $b ? -1 : 1;
  68. }
  69. /**
  70. * Calculate the trailing whitespace.
  71. *
  72. * What we're doing here is grabbing everything after the final newline.
  73. *
  74. * @return string
  75. */
  76. public static function calculateTrailingWhitespaceIndent(Token $token)
  77. {
  78. if (!$token->isWhitespace()) {
  79. throw new \InvalidArgumentException(sprintf('The given token must be whitespace, got "%s".', $token->getName()));
  80. }
  81. $str = strrchr(
  82. str_replace(["\r\n", "\r"], "\n", $token->getContent()),
  83. "\n"
  84. );
  85. if (false === $str) {
  86. return '';
  87. }
  88. return ltrim($str, "\n");
  89. }
  90. /**
  91. * Perform stable sorting using provided comparison function.
  92. *
  93. * Stability is ensured by using Schwartzian transform.
  94. *
  95. * @param mixed[] $elements
  96. * @param callable $getComparedValue a callable that takes a single element and returns the value to compare
  97. * @param callable $compareValues a callable that compares two values
  98. *
  99. * @return mixed[]
  100. */
  101. public static function stableSort(array $elements, callable $getComparedValue, callable $compareValues)
  102. {
  103. array_walk($elements, static function (&$element, $index) use ($getComparedValue) {
  104. $element = [$element, $index, $getComparedValue($element)];
  105. });
  106. usort($elements, static function ($a, $b) use ($compareValues) {
  107. $comparison = $compareValues($a[2], $b[2]);
  108. if (0 !== $comparison) {
  109. return $comparison;
  110. }
  111. return self::cmpInt($a[1], $b[1]);
  112. });
  113. return array_map(static function (array $item) {
  114. return $item[0];
  115. }, $elements);
  116. }
  117. /**
  118. * Sort fixers by their priorities.
  119. *
  120. * @param FixerInterface[] $fixers
  121. *
  122. * @return FixerInterface[]
  123. */
  124. public static function sortFixers(array $fixers)
  125. {
  126. // Schwartzian transform is used to improve the efficiency and avoid
  127. // `usort(): Array was modified by the user comparison function` warning for mocked objects.
  128. return self::stableSort(
  129. $fixers,
  130. static function (FixerInterface $fixer) {
  131. return $fixer->getPriority();
  132. },
  133. static function ($a, $b) {
  134. return self::cmpInt($b, $a);
  135. }
  136. );
  137. }
  138. /**
  139. * Join names in natural language wrapped in backticks, e.g. `a`, `b` and `c`.
  140. *
  141. * @param string[] $names
  142. *
  143. * @throws \InvalidArgumentException
  144. *
  145. * @return string
  146. */
  147. public static function naturalLanguageJoinWithBackticks(array $names)
  148. {
  149. if (empty($names)) {
  150. throw new \InvalidArgumentException('Array of names cannot be empty');
  151. }
  152. $names = array_map(static function ($name) {
  153. return sprintf('`%s`', $name);
  154. }, $names);
  155. $last = array_pop($names);
  156. if ($names) {
  157. return implode(', ', $names).' and '.$last;
  158. }
  159. return $last;
  160. }
  161. }