FindInvalid   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 33
dl 0
loc 63
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A findFunctionCalls() 0 31 5
A __construct() 0 4 1
A renderTable() 0 12 2
1
<?php
2
3
namespace Juddling\RouteChecker;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Routing\Route;
0 ignored issues
show
Bug introduced by
The type Illuminate\Routing\Route was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use PhpParser\Node;
8
use PhpParser\NodeTraverser;
9
use PhpParser\NodeVisitor\FindingVisitor;
10
use PhpParser\ParserFactory;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
abstract class FindInvalid
14
{
15
    protected $parser;
16
    /** @var Collection */
17
    protected $routeCalls;
18
    protected $results;
19
    protected $nameOfArgument = 'Parameter';
20
21
    public function __construct()
22
    {
23
        $this->parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
24
        $this->results = new Collection;
25
    }
26
27
    abstract protected function check(string $argument);
28
29
    abstract protected function getFunctionName();
30
31
    public function findFunctionCalls($file)
32
    {
33
        $code = file_get_contents($file);
34
35
        $traverser = new NodeTraverser;
36
        $visitor = new FindingVisitor(function (Node $node) use ($file) {
37
            if ($node instanceof Node\Expr\FuncCall) {
38
                if ($node->name instanceof Node\Name && $node->name->toString() === $this->getFunctionName()) {
39
                    $firstArgument = $node->args[0];
40
41
                    if ($firstArgument->value instanceof \PhpParser\Node\Scalar\String_) {
42
                        // value of first argument is route name
43
                        $stringScalar = $firstArgument->value;
44
                        $firstArgument = $stringScalar->value;
45
46
                        $this->results->push([
47
                            'name' => $firstArgument,
48
                            'valid' => $this->check($firstArgument),
49
                            'file' => $file
50
                        ]);
51
52
                        return true;
53
                    }
54
                }
55
            }
56
57
            return false;
58
        });
59
        $traverser->addVisitor($visitor);
60
        $traverser->traverse($this->parser->parse($code));
61
        return $visitor->getFoundNodes();
62
    }
63
64
    public function renderTable(OutputInterface $output)
65
    {
66
        $table = new \Symfony\Component\Console\Helper\Table($output);
67
        $table->setHeaders([$this->nameOfArgument, 'Valid', 'File']);
68
69
        $table->addRows($this->results->sortBy('valid')->map(function ($call) {
70
            $call['valid'] = $call['valid'] ? '✅' : '❌';
71
            return $call;
72
        })->toArray());
73
74
        $table->setStyle('borderless');
75
        $table->render();
76
    }
77
}
78