1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ComposerRequireCheckerTest\NodeVisitor; |
4
|
|
|
|
5
|
|
|
use ComposerRequireChecker\NodeVisitor\UsedSymbolCollector; |
6
|
|
|
use PhpParser\Node; |
7
|
|
|
use PhpParser\NodeTraverser; |
8
|
|
|
use PhpParser\NodeTraverserInterface; |
9
|
|
|
use PhpParser\NodeVisitor\NameResolver; |
10
|
|
|
use PhpParser\Parser; |
11
|
|
|
use PhpParser\ParserFactory; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @coversNothing |
15
|
|
|
* |
16
|
|
|
* @group functional |
17
|
|
|
*/ |
18
|
|
|
final class UsedSymbolCollectorFunctionalTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var UsedSymbolCollector |
22
|
|
|
*/ |
23
|
|
|
private $collector; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Parser |
27
|
|
|
*/ |
28
|
|
|
private $parser; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var NodeTraverserInterface |
32
|
|
|
*/ |
33
|
|
|
private $traverser; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritDoc} |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
protected function setUp() |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$this->collector = new UsedSymbolCollector(); |
41
|
|
|
$this->parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7); |
42
|
|
|
$this->traverser = new NodeTraverser(); |
43
|
|
|
|
44
|
|
|
$this->traverser->addVisitor(new NameResolver()); |
45
|
|
|
$this->traverser->addVisitor($this->collector); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testWillCollectSymbolsUsedInThisFile() |
49
|
|
|
{ |
50
|
|
|
$this->traverseClassAST(self::class); |
51
|
|
|
|
52
|
|
|
self::assertSameCollectedSymbols( |
53
|
|
|
[ |
54
|
|
|
'ComposerRequireChecker\NodeVisitor\UsedSymbolCollector', |
55
|
|
|
'PHPUnit_Framework_TestCase', |
56
|
|
|
'PhpParser\NodeTraverser', |
57
|
|
|
'PhpParser\ParserFactory', |
58
|
|
|
'file_get_contents', |
59
|
|
|
'ReflectionClass', |
60
|
|
|
'array_diff', |
61
|
|
|
'self', |
62
|
|
|
'PhpParser\NodeVisitor\NameResolver', |
63
|
|
|
'string', |
64
|
|
|
'array', |
65
|
|
|
], |
66
|
|
|
$this->collector->getCollectedSymbols() |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testWillCollectFunctionDefinitionTypes() |
71
|
|
|
{ |
72
|
|
|
$this->traverseStringAST('<?php function foo(My\ParameterType $bar, array $fooBar) {}'); |
73
|
|
|
|
74
|
|
|
self::assertSameCollectedSymbols( |
75
|
|
|
[ |
76
|
|
|
'My\ParameterType', |
77
|
|
|
'array', |
78
|
|
|
], |
79
|
|
|
$this->collector->getCollectedSymbols() |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testWillCollectMethodDefinitionTypes() |
84
|
|
|
{ |
85
|
|
|
$this->traverseStringAST('<?php class Foo { function foo(My\ParameterType $bar, array $fooBar) {}}'); |
86
|
|
|
|
87
|
|
|
self::assertSameCollectedSymbols( |
88
|
|
|
[ |
89
|
|
|
'My\ParameterType', |
90
|
|
|
'array', |
91
|
|
|
], |
92
|
|
|
$this->collector->getCollectedSymbols() |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testWillCollectFunctionReturnTypes() |
97
|
|
|
{ |
98
|
|
|
$this->traverseStringAST('<?php function foo($bar) : My\ReturnType {}'); |
99
|
|
|
|
100
|
|
|
self::assertSameCollectedSymbols( |
101
|
|
|
[ |
102
|
|
|
'My\ReturnType', |
103
|
|
|
], |
104
|
|
|
$this->collector->getCollectedSymbols() |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testWillCollectMethodReturnTypes() |
109
|
|
|
{ |
110
|
|
|
$this->traverseStringAST('<?php class Foo { function foo($bar) : My\ReturnType {}}'); |
111
|
|
|
|
112
|
|
|
self::assertSameCollectedSymbols( |
113
|
|
|
[ |
114
|
|
|
'My\ReturnType', |
115
|
|
|
], |
116
|
|
|
$this->collector->getCollectedSymbols() |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testWillCollectSimpleFunctionReturnTypes() |
121
|
|
|
{ |
122
|
|
|
$this->traverseStringAST('<?php function foo($bar) : int {}'); |
123
|
|
|
|
124
|
|
|
self::assertSameCollectedSymbols( |
125
|
|
|
[ |
126
|
|
|
'int', |
127
|
|
|
], |
128
|
|
|
$this->collector->getCollectedSymbols() |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testWontCollectAnyUsageTypes() |
133
|
|
|
{ |
134
|
|
|
$this->traverseStringAST('<?php function foo($bar) {}'); |
135
|
|
|
|
136
|
|
|
self::assertSameCollectedSymbols( |
137
|
|
|
[], |
138
|
|
|
$this->collector->getCollectedSymbols() |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function traverseStringAST(string $stringAST) |
143
|
|
|
{ |
144
|
|
|
return $this->traverser->traverse( |
145
|
|
|
$this->parser->parse( |
|
|
|
|
146
|
|
|
$stringAST |
147
|
|
|
) |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
private function traverseClassAST(string $className) : array |
152
|
|
|
{ |
153
|
|
|
return $this->traverseStringAST( |
154
|
|
|
file_get_contents((new \ReflectionClass($className))->getFileName()) |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private static function assertSameCollectedSymbols(array $expected, array $actual) |
159
|
|
|
{ |
160
|
|
|
self::assertSame(array_diff($expected, $actual), array_diff($actual, $expected)); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
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.