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