1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. Cycle ProxyFactory |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Valentin V (Vvval) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\ORM\Promise\Visitor; |
13
|
|
|
|
14
|
|
|
use PhpParser\Builder\Use_; |
15
|
|
|
use PhpParser\Node; |
16
|
|
|
use PhpParser\NodeVisitorAbstract; |
17
|
|
|
|
18
|
|
|
use function Cycle\ORM\Promise\inject; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Add use statement to the code. |
22
|
|
|
*/ |
23
|
|
|
final class AddUseStmts extends NodeVisitorAbstract |
24
|
|
|
{ |
25
|
|
|
/** @var array */ |
26
|
|
|
private $useStmts; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param array $useStmts |
30
|
|
|
*/ |
31
|
|
|
public function __construct(array $useStmts) |
32
|
|
|
{ |
33
|
|
|
$this->useStmts = $useStmts; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function leaveNode(Node $node) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
if ($node instanceof Node\Stmt\Namespace_) { |
42
|
|
|
$node->stmts = inject( |
43
|
|
|
$node->stmts, |
44
|
|
|
Node\Stmt\Class_::class, |
45
|
|
|
$this->removeDuplicates($node->stmts, $this->buildUseStmts()) |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Node\Stmt[] $stmts |
54
|
|
|
* @param Node[] $nodes |
55
|
|
|
* @return Node\Stmt\Use_[] |
56
|
|
|
*/ |
57
|
|
|
private function removeDuplicates(array $stmts, array $nodes): array |
58
|
|
|
{ |
59
|
|
|
$uses = $this->getExistingUseParts($stmts); |
60
|
|
|
|
61
|
|
|
foreach ($nodes as $i => $node) { |
62
|
|
|
if (!$node instanceof Node\Stmt\Use_) { |
63
|
|
|
continue; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
foreach ($node->uses as $use) { |
67
|
|
|
if (in_array($use->name->parts, $uses, true)) { |
68
|
|
|
unset($nodes[$i]); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $nodes; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Node\Stmt[] $stmts |
78
|
|
|
* @return string[] |
79
|
|
|
*/ |
80
|
|
|
private function getExistingUseParts(array $stmts): array |
81
|
|
|
{ |
82
|
|
|
$parts = []; |
83
|
|
|
foreach ($stmts as $stmt) { |
84
|
|
|
if (!$stmt instanceof Node\Stmt\Use_) { |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
foreach ($stmt->uses as $use) { |
89
|
|
|
$parts[] = $use->name->parts; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $parts; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return Node[] |
98
|
|
|
*/ |
99
|
|
|
private function buildUseStmts(): array |
100
|
|
|
{ |
101
|
|
|
$stmts = []; |
102
|
|
|
foreach ($this->useStmts as $name) { |
103
|
|
|
$stmt = new Use_(new Node\Name($name), Node\Stmt\Use_::TYPE_NORMAL); |
104
|
|
|
$stmts[] = $stmt->getNode(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $stmts; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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.