alex-5zi/hexlet-psr-linter

View on GitHub
src/Linter/LinterVisitor.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php
namespace hexletPsrLinter\Linter;

use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;

class LinterVisitor extends NodeVisitorAbstract
{
    private $rules;
    private $path;
    private $fix;

    public function __construct($rules, $path, $fix)
    {
        $this->rules = $rules;
        $this->path = $path;
        $this->fix = $fix;
    }

    public function beforeTraverse(array $nodes)
    {
        foreach ($this->rules as $rule) {
            $rule->beforeCheck($nodes);
            $rule->setPath($this->path);
        }
    }

    public function enterNode(Node $node)
    {
        foreach ($this->rules as $rule) {
            $rule->check($node);
        }
    }

    public function leaveNode(Node $node)
    {
        if ($this->fix) {
            foreach ($this->rules as $rule) {
                $rule->autofix($node);
            }
        }
    }

    public function afterTraverse(array $nodes)
    {
        foreach ($this->rules as $rule) {
            $rule->afterCheck($nodes);
        }
    }
}