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\Node\Expr\ClassConstFetch; |
19
|
|
|
use PhpParser\Node\Expr\FuncCall; |
20
|
|
|
use PhpParser\Node\Expr\New_; |
21
|
|
|
use PhpParser\Node\Expr\StaticCall; |
22
|
|
|
use PhpParser\Node\Name; |
23
|
|
|
use PhpParser\Node\Name\FullyQualified; |
24
|
|
|
use PhpParser\Node\Stmt\GroupUse; |
25
|
|
|
use PhpParser\Node\Stmt\Use_; |
26
|
|
|
use PhpParser\Node\Stmt\UseUse; |
27
|
|
|
use PhpParser\NodeVisitorAbstract; |
28
|
|
|
|
29
|
|
|
final class IgnoreNamespaceScoperNodeVisitor extends NodeVisitorAbstract |
30
|
|
|
{ |
31
|
|
|
private $whitelist; |
32
|
|
|
private $whitelister; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string[] $whitelist |
36
|
|
|
* @param callable $whitelister |
37
|
|
|
*/ |
38
|
|
|
public function __construct(array $whitelist, callable $whitelister) |
39
|
|
|
{ |
40
|
|
|
$this->whitelist = $whitelist; |
41
|
|
|
$this->whitelister = $whitelister; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
|
|
public function enterNode(Node $node) |
48
|
|
|
{ |
49
|
|
|
// |
50
|
|
|
// For use statements |
51
|
|
|
// |
52
|
|
|
if ($node instanceof UseUse |
53
|
|
|
&& $node->hasAttribute('parent') |
54
|
|
|
&& false === ($node->getAttribute('parent') instanceof GroupUse) |
55
|
|
|
&& ( |
56
|
|
|
// Is not a whitelisted use statement of the global namespace |
57
|
|
|
(1 === count($node->name->parts) && false === ($this->whitelister)($node->name->getFirst())) |
58
|
|
|
// Is not from the Composer namespace |
59
|
|
|
|| 'Composer' === $node->name->getFirst() |
60
|
|
|
// Is not a whitelisted class |
61
|
|
|
|| ($node->getAttribute('parent') instanceof Use_ |
62
|
|
|
&& Use_::TYPE_NORMAL === $node->getAttribute('parent')->type |
63
|
|
|
&& in_array((string) $node->name, $this->whitelist) |
64
|
|
|
) |
65
|
|
|
) |
66
|
|
|
) { |
67
|
|
|
$node->setAttribute('phpscoper_ignore', true); |
68
|
|
|
|
69
|
|
|
return $node; |
70
|
|
|
} elseif (false === ($node instanceof FullyQualified) || false === $node->isFullyQualified()) { |
71
|
|
|
return $node; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** @var FullyQualified $node */ |
75
|
|
|
|
76
|
|
|
// Is a fully qualified call from the global namespace which is not whitelisted |
77
|
|
View Code Duplication |
if (1 === count($node->parts) |
|
|
|
|
78
|
|
|
&& (false === ($this->whitelister)($node->getFirst())) |
79
|
|
|
) { |
80
|
|
|
$node->setAttribute('phpscoper_ignore', true); |
81
|
|
|
|
82
|
|
|
return $node; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (false === $node->hasAttribute('parent')) { |
86
|
|
|
return $node; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$parentNode = $node->getAttribute('parent'); |
90
|
|
|
|
91
|
|
|
// Is a static method call of a whitelisted class |
92
|
|
|
if ($parentNode instanceof StaticCall |
93
|
|
|
&& in_array((string) $parentNode->class, $this->whitelist) |
94
|
|
|
) { |
95
|
|
|
$node->setAttribute('phpscoper_ignore', true); |
96
|
|
|
|
97
|
|
|
return $node; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// // Is a method call of a whitelisted class |
|
|
|
|
101
|
|
|
// if ($parentNode instanceof FuncCall |
102
|
|
|
// && $parentNode->name instanceof Name |
103
|
|
|
// && in_array((string) $parentNode->name->slice(0, -1), $this->whitelist) |
104
|
|
|
// ) { |
105
|
|
|
// $node->setAttribute('phpscoper_ignore', true); |
106
|
|
|
// |
107
|
|
|
// return $node; |
108
|
|
|
// } |
109
|
|
|
|
110
|
|
|
// Is a new instance of a whitelisted class |
111
|
|
|
if ($parentNode instanceof New_ |
112
|
|
|
&& in_array((string) $parentNode->class->toString(), $this->whitelist) |
113
|
|
|
) { |
114
|
|
|
$node->setAttribute('phpscoper_ignore', true); |
115
|
|
|
|
116
|
|
|
return $node; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Is a constant call of a whitelisted class |
120
|
|
|
if ($parentNode instanceof ClassConstFetch |
121
|
|
|
&& in_array((string) $node, $this->whitelist) |
122
|
|
|
) { |
123
|
|
|
$node->setAttribute('phpscoper_ignore', true); |
124
|
|
|
|
125
|
|
|
return $node; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $node; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.