/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
$tokensAnalyzer = new TokensAnalyzer($tokens);
$classes = array_keys($tokens->findGivenKind(T_CLASS));
$numClasses = count($classes);
for ($i = 0; $i < $numClasses; ++$i) {
$index = $classes[$i];
// is it an an anonymous class definition?
if ($tokensAnalyzer->isAnonymousClass($index)) {
continue;
}
// is it inside a namespace?
$nspIndex = $tokens->getPrevTokenOfKind($index, array(array(T_NAMESPACE, 'namespace')));
if (null !== $nspIndex) {
$nspIndex = $tokens->getNextMeaningfulToken($nspIndex);
// make sure it's not the global namespace, as PHP4 constructors are allowed in there
if (!$tokens[$nspIndex]->equals('{')) {
// unless it's the global namespace, the index currently points to the name
$nspIndex = $tokens->getNextTokenOfKind($nspIndex, array(';', '{'));
if ($tokens[$nspIndex]->equals(';')) {
// the class is inside a (non-block) namespace, no PHP4-code should be in there
break;
}
// the index points to the { of a block-namespace
$nspEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $nspIndex);
if ($index < $nspEnd) {
// the class is inside a block namespace, skip other classes that might be in it
for ($j = $i + 1; $j < $numClasses; ++$j) {
if ($classes[$j] < $nspEnd) {
++$i;
}
}
// and continue checking the classes that might follow
continue;
}
}
}
$classNameIndex = $tokens->getNextMeaningfulToken($index);
$className = $tokens[$classNameIndex]->getContent();
$classStart = $tokens->getNextTokenOfKind($classNameIndex, array('{'));
$classEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $classStart);
$this->fixConstructor($tokens, $className, $classStart, $classEnd);
$this->fixParent($tokens, $classStart, $classEnd);
}
}