Passed
Push — master ( 62af2e...d65fca )
by Théo
07:27 queued 04:04
created

AppendParentNode::beforeTraverse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper\PhpParser\NodeVisitor;
16
17
use PhpParser\Node;
18
use PhpParser\NodeVisitorAbstract;
19
20
/**
21
 * Appends the parent node as an attribute to each node. This allows to have more context in the other visitors when
22
 * inspecting a node.
23
 * TODO: rename to ParentNodeAppender.
24
 *
25
 * @private
26
 */
27
final class AppendParentNode extends NodeVisitorAbstract
28
{
29
    private const PARENT_ATTRIBUTE = 'parent';
30
31
    private $stack;
32
33 368
    public static function hasParent(Node $node): bool
34
    {
35 368
        return $node->hasAttribute(self::PARENT_ATTRIBUTE);
36
    }
37
38 368
    public static function getParent(Node $node): Node
39
    {
40 368
        return $node->getAttribute(self::PARENT_ATTRIBUTE);
41
    }
42
43 190
    public static function findParent(Node $node): ?Node
44
    {
45 190
        return $node->hasAttribute(self::PARENT_ATTRIBUTE)
46 190
            ? $node->getAttribute(self::PARENT_ATTRIBUTE)
47 190
            : null
48
        ;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54 371
    public function beforeTraverse(array $nodes): ?array
55
    {
56 371
        $this->stack = [];
57
58 371
        return $nodes;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $nodes; (array) is incompatible with the return type declared by the interface PhpParser\NodeVisitor::beforeTraverse of type null|PhpParser\Node[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64 370
    public function enterNode(Node $node): Node
65
    {
66 370
        if ([] !== $this->stack) {
67 369
            $node->setAttribute(self::PARENT_ATTRIBUTE, $this->stack[count($this->stack) - 1]);
68
        }
69
70 370
        $this->stack[] = $node;
71
72 370
        return $node;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 370
    public function leaveNode(Node $node): Node
79
    {
80 370
        array_pop($this->stack);
81
82 370
        return $node;
83
    }
84
}
85