PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Symfony/CS/Fixer/Contrib/EregToPregFixer.php

http://github.com/fabpot/PHP-CS-Fixer
PHP | 171 lines | 84 code | 26 blank | 61 comment | 11 complexity | 0575bec15ad19fe0638a5acd5494c4ec 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\Fixer\Contrib;
  12. use Symfony\CS\AbstractFixer;
  13. use Symfony\CS\Tokenizer\Tokens;
  14. use Symfony\CS\Utils;
  15. /**
  16. * @author Matteo Beccati <matteo@beccati.com>
  17. */
  18. 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 fix(\SplFileInfo $file, $content)
  40. {
  41. if (!$this->cursoryMatch($content)) {
  42. return $content;
  43. }
  44. $tokens = Tokens::fromCode($content);
  45. $end = $tokens->count() - 1;
  46. foreach (self::$functions as $map) {
  47. // the sequence is the function name, followed by "(" and a quoted string
  48. $seq = array(array(T_STRING, $map[0]), '(', array(T_CONSTANT_ENCAPSED_STRING));
  49. $currIndex = 0;
  50. while (null !== $currIndex) {
  51. $match = $tokens->findSequence($seq, $currIndex, $end, false);
  52. // did we find a match?
  53. if (null === $match) {
  54. break;
  55. }
  56. // findSequence also returns the tokens, but we're only interested in the indexes, i.e.:
  57. // 0 => function name,
  58. // 1 => bracket "("
  59. // 2 => quoted string passed as 1st parameter
  60. $match = array_keys($match);
  61. // advance tokenizer cursor
  62. $currIndex = $match[2];
  63. // ensure it's a function call (not a method / static call)
  64. $prev = $tokens->getPrevMeaningfulToken($match[0]);
  65. if (null === $prev || $tokens[$prev]->isGivenKind(array(T_OBJECT_OPERATOR, T_DOUBLE_COLON))) {
  66. continue;
  67. }
  68. // ensure the first parameter is just a string (e.g. has nothing appended)
  69. $next = $tokens->getNextMeaningfulToken($match[2]);
  70. if (null === $next || !$tokens[$next]->equalsAny(array(',', ')'))) {
  71. continue;
  72. }
  73. // convert to PCRE
  74. $string = substr($tokens[$match[2]]->getContent(), 1, -1);
  75. $quote = substr($tokens[$match[2]]->getContent(), 0, 1);
  76. $delim = $this->getBestDelimiter($string);
  77. $preg = $delim.addcslashes($string, $delim).$delim.'D'.$map[2];
  78. // check if the preg is valid
  79. if (!$this->checkPreg($preg)) {
  80. continue;
  81. }
  82. // modify function and argument
  83. $tokens[$match[2]]->setContent($quote.$preg.$quote);
  84. $tokens[$match[0]]->setContent($map[1]);
  85. }
  86. }
  87. return $tokens->generateCode();
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getDescription()
  93. {
  94. return 'Replace deprecated ereg regular expression functions with preg. Warning! This could change code behavior.';
  95. }
  96. /**
  97. * Check the validity of a PCRE.
  98. *
  99. * @param string $pattern the regular expression
  100. *
  101. * @return bool
  102. */
  103. private function checkPreg($pattern)
  104. {
  105. return false !== @preg_match($pattern, '');
  106. }
  107. /**
  108. * Get the delimiter that would require the least escaping in a regular expression.
  109. *
  110. * @param string $pattern the regular expression
  111. *
  112. * @return string the preg delimiter
  113. */
  114. private function getBestDelimiter($pattern)
  115. {
  116. // try do find something that's not used
  117. $delimiters = array();
  118. foreach (self::$delimiters as $k => $d) {
  119. if (false === strpos($pattern, $d)) {
  120. return $d;
  121. }
  122. $delimiters[$d] = array(substr_count($pattern, $d), $k);
  123. }
  124. // return the least used delimiter, using the position in the list as a tie breaker
  125. uasort($delimiters, function ($a, $b) {
  126. if ($a[0] === $b[0]) {
  127. return Utils::cmpInt($a, $b);
  128. }
  129. return $a[0] < $b[0] ? -1 : 1;
  130. });
  131. return key($delimiters);
  132. }
  133. /**
  134. * Perform a quick search to see if any ext/ereg functions are used.
  135. *
  136. * @param string $content the content itself
  137. *
  138. * @return bool
  139. */
  140. private function cursoryMatch($content)
  141. {
  142. // just searching for "ereg" or "split" will do, since all the function names start with either of them
  143. return false !== stripos($content, 'ereg') || false !== stripos($content, 'split');
  144. }
  145. }