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\Arg; |
19
|
|
|
use PhpParser\Node\Expr\ClassConstFetch; |
20
|
|
|
use PhpParser\Node\Name; |
21
|
|
|
use PhpParser\Node\Name\FullyQualified; |
22
|
|
|
use PhpParser\NodeVisitorAbstract; |
23
|
|
|
|
24
|
|
|
final class ScopeConstStmtNodeVisitor extends NodeVisitorAbstract |
25
|
|
|
{ |
26
|
|
|
private $prefix; |
27
|
|
|
private $namespaceStatements; |
28
|
|
|
private $useStmtCollection; |
29
|
|
|
private $whitelist; |
30
|
|
|
|
31
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
32
|
|
|
string $prefix, |
33
|
|
|
NamespaceStmtCollection $namespaceStatements, |
34
|
|
|
UseStmtCollection $useStatements, |
35
|
|
|
array $whitelist |
36
|
|
|
) { |
37
|
|
|
$this->prefix = $prefix; |
38
|
|
|
$this->namespaceStatements = $namespaceStatements; |
39
|
|
|
$this->useStmtCollection = $useStatements; |
40
|
|
|
$this->whitelist = $whitelist; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
|
|
public function enterNode(Node $node): Node |
47
|
|
|
{ |
48
|
|
|
if (false === ($node instanceof ClassConstFetch)) { |
49
|
|
|
return $node; |
50
|
|
|
} |
51
|
|
|
/** @var ClassConstFetch $node */ |
52
|
|
|
$constClassNode = $node->class; |
53
|
|
|
|
54
|
|
|
if (false === ($constClassNode instanceof Name)) { |
55
|
|
|
return $node; |
56
|
|
|
} |
57
|
|
|
/* @var Name $useStatement */ |
58
|
|
|
|
59
|
|
|
if ($node->hasAttribute('parent') && $node->getAttribute('parent') instanceof Arg) { |
60
|
|
|
return $node; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$useStatement = $this->useStmtCollection->findStatementForName($constClassNode->getFirst()); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$prefix = false; |
66
|
|
|
|
67
|
|
View Code Duplication |
if (null === $useStatement) { |
|
|
|
|
68
|
|
|
if (0 === count($this->namespaceStatements)) { |
69
|
|
|
$newClassNode = new FullyQualified($constClassNode, $constClassNode->getAttributes()); |
70
|
|
|
|
71
|
|
|
$prefix = true; |
72
|
|
|
} else { |
73
|
|
|
$namespaceStatement = $this->namespaceStatements->getNamespaceName(); |
74
|
|
|
|
75
|
|
|
$newClassNode = FullyQualified::concat($namespaceStatement, $constClassNode, $constClassNode->getAttributes()); |
76
|
|
|
} |
77
|
|
|
} else { |
78
|
|
|
$newClassNode = FullyQualified::concat($useStatement, $constClassNode->slice(1), $constClassNode->getAttributes()); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$newClassNode->setAttribute('phpscoper_ignore', true); |
82
|
|
|
|
83
|
|
View Code Duplication |
if (in_array((string) $newClassNode, $this->whitelist)) { |
|
|
|
|
84
|
|
|
// Continue |
85
|
|
|
} elseif ($prefix) { |
86
|
|
|
$newClassNode = FullyQualified::concat($this->prefix, $newClassNode, $newClassNode->getAttributes()); |
87
|
|
|
} else { |
88
|
|
|
return $node; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return new ClassConstFetch($newClassNode, $node->name, $node->getAttributes()); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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.