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; |
|
|
|
|
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
|
|
|
|
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.