public function transform(StreamMetaData $metadata): string
    {
        $newExpressionFinder = new NodeFinderVisitor([Node\Expr\New_::class]);

        // TODO: move this logic into walkSyntaxTree(Visitor $nodeVistor) method
        $traverser = new NodeTraverser();
        $traverser->addVisitor($newExpressionFinder);
        $traverser->traverse($metadata->syntaxTree);

        /** @var Node\Expr\New_[] $newExpressions */
        $newExpressions = $newExpressionFinder->getFoundNodes();

        if (empty($newExpressions)) {
            return self::RESULT_ABSTAIN;
        }

        foreach ($newExpressions as $newExpressionNode) {
            $startPosition = $newExpressionNode->getAttribute('startTokenPos');

            $metadata->tokenStream[$startPosition][1] = '\\' . __CLASS__ . '::getInstance()->{';
            if ($metadata->tokenStream[$startPosition+1][0] === T_WHITESPACE) {
                unset($metadata->tokenStream[$startPosition+1]);
            }
            $isExplicitClass  = $newExpressionNode->class instanceof Node\Name;
            $endClassNamePos  = $newExpressionNode->class->getAttribute('endTokenPos');
            $expressionSuffix = $isExplicitClass ? '::class}' : '}';
            $metadata->tokenStream[$endClassNamePos][1] .= $expressionSuffix;
        }

        return self::RESULT_TRANSFORMED;
    }
public function transform(StreamMetaData $metadata): string
    {
        $newExpressionFinder = new NodeFinderVisitor([Node\Expr\New_::class]);

        // TODO: move this logic into walkSyntaxTree(Visitor $nodeVistor) method
        $traverser = new NodeTraverser();
        $traverser->addVisitor($newExpressionFinder);
        $traverser->traverse($metadata->syntaxTree);

        /** @var Node\Expr\New_[] $newExpressions */
        $newExpressions = $newExpressionFinder->getFoundNodes();

        if (empty($newExpressions)) {
            return self::RESULT_ABSTAIN;
        }

        foreach ($newExpressions as $newExpressionNode) {
            $startPosition = $newExpressionNode->getAttribute('startTokenPos');

            $metadata->tokenStream[$startPosition][1] = '\\' . __CLASS__ . '::getInstance()->{';
            if ($metadata->tokenStream[$startPosition+1][0] === T_WHITESPACE) {
                unset($metadata->tokenStream[$startPosition+1]);
            }
            $isExplicitClass  = $newExpressionNode->class instanceof Node\Name;
            $endClassNamePos  = $newExpressionNode->class->getAttribute('endTokenPos');
            $expressionSuffix = $isExplicitClass ? '::class}' : '}';
            $metadata->tokenStream[$endClassNamePos][1] .= $expressionSuffix;
        }

        return self::RESULT_TRANSFORMED;
    }
public function transform ( StreamMetaData $ metadata ) : string { $ newExpressionFinder = new NodeFinderVisitor ( [ Node \ Expr \ New_ :: class ] ) ; // TODO: move this logic into walkSyntaxTree(Visitor $nodeVistor) method $ traverser = new NodeTraverser ( ) ; $ traverser -> addVisitor ( $ newExpressionFinder ) ; $ traverser -> traverse ( $ metadata -> syntaxTree ) ; /** @var Node\Expr\New_[] $newExpressions */ $ newExpressions = $ newExpressionFinder -> getFoundNodes ( ) ; if ( empty ( $ newExpressions ) ) { return self :: RESULT_ABSTAIN ; } foreach ( $ newExpressions as $ newExpressionNode ) { $ startPosition = $ newExpressionNode -> getAttribute ( 'startTokenPos' ) ; $ metadata -> tokenStream [ $ startPosition ] [ 1 ] = '\\' . __CLASS__ . '::getInstance()->{' ; if ( $ metadata -> tokenStream [ $ startPosition + 1 ] [ 0 ] === T_WHITESPACE ) { unset ( $ metadata -> tokenStream [ $ startPosition + 1 ] ) ; } $ isExplicitClass = $ newExpressionNode -> class instanceof Node \ Name ; $ endClassNamePos = $ newExpressionNode -> class -> getAttribute ( 'endTokenPos' ) ; $ expressionSuffix = $ isExplicitClass ? '::class}' : '}' ; $ metadata -> tokenStream [ $ endClassNamePos ] [ 1 ] .= $ expressionSuffix ; } return self :: RESULT_TRANSFORMED ; }
Rewrites all "new" expressions with our implementation

@return string See RESULT_XXX constants in the interface
Rewrites all new expressions with our implementation