1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ComposerRequireCheckerTest\NodeVisitor; |
4
|
|
|
|
5
|
|
|
use ComposerRequireChecker\NodeVisitor\DefinedSymbolCollector; |
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 DefinedSymbolCollectorFunctionalTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var DefinedSymbolCollector |
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 DefinedSymbolCollector(); |
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 testWillCollectSymbolsDefinedInThisFile() |
49
|
|
|
{ |
50
|
|
|
$this->traverseClassAST(self::class); |
51
|
|
|
|
52
|
|
|
self::assertSameCollectedSymbols( |
53
|
|
|
['ComposerRequireCheckerTest\NodeVisitor\DefinedSymbolCollectorFunctionalTest'], |
54
|
|
|
$this->collector->getDefinedSymbols() |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testWillCollectFunctionDefinition() |
59
|
|
|
{ |
60
|
|
|
$this->traverseStringAST('function foo() {}'); |
61
|
|
|
|
62
|
|
|
self::assertSameCollectedSymbols( |
63
|
|
|
['foo'], |
64
|
|
|
$this->collector->getDefinedSymbols() |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testWillCollectNamespacedFunctionDefinition() |
69
|
|
|
{ |
70
|
|
|
$this->traverseStringAST('namespace Foo; function foo() {}'); |
71
|
|
|
|
72
|
|
|
self::assertSameCollectedSymbols( |
73
|
|
|
['Foo\foo'], |
74
|
|
|
$this->collector->getDefinedSymbols() |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testWillCollectConstDefinition() |
79
|
|
|
{ |
80
|
|
|
$this->traverseStringAST('const foo = "bar", baz = "tab";'); |
81
|
|
|
|
82
|
|
|
self::assertSameCollectedSymbols( |
83
|
|
|
['foo', 'baz'], |
84
|
|
|
$this->collector->getDefinedSymbols() |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testWillCollectNamespacedConstDefinition() |
89
|
|
|
{ |
90
|
|
|
$this->traverseStringAST('namespace Foo; const foo = "bar", baz = "tab";'); |
91
|
|
|
|
92
|
|
|
self::assertSameCollectedSymbols( |
93
|
|
|
['Foo\foo', 'Foo\baz'], |
94
|
|
|
$this->collector->getDefinedSymbols() |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function traverseStringAST(string $phpSource) : array |
99
|
|
|
{ |
100
|
|
|
return $this->traverser->traverse($this->parser->parse('<?php ' . $phpSource)); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function traverseClassAST(string $className) : array |
104
|
|
|
{ |
105
|
|
|
return $this->traverser->traverse( |
106
|
|
|
$this->parser->parse( |
|
|
|
|
107
|
|
|
file_get_contents((new \ReflectionClass($className))->getFileName()) |
108
|
|
|
) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private static function assertSameCollectedSymbols(array $expected, array $actual) |
113
|
|
|
{ |
114
|
|
|
self::assertSame(array_diff($expected, $actual), array_diff($actual, $expected)); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
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.