EregToPregFixer.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\Fixer\Alias;
  12. use PhpCsFixer\AbstractFixer;
  13. use PhpCsFixer\Tokenizer\Tokens;
  14. use PhpCsFixer\Utils;
  15. /**
  16. * @author Matteo Beccati <matteo@beccati.com>
  17. */
  18. final class EregToPregFixer extends AbstractFixer
  19. {
  20. /**
  21. * @var array the list of the ext/ereg function names, their preg equivalent and the preg modifier(s), if any
  22. * all condensed in an array of arrays
  23. */
  24. private static $functions = array(
  25. array('ereg', 'preg_match', ''),
  26. array('eregi', 'preg_match', 'i'),
  27. array('ereg_replace', 'preg_replace', ''),
  28. array('eregi_replace', 'preg_replace', 'i'),
  29. array('split', 'preg_split', ''),
  30. array('spliti', 'preg_split', 'i'),
  31. );
  32. /**
  33. * @var array the list of preg delimiters, in order of preference
  34. */
  35. private static $delimiters = array('/', '#', '!');
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function isCandidate(Tokens $tokens)
  40. {
  41. return $tokens->isTokenKindFound(T_STRING);
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function isRisky()
  47. {
  48. return true;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function fix(\SplFileInfo $file, Tokens $tokens)
  54. {
  55. $end = $tokens->count() - 1;
  56. foreach (self::$functions as $map) {
  57. // the sequence is the function name, followed by "(" and a quoted string
  58. $seq = array(array(T_STRING, $map[0]), '(', array(T_CONSTANT_ENCAPSED_STRING));
  59. $currIndex = 0;
  60. while (null !== $currIndex) {
  61. $match = $tokens->findSequence($seq, $currIndex, $end, false);
  62. // did we find a match?
  63. if (null === $match) {
  64. break;
  65. }
  66. // findSequence also returns the tokens, but we're only interested in the indexes, i.e.:
  67. // 0 => function name,
  68. // 1 => bracket "("
  69. // 2 => quoted string passed as 1st parameter
  70. $match = array_keys($match);
  71. // advance tokenizer cursor
  72. $currIndex = $match[2];
  73. // ensure it's a function call (not a method / static call)
  74. $prev = $tokens->getPrevMeaningfulToken($match[0]);
  75. if (null === $prev || $tokens[$prev]->isGivenKind(array(T_OBJECT_OPERATOR, T_DOUBLE_COLON))) {
  76. continue;
  77. }
  78. // ensure the first parameter is just a string (e.g. has nothing appended)
  79. $next = $tokens->getNextMeaningfulToken($match[2]);
  80. if (null === $next || !$tokens[$next]->equalsAny(array(',', ')'))) {
  81. continue;
  82. }
  83. // convert to PCRE
  84. $string = substr($tokens[$match[2]]->getContent(), 1, -1);
  85. $quote = substr($tokens[$match[2]]->getContent(), 0, 1);
  86. $delim = $this->getBestDelimiter($string);
  87. $preg = $delim.addcslashes($string, $delim).$delim.'D'.$map[2];
  88. // check if the preg is valid
  89. if (!$this->checkPreg($preg)) {
  90. continue;
  91. }
  92. // modify function and argument
  93. $tokens[$match[2]]->setContent($quote.$preg.$quote);
  94. $tokens[$match[0]]->setContent($map[1]);
  95. }
  96. }
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. protected function getDescription()
  102. {
  103. return 'Replace deprecated ereg regular expression functions with preg.';
  104. }
  105. /**
  106. * Check the validity of a PCRE.
  107. *
  108. * @param string $pattern the regular expression
  109. *
  110. * @return bool
  111. */
  112. private function checkPreg($pattern)
  113. {
  114. return false !== @preg_match($pattern, '');
  115. }
  116. /**
  117. * Get the delimiter that would require the least escaping in a regular expression.
  118. *
  119. * @param string $pattern the regular expression
  120. *
  121. * @return string the preg delimiter
  122. */
  123. private function getBestDelimiter($pattern)
  124. {
  125. // try do find something that's not used
  126. $delimiters = array();
  127. foreach (self::$delimiters as $k => $d) {
  128. if (false === strpos($pattern, $d)) {
  129. return $d;
  130. }
  131. $delimiters[$d] = array(substr_count($pattern, $d), $k);
  132. }
  133. // return the least used delimiter, using the position in the list as a tie breaker
  134. uasort($delimiters, function ($a, $b) {
  135. if ($a[0] === $b[0]) {
  136. return Utils::cmpInt($a, $b);
  137. }
  138. return $a[0] < $b[0] ? -1 : 1;
  139. });
  140. return key($delimiters);
  141. }
  142. }