Completed
Push — master ( a70c71...744f1a )
by Théo
04:21 queued 01:53
created

AppendParentNode::findParent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
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\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
final class AppendParentNode extends NodeVisitorAbstract
26
{
27
    private const PARENT_ATTRIBUTE = 'parent';
28
29
    private $stack;
30
31 341
    public static function hasParent(Node $node): bool
32
    {
33 341
        return $node->hasAttribute(self::PARENT_ATTRIBUTE);
34
    }
35
36 341
    public static function getParent(Node $node): Node
37
    {
38 341
        return $node->getAttribute(self::PARENT_ATTRIBUTE);
39
    }
40
41 177
    public static function findParent(Node $node): ?Node
42
    {
43 177
        return $node->hasAttribute(self::PARENT_ATTRIBUTE)
44 177
            ? $node->getAttribute(self::PARENT_ATTRIBUTE)
45 177
            : null
46
        ;
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52 344
    public function beforeTraverse(array $nodes): ?array
53
    {
54 344
        $this->stack = [];
55
56 344
        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...
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62 343
    public function enterNode(Node $node): Node
63
    {
64 343
        if ([] !== $this->stack) {
65 342
            $node->setAttribute(self::PARENT_ATTRIBUTE, $this->stack[count($this->stack) - 1]);
66
        }
67
68 343
        $this->stack[] = $node;
69
70 343
        return $node;
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76 343
    public function leaveNode(Node $node): Node
77
    {
78 343
        array_pop($this->stack);
79
80 343
        return $node;
81
    }
82
}
83