public function fix(\SplFileInfo $file, Tokens $tokens)
{
$end = $tokens->count() - 1;
foreach (self::$functions as $map) {
// the sequence is the function name, followed by "(" and a quoted string
$seq = array(array(T_STRING, $map[0]), '(', array(T_CONSTANT_ENCAPSED_STRING));
$currIndex = 0;
while (null !== $currIndex) {
$match = $tokens->findSequence($seq, $currIndex, $end, false);
// did we find a match?
if (null === $match) {
break;
}
// findSequence also returns the tokens, but we're only interested in the indexes, i.e.:
// 0 => function name,
// 1 => bracket "("
// 2 => quoted string passed as 1st parameter
$match = array_keys($match);
// advance tokenizer cursor
$currIndex = $match[2];
// ensure it's a function call (not a method / static call)
$prev = $tokens->getPrevMeaningfulToken($match[0]);
if (null === $prev || $tokens[$prev]->isGivenKind(array(T_OBJECT_OPERATOR, T_DOUBLE_COLON))) {
continue;
}
// ensure the first parameter is just a string (e.g. has nothing appended)
$next = $tokens->getNextMeaningfulToken($match[2]);
if (null === $next || !$tokens[$next]->equalsAny(array(',', ')'))) {
continue;
}
// convert to PCRE
$string = substr($tokens[$match[2]]->getContent(), 1, -1);
$quote = substr($tokens[$match[2]]->getContent(), 0, 1);
$delim = $this->getBestDelimiter($string);
$preg = $delim . addcslashes($string, $delim) . $delim . 'D' . $map[2];
// check if the preg is valid
if (!$this->checkPreg($preg)) {
continue;
}
// modify function and argument
$tokens[$match[2]]->setContent($quote . $preg . $quote);
$tokens[$match[0]]->setContent($map[1]);
}
}
}