NodeVisitor   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 0
loc 57
ccs 20
cts 22
cp 0.9091
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A getResult() 0 4 1
A enterNode() 0 12 4
A leaveNode() 0 6 2
A getSource() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Bigwhoop\Trumpet\CodeParsing\Php;
4
5
use PhpParser\Node;
6
use PhpParser\NodeVisitorAbstract;
7
use PhpParser\PrettyPrinterAbstract as Printer;
8
9
final class NodeVisitor extends NodeVisitorAbstract
10
{
11
    /** @var ParserResult */
12
    private $result;
13
14
    /** @var PhpClass|null */
15
    private $currentClass;
16
17
    /** @var Printer */
18
    private $printer;
19
20
    /**
21
     * @param Printer      $printer
22
     * @param ParserResult $result
23
     */
24 11
    public function __construct(Printer $printer, ParserResult $result = null)
25
    {
26 11
        $this->printer = $printer;
27 11
        $this->result = $result ?: new ParserResult();
28 11
    }
29
30 11
    public function getResult(): ParserResult
31
    {
32 11
        return $this->result;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 11
    public function enterNode(Node $node)
39
    {
40 11
        if ($node instanceof Node\Stmt\Class_) {
41 10
            $fullyQualifiedName = $node->namespacedName->toString();
0 ignored issues
show
Bug introduced by
The property namespacedName does not seem to exist in PhpParser\Node\Stmt\Class_.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
42 10
            $this->currentClass = new PhpClass($fullyQualifiedName, [], $this->getSource($node));
43
        } elseif ($node instanceof Node\Stmt\ClassMethod) {
44 8
            $this->currentClass->addMethod(new PhpMethod($node->name, $node->isStatic(), $this->getSource($node)));
45
        } elseif ($node instanceof Node\Stmt\Function_) {
46 8
            $fullyQualifiedName = $node->namespacedName->toString();
0 ignored issues
show
Bug introduced by
The property namespacedName does not seem to exist in PhpParser\Node\Stmt\Function_.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
47 8
            $this->result->addFunction(new PhpFunction($fullyQualifiedName, $this->getSource($node)));
48
        }
49 11
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 11
    public function leaveNode(Node $node)
55
    {
56 11
        if ($node instanceof Node\Stmt\Class_) {
57 10
            $this->result->addClass($this->currentClass);
0 ignored issues
show
Bug introduced by
It seems like $this->currentClass can be null; however, addClass() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
58
        }
59 11
    }
60
61 11
    private function getSource(Node $node): string
62
    {
63 11
        return $this->printer->prettyPrint([$node]);
64
    }
65
}
66