EregToPregFixer.php 4.6 KB

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