Completed
Push — master ( 74c262...0b4843 )
by Théo
03:38 queued 01:33
created

GroupUseStmtTransformer::createUses_()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 20
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\UseStmt;
16
17
use PhpParser\Node\Name;
18
use PhpParser\Node\Stmt\GroupUse;
19
use PhpParser\Node\Stmt\Use_;
20
use PhpParser\Node\Stmt\UseUse;
21
use PhpParser\NodeVisitorAbstract;
22
23
/**
24
 * Transforms the grouped use statements into regular use statements which are easier to work with.
25
 *
26
 * ```
27
 * use A\B\{C\D, function b\c, const D};
28
 * ```
29
 *
30
 * =>
31
 *
32
 * ```
33
 * use Humbug\A\B\C\D;
34
 * use function Humbug\A\B\b\c;
35
 * use const Humbug\A\B\D;
36
 * ```
37
 *
38
 * @private
39
 */
40
final class GroupUseStmtTransformer extends NodeVisitorAbstract
41
{
42
    /**
43
     * @inheritdoc
44
     */
45
    public function beforeTraverse(array $nodes)
46
    {
47
        $newNodes = [];
48
49
        foreach ($nodes as $node) {
50
            if ($node instanceof GroupUse) {
51
                $uses_ = $this->createUses_($node);
52
53
                array_splice($newNodes, count($newNodes), 0, $uses_);
54
            } else {
55
                $newNodes[] = $node;
56
            }
57
        }
58
59
        return $newNodes;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $newNodes; (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
     * @param GroupUse $node
64
     *
65
     * @return Use_[]
66
     */
67
    public function createUses_(GroupUse $node): array
68
    {
69
        return array_map(
70
            function (UseUse $use) use ($node): Use_ {
71
                $newUse = new UseUse(
72
                    Name::concat($node->prefix, $use->name, $use->name->getAttributes()),
73
                    $use->alias,
74
                    $use->type,
75
                    $use->getAttributes()
76
                );
77
78
                return new Use_(
79
                    [$newUse],
80
                    $node->type,
81
                    $node->getAttributes()
82
                );
83
            },
84
            $node->uses
85
        );
86
    }
87
}
88