Completed
Branch feature/add-scrutinizer (27b569)
by Matthias
02:05
created

UsedSymbolCollectorTest   B

Complexity

Total Complexity 22

Size/Duplication

Total Lines 291
Duplicated Lines 81.1 %

Coupling/Cohesion

Components 1
Dependencies 18

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
lcom 1
cbo 18
dl 236
loc 291
rs 7.3333
c 1
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testExtendingClass() 0 11 1
A testExtendingInterface() 12 12 1
A testImplements() 12 12 1
A testStaticCall() 12 12 1
A testStaticPropertyFetch() 12 12 1
A testClassConstantFetch() 12 12 1
A testNew() 12 12 1
A testInstanceof() 12 12 1
A testCatch() 0 11 1
A testFunctionCallUsage() 12 12 1
A testFunctionParameterType() 15 15 1
A testFunctionParameterTypeAsString() 15 15 1
A testMethodParameterType() 15 15 1
A testMethodParameterTypeAsString() 15 15 1
A testFunctionReturnType() 12 12 1
A testFunctionReturnTypeAsString() 12 12 1
A testMethodReturnType() 12 12 1
A testMethodReturnTypeAsString() 12 12 1
A testConstantFetch() 12 12 1
A testTraits() 10 10 1
A testBeforeTraverseResetsRecordedSymbols() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace ComposerRequireCheckerTest\NodeVisitor;
4
5
use ComposerRequireChecker\NodeVisitor\UsedSymbolCollector;
6
use PhpParser\Node\Expr\ClassConstFetch;
7
use PhpParser\Node\Expr\ConstFetch;
8
use PhpParser\Node\Expr\FuncCall;
9
use PhpParser\Node\Expr\Instanceof_;
10
use PhpParser\Node\Expr\New_;
11
use PhpParser\Node\Expr\StaticCall;
12
use PhpParser\Node\Expr\StaticPropertyFetch;
13
use PhpParser\Node\Expr\Variable;
14
use PhpParser\Node\Name;
15
use PhpParser\Node\Param;
16
use PhpParser\Node\Stmt\Catch_;
17
use PhpParser\Node\Stmt\Class_;
18
use PhpParser\Node\Stmt\ClassMethod;
19
use PhpParser\Node\Stmt\Function_;
20
use PhpParser\Node\Stmt\Interface_;
21
use PhpParser\Node\Stmt\TraitUse;
22
use PHPUnit\Framework\TestCase;
23
24
/**
25
 * @covers \ComposerRequireChecker\NodeVisitor\UsedSymbolCollector
26
 */
27
class UsedSymbolCollectorTest extends TestCase
28
{
29
    /** @var UsedSymbolCollector */
30
    private $visitor;
31
32
    protected function setUp()
33
    {
34
        parent::setUp();
35
36
        $this->visitor = new UsedSymbolCollector();
37
    }
38
39
    public function testExtendingClass()
40
    {
41
        $node = new Class_('Foo');
42
        $node->extends = new Name('Bar');
43
44
        $this->visitor->enterNode($node);
45
46
        $symbols = $this->visitor->getCollectedSymbols();
47
        $this->assertCount(1, $symbols);
48
        $this->assertContains('Bar', $symbols);
49
    }
50
51 View Code Duplication
    public function testExtendingInterface()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
52
    {
53
        $node = new Interface_('Foo');
54
        $node->extends = [new Name('Bar'), new Name('Baz')];
55
56
        $this->visitor->enterNode($node);
57
58
        $symbols = $this->visitor->getCollectedSymbols();
59
        $this->assertCount(2, $symbols);
60
        $this->assertContains('Bar', $symbols);
61
        $this->assertContains('Baz', $symbols);
62
    }
63
64 View Code Duplication
    public function testImplements()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
65
    {
66
        $node = new Class_('Foo');
67
        $node->implements = [new Name('Bar'), new Name('Baz')];
68
69
        $this->visitor->enterNode($node);
70
71
        $symbols = $this->visitor->getCollectedSymbols();
72
        $this->assertCount(2, $symbols);
73
        $this->assertContains('Bar', $symbols);
74
        $this->assertContains('Baz', $symbols);
75
    }
76
77 View Code Duplication
    public function testStaticCall()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
78
    {
79
        $class = new Name('Foo');
80
        $node = new StaticCall($class, 'foo');
81
        $node->class = $class;
82
83
        $this->visitor->enterNode($node);
84
85
        $symbols = $this->visitor->getCollectedSymbols();
86
        $this->assertCount(1, $symbols);
87
        $this->assertContains('Foo', $symbols);
88
    }
89
90 View Code Duplication
    public function testStaticPropertyFetch()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
91
    {
92
        $class = new Name('Foo');
93
        $node = new StaticPropertyFetch($class, 'foo');
94
        $node->class = $class;
95
96
        $this->visitor->enterNode($node);
97
98
        $symbols = $this->visitor->getCollectedSymbols();
99
        $this->assertCount(1, $symbols);
100
        $this->assertContains('Foo', $symbols);
101
    }
102
103 View Code Duplication
    public function testClassConstantFetch()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
104
    {
105
        $class = new Name('Foo');
106
        $node = new ClassConstFetch($class, 'FOO');
107
        $node->class = $class;
108
109
        $this->visitor->enterNode($node);
110
111
        $symbols = $this->visitor->getCollectedSymbols();
112
        $this->assertCount(1, $symbols);
113
        $this->assertContains('Foo', $symbols);
114
    }
115
116 View Code Duplication
    public function testNew()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
117
    {
118
        $class = new Name('Foo');
119
        $node = new New_($class);
120
        $node->class = $class;
121
122
        $this->visitor->enterNode($node);
123
124
        $symbols = $this->visitor->getCollectedSymbols();
125
        $this->assertCount(1, $symbols);
126
        $this->assertContains('Foo', $symbols);
127
    }
128
129 View Code Duplication
    public function testInstanceof()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
130
    {
131
        $class = new Name('Foo');
132
        $node = new Instanceof_(new Variable('foo'), $class);
133
        $node->class = $class;
134
135
        $this->visitor->enterNode($node);
136
137
        $symbols = $this->visitor->getCollectedSymbols();
138
        $this->assertCount(1, $symbols);
139
        $this->assertContains('Foo', $symbols);
140
    }
141
142
    public function testCatch()
143
    {
144
        $class = new Name('Foo');
145
        $node = new Catch_([$class], 'e');
146
147
        $this->visitor->enterNode($node);
148
149
        $symbols = $this->visitor->getCollectedSymbols();
150
        $this->assertCount(1, $symbols);
151
        $this->assertContains('Foo', $symbols);
152
    }
153
154 View Code Duplication
    public function testFunctionCallUsage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
155
    {
156
        $functionName = new Name('foo');
157
        $node = new FuncCall($functionName);
158
        $node->name = $functionName;
159
160
        $this->visitor->enterNode($node);
161
162
        $symbols = $this->visitor->getCollectedSymbols();
163
        $this->assertCount(1, $symbols);
164
        $this->assertContains('foo', $symbols);
165
    }
166
167 View Code Duplication
    public function testFunctionParameterType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
168
    {
169
        $functionName = new Name('foo');
170
        $node = new Function_($functionName);
171
        $node->name = $functionName;
0 ignored issues
show
Documentation Bug introduced by
It seems like $functionName of type object<PhpParser\Node\Name> is incompatible with the declared type string of property $name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
172
        $param = new Param('bar');
173
        $param->type = new Name('Baz');
174
        $node->params = [$param];
175
176
        $this->visitor->enterNode($node);
177
178
        $symbols = $this->visitor->getCollectedSymbols();
179
        $this->assertCount(1, $symbols);
180
        $this->assertContains('Baz', $symbols);
181
    }
182
183 View Code Duplication
    public function testFunctionParameterTypeAsString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
184
    {
185
        $functionName = new Name('foo');
186
        $node = new Function_($functionName);
187
        $node->name = $functionName;
0 ignored issues
show
Documentation Bug introduced by
It seems like $functionName of type object<PhpParser\Node\Name> is incompatible with the declared type string of property $name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
188
        $param = new Param('bar');
189
        $param->type = 'Baz';
190
        $node->params = [$param];
191
192
        $this->visitor->enterNode($node);
193
194
        $symbols = $this->visitor->getCollectedSymbols();
195
        $this->assertCount(1, $symbols);
196
        $this->assertContains('Baz', $symbols);
197
    }
198
199 View Code Duplication
    public function testMethodParameterType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
200
    {
201
        $functionName = new Name('foo');
202
        $node = new ClassMethod($functionName);
203
        $node->name = $functionName;
0 ignored issues
show
Documentation Bug introduced by
It seems like $functionName of type object<PhpParser\Node\Name> is incompatible with the declared type string of property $name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
204
        $param = new Param('bar');
205
        $param->type = new Name('Baz');
206
        $node->params = [$param];
207
208
        $this->visitor->enterNode($node);
209
210
        $symbols = $this->visitor->getCollectedSymbols();
211
        $this->assertCount(1, $symbols);
212
        $this->assertContains('Baz', $symbols);
213
    }
214
215 View Code Duplication
    public function testMethodParameterTypeAsString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
216
    {
217
        $functionName = new Name('foo');
218
        $node = new ClassMethod($functionName);
219
        $node->name = $functionName;
0 ignored issues
show
Documentation Bug introduced by
It seems like $functionName of type object<PhpParser\Node\Name> is incompatible with the declared type string of property $name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
220
        $param = new Param('bar');
221
        $param->type = 'Baz';
222
        $node->params = [$param];
223
224
        $this->visitor->enterNode($node);
225
226
        $symbols = $this->visitor->getCollectedSymbols();
227
        $this->assertCount(1, $symbols);
228
        $this->assertContains('Baz', $symbols);
229
    }
230
231 View Code Duplication
    public function testFunctionReturnType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
232
    {
233
        $functionName = new Name('foo');
234
        $node = new Function_($functionName);
235
        $node->returnType = new Name('Bar');
236
237
        $this->visitor->enterNode($node);
238
239
        $symbols = $this->visitor->getCollectedSymbols();
240
        $this->assertCount(1, $symbols);
241
        $this->assertContains('Bar', $symbols);
242
    }
243
244 View Code Duplication
    public function testFunctionReturnTypeAsString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
245
    {
246
        $functionName = new Name('foo');
247
        $node = new Function_($functionName);
248
        $node->returnType = 'Bar';
249
250
        $this->visitor->enterNode($node);
251
252
        $symbols = $this->visitor->getCollectedSymbols();
253
        $this->assertCount(1, $symbols);
254
        $this->assertContains('Bar', $symbols);
255
    }
256
257 View Code Duplication
    public function testMethodReturnType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
258
    {
259
        $functionName = new Name('foo');
260
        $node = new ClassMethod($functionName);
261
        $node->returnType = new Name('Bar');
262
263
        $this->visitor->enterNode($node);
264
265
        $symbols = $this->visitor->getCollectedSymbols();
266
        $this->assertCount(1, $symbols);
267
        $this->assertContains('Bar', $symbols);
268
    }
269
270 View Code Duplication
    public function testMethodReturnTypeAsString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
271
    {
272
        $functionName = new Name('foo');
273
        $node = new ClassMethod($functionName);
274
        $node->returnType = 'Bar';
275
276
        $this->visitor->enterNode($node);
277
278
        $symbols = $this->visitor->getCollectedSymbols();
279
        $this->assertCount(1, $symbols);
280
        $this->assertContains('Bar', $symbols);
281
    }
282
283 View Code Duplication
    public function testConstantFetch()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
284
    {
285
        $exceptionClass = new Name('FooException');
286
        $node = new ConstFetch($exceptionClass);
287
        $node->name = $exceptionClass;
288
289
        $this->visitor->enterNode($node);
290
291
        $symbols = $this->visitor->getCollectedSymbols();
292
        $this->assertCount(1, $symbols);
293
        $this->assertContains('FooException', $symbols);
294
    }
295
296 View Code Duplication
    public function testTraits()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
297
    {
298
        $node = new TraitUse([new Name('Foo')]);
299
300
        $this->visitor->enterNode($node);
301
302
        $symbols = $this->visitor->getCollectedSymbols();
303
        $this->assertCount(1, $symbols);
304
        $this->assertContains('Foo', $symbols);
305
    }
306
307 View Code Duplication
    public function testBeforeTraverseResetsRecordedSymbols()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
308
    {
309
        $node = new Class_('Foo');
310
        $node->extends = new Name('Bar');
311
        $this->visitor->enterNode($node);
312
313
        $this->visitor->beforeTraverse([]);
314
315
        $this->assertCount(0, $this->visitor->getCollectedSymbols());
316
    }
317
}
318