# betterdocs/3.8.9/includes/Dependencies/SuperClosure/Analyzer/Visitor/MagicConstantVisitor.php

BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ &amp; Chatbot, version 3.8.9. 51 lines.

- Page: https://pluginprobe.com/plugins/betterdocs/3.8.9/code/includes/Dependencies/SuperClosure/Analyzer/Visitor/MagicConstantVisitor.php
- Raw: https://pluginprobe.com/plugins/betterdocs/3.8.9/raw/includes/Dependencies/SuperClosure/Analyzer/Visitor/MagicConstantVisitor.php
- Modified: 2023-08-14T08:41:14+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/betterdocs/3.8.9/code/includes/Dependencies/SuperClosure/Analyzer/Visitor/MagicConstantVisitor.php#L10-L20`.

```php
<?php namespace WPDeveloper\BetterDocs\Dependencies\SuperClosure\Analyzer\Visitor;

use WPDeveloper\BetterDocs\Dependencies\PhpParser\Node\Scalar\LNumber as NumberNode;
use WPDeveloper\BetterDocs\Dependencies\PhpParser\Node\Scalar\String_ as StringNode;
use WPDeveloper\BetterDocs\Dependencies\PhpParser\Node as AstNode;
use WPDeveloper\BetterDocs\Dependencies\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') ?: 0);
            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'] ?: '');
        }
    }
}

```
