MagicConstantVisitor.php 1.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
<?php namespace SuperClosure\Analyzer\Visitor;

use PhpParser\Node\Scalar\LNumber as NumberNode;
use PhpParser\Node\Scalar\String_ as StringNode;
use PhpParser\Node as AstNode;
use PhpParser\NodeVisitorAbstract as NodeVisitor;

/**
 * This is a visitor that resolves magic constants (e.g., __FILE__) to their
 * intended values within a closure's AST.
 *
 * @internal
 */
final class MagicConstantVisitor extends NodeVisitor
{
    /**
     * @var array
     */
    private $location;

    /**
     * @param array $location
     */
    public function __construct(array $location)
    {
        $this->location = $location;
    }

    public function leaveNode(AstNode $node)
    {
        switch ($node->getType()) {
            case 'Scalar_MagicConst_Class' :
                return new StringNode($this->location['class']);
            case 'Scalar_MagicConst_Dir' :
                return new StringNode($this->location['directory']);
            case 'Scalar_MagicConst_File' :
                return new StringNode($this->location['file']);
            case 'Scalar_MagicConst_Function' :
                return new StringNode($this->location['function']);
            case 'Scalar_MagicConst_Line' :
                return new NumberNode($node->getAttribute('startLine'));
            case 'Scalar_MagicConst_Method' :
                return new StringNode($this->location['method']);
            case 'Scalar_MagicConst_Namespace' :
                return new StringNode($this->location['namespace']);
            case 'Scalar_MagicConst_Trait' :
                return new StringNode($this->location['trait']);
        }
    }
}