Completed
Push — master ( b60ce0...53cbf4 )
by Matthias
02:13
created

DefinedSymbolCollectorFunctionalTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 7
dl 9
loc 99
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 9 9 1
A testWillCollectSymbolsDefinedInThisFile() 0 9 1
A testWillCollectFunctionDefinition() 0 9 1
A testWillCollectNamespacedFunctionDefinition() 0 9 1
A testWillCollectConstDefinition() 0 9 1
A testWillCollectNamespacedConstDefinition() 0 9 1
A traverseStringAST() 0 4 1
A traverseClassAST() 0 8 1
A assertSameCollectedSymbols() 0 4 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\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
use PHPUnit\Framework\TestCase;
13
14
/**
15
 * @coversNothing
16
 *
17
 * @group functional
18
 */
19
final class DefinedSymbolCollectorFunctionalTest extends TestCase
20
{
21
    /**
22
     * @var DefinedSymbolCollector
23
     */
24
    private $collector;
25
26
    /**
27
     * @var Parser
28
     */
29
    private $parser;
30
31
    /**
32
     * @var NodeTraverserInterface
33
     */
34
    private $traverser;
35
36
    /**
37
     * {@inheritDoc}
38
     */
39 View Code Duplication
    protected function setUp()
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...
40
    {
41
        $this->collector = new DefinedSymbolCollector();
42
        $this->parser    = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
43
        $this->traverser = new NodeTraverser();
44
45
        $this->traverser->addVisitor(new NameResolver());
46
        $this->traverser->addVisitor($this->collector);
47
    }
48
49
    public function testWillCollectSymbolsDefinedInThisFile()
50
    {
51
        $this->traverseClassAST(self::class);
52
53
        self::assertSameCollectedSymbols(
54
            ['ComposerRequireCheckerTest\NodeVisitor\DefinedSymbolCollectorFunctionalTest'],
55
            $this->collector->getDefinedSymbols()
56
        );
57
    }
58
59
    public function testWillCollectFunctionDefinition()
60
    {
61
        $this->traverseStringAST('function foo() {}');
62
63
        self::assertSameCollectedSymbols(
64
            ['foo'],
65
            $this->collector->getDefinedSymbols()
66
        );
67
    }
68
69
    public function testWillCollectNamespacedFunctionDefinition()
70
    {
71
        $this->traverseStringAST('namespace Foo; function foo() {}');
72
73
        self::assertSameCollectedSymbols(
74
            ['Foo\foo'],
75
            $this->collector->getDefinedSymbols()
76
        );
77
    }
78
79
    public function testWillCollectConstDefinition()
80
    {
81
        $this->traverseStringAST('const foo = "bar", baz = "tab";');
82
83
        self::assertSameCollectedSymbols(
84
            ['foo', 'baz'],
85
            $this->collector->getDefinedSymbols()
86
        );
87
    }
88
89
    public function testWillCollectNamespacedConstDefinition()
90
    {
91
        $this->traverseStringAST('namespace Foo; const foo = "bar", baz = "tab";');
92
93
        self::assertSameCollectedSymbols(
94
            ['Foo\foo', 'Foo\baz'],
95
            $this->collector->getDefinedSymbols()
96
        );
97
    }
98
99
    private function traverseStringAST(string $phpSource) : array
100
    {
101
        return $this->traverser->traverse($this->parser->parse('<?php ' . $phpSource));
0 ignored issues
show
Bug introduced by
It seems like $this->parser->parse('<?php ' . $phpSource) targeting PhpParser\Parser::parse() can also be of type null; however, PhpParser\NodeTraverserInterface::traverse() does only seem to accept array<integer,object<PhpParser\Node>>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
102
    }
103
104
    private function traverseClassAST(string $className) : array
105
    {
106
        return $this->traverser->traverse(
107
            $this->parser->parse(
0 ignored issues
show
Bug introduced by
It seems like $this->parser->parse(fil...Name))->getFileName())) targeting PhpParser\Parser::parse() can also be of type null; however, PhpParser\NodeTraverserInterface::traverse() does only seem to accept array<integer,object<PhpParser\Node>>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
108
                file_get_contents((new \ReflectionClass($className))->getFileName())
109
            )
110
        );
111
    }
112
113
    private static function assertSameCollectedSymbols(array $expected, array $actual)
114
    {
115
        self::assertSame(array_diff($expected, $actual), array_diff($actual, $expected));
116
    }
117
}
118