<commit_before><?php

namespace sekjun9878\ScopeResolver\NodeVisitor;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt;
use PhpParser\NodeVisitorAbstract;
use sekjun9878\ScopeResolver\NamespaceName;
use sekjun9878\ScopeResolver\Scope\ClassMethodScope;
use sekjun9878\ScopeResolver\Scope\ClassScope;
use sekjun9878\ScopeResolver\Scope\ClosureScope;
use sekjun9878\ScopeResolver\Scope\Definition\ScopeInterface;
use sekjun9878\ScopeResolver\Scope\FunctionScope;
use sekjun9878\ScopeResolver\Scope\RootNamespaceScope;

/**
 * Class ScopeResolver
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class ScopeResolver extends NodeVisitorAbstract
{
    /** @var NamespaceName $namespace */
    protected $namespace;

    /** @var ScopeInterface[] $scope */
    protected $scope = [];

    public function __construct()
    {
        $this->namespace = new NamespaceName([]);
        $this->scope[] = new RootNamespaceScope;
    }

    public function enterNode(Node $node)
    {
        $node->setAttribute("scope", end($this->scope));

        if($node instanceof Stmt\Namespace_ and !empty($node->name))
        {
            $this->namespace = new NamespaceName($node->name->parts);
        }
        elseif($node instanceof Stmt\Class_)
        {
            $this->scope[] = new ClassScope($this->namespace, $node->name);
        }
        elseif($node instanceof Stmt\Function_)
        {
            $this->scope[] = new FunctionScope($this->namespace, $node->name);
        }
        elseif($node instanceof Stmt\ClassMethod)
        {
            $this->scope[] = new ClassMethodScope(end($this->scope), $node->name);
        }
        elseif($node instanceof Expr\Closure)
        {
            $this->scope[] = new ClosureScope(end($this->scope), $node->getAttribute("startFilePos", mt_rand()));
        }
    }

    public function leaveNode(Node $node)
    {
        if($node instanceof Stmt\NameSpace_)
        {
            $this->namespace = new RootNamespaceScope;
        }
        else if(
            $node instanceof Stmt\Class_
            or $node instanceof Stmt\ClassMethod
            or $node instanceof Stmt\Function_
            or $node instanceof Expr\Closure
        )
        {
            array_pop($this->scope);
        }
    }
}<commit_msg>Fix bug in ScopeResolver<commit_after><?php

namespace sekjun9878\ScopeResolver\NodeVisitor;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt;
use PhpParser\NodeVisitorAbstract;
use sekjun9878\ScopeResolver\NamespaceName;
use sekjun9878\ScopeResolver\Scope\ClassMethodScope;
use sekjun9878\ScopeResolver\Scope\ClassScope;
use sekjun9878\ScopeResolver\Scope\ClosureScope;
use sekjun9878\ScopeResolver\Scope\Definition\ScopeInterface;
use sekjun9878\ScopeResolver\Scope\FunctionScope;
use sekjun9878\ScopeResolver\Scope\RootNamespaceScope;

/**
 * Class ScopeResolver
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class ScopeResolver extends NodeVisitorAbstract
{
    /** @var NamespaceName $namespace */
    protected $namespace;

    /** @var ScopeInterface[] $scope */
    protected $scope = [];

    public function __construct()
    {
        $this->namespace = new NamespaceName([]);
        $this->scope = [new RootNamespaceScope];
    }

    public function enterNode(Node $node)
    {
        $node->setAttribute("scope", end($this->scope));

        if($node instanceof Stmt\Namespace_ and !empty($node->name))
        {
            $this->namespace = new NamespaceName($node->name->parts);
        }
        elseif($node instanceof Stmt\Class_)
        {
            $this->scope[] = new ClassScope($this->namespace, $node->name);
        }
        elseif($node instanceof Stmt\Function_)
        {
            $this->scope[] = new FunctionScope($this->namespace, $node->name);
        }
        elseif($node instanceof Stmt\ClassMethod)
        {
            $this->scope[] = new ClassMethodScope(end($this->scope), $node->name);
        }
        elseif($node instanceof Expr\Closure)
        {
            $this->scope[] = new ClosureScope(end($this->scope), $node->getAttribute("startFilePos", mt_rand()));
        }
    }

    public function leaveNode(Node $node)
    {
        if($node instanceof Stmt\Namespace_)
        {
            $this->namespace = new NamespaceName([]);
            $this->scope = [new RootNamespaceScope];
        }
        elseif($node instanceof Stmt\Class_
            or $node instanceof Stmt\ClassMethod
            or $node instanceof Stmt\Function_
            or $node instanceof Expr\Closure)
        {
            array_pop($this->scope);
        }
    }
}
{'fraction_non_alphanumeric': 0.1270967741935484, 'fraction_numerical': 0.013763440860215054, 'mean_word_length': 2.679588607594937, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7299827', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1344, 'n_words': 258}