FindInvalidRouteDefinitions   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B nodeVisitor() 0 25 7
A findFunctionCalls() 0 7 1
A check() 0 5 1
A getFunctionName() 0 3 1
1
<?php
2
3
namespace Juddling\RouteChecker;
4
5
use PhpParser\Node;
6
use PhpParser\NodeTraverser;
7
use PhpParser\NodeVisitor\FindingVisitor;
8
9
class FindInvalidRouteDefinitions extends FindInvalid
10
{
11
    protected $nameOfArgument = 'Route Controller Action';
12
13
    public function findFunctionCalls($file)
14
    {
15
        $traverser = new NodeTraverser;
16
        $visitor = new FindingVisitor($this->nodeVisitor($file));
17
        $traverser->addVisitor($visitor);
18
        $traverser->traverse($this->parser->parse(file_get_contents($file)));
19
        return $visitor->getFoundNodes();
20
    }
21
22
    protected function nodeVisitor($file): callable
23
    {
24
        return function (Node $node) use ($file) {
25
            if ($node instanceof Node\Expr\StaticCall) {
26
                foreach ($node->args as $argument) {
27
                    $routeConfiguration = $argument->value;
28
                    if ($routeConfiguration instanceof Node\Expr\Array_) {
29
                        foreach ($routeConfiguration->items as $routeConfigurationItem) {
30
                            if ($routeConfigurationItem->key instanceof Node\Scalar\String_ && $routeConfigurationItem->key->value === 'uses') {
31
                                $action = $routeConfigurationItem->value->value;
32
33
                                $this->results->push([
34
                                    'name' => $action,
35
                                    'valid' => $this->check($action),
36
                                    'file' => $file,
37
                                ]);
38
39
                                return true;
40
                            }
41
                        }
42
                    }
43
                }
44
            }
45
46
            return false;
47
        };
48
    }
49
50
    protected function check(string $argument)
51
    {
52
        [$className, $methodName] = explode('@', $argument);
53
        $fullyQualifiedName = "App\Http\Controllers\\$className";
54
        return in_array($methodName, get_class_methods($fullyQualifiedName));
55
    }
56
57
    protected function getFunctionName()
58
    {
59
        throw new \RuntimeException;
60
    }
61
}
62