Test Failed
Push — master ( 53494e...bc3061 )
by Théo
02:45
created

ParentNodeAppender::findParent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 7
rs 10
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
use function count;
20
21
/**
22
 * Appends the parent node as an attribute to each node. This allows to have more context in the other visitors when
23
 * inspecting a node.
24
 * TODO: rename to ParentNodeAppender.
25
 *
26
 * @private
27
 */
28
final class ParentNodeAppender extends NodeVisitorAbstract
29
{
30
    public const PARENT_ATTRIBUTE = 'parent';
31
32
    private $stack;
33
34
    public static function hasParent(Node $node): bool
35
    {
36
        return $node->hasAttribute(self::PARENT_ATTRIBUTE);
37
    }
38
39
    public static function getParent(Node $node): Node
40
    {
41
        return $node->getAttribute(self::PARENT_ATTRIBUTE);
42
    }
43
44
    public static function findParent(Node $node): ?Node
45
    {
46
        return $node->hasAttribute(self::PARENT_ATTRIBUTE)
47
            ? $node->getAttribute(self::PARENT_ATTRIBUTE)
48
            : null
49
        ;
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function beforeTraverse(array $nodes): ?array
56
    {
57
        $this->stack = [];
58
59
        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...
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function enterNode(Node $node): Node
66
    {
67
        if ([] !== $this->stack) {
68
            $node->setAttribute(self::PARENT_ATTRIBUTE, $this->stack[count($this->stack) - 1]);
69
        }
70
71
        $this->stack[] = $node;
72
73
        return $node;
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function leaveNode(Node $node): Node
80
    {
81
        array_pop($this->stack);
82
83
        return $node;
84
    }
85
}
86