Completed
Pull Request — master (#448)
by
unknown
08:31
created

SystemComplexityVisitorTest::provideExamples()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 6
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Test\Hal\Metric\Class_\Coupling;
3
4
use Hal\Metric\Class_\ClassEnumVisitor;
5
use Hal\Metric\Class_\Structural\SystemComplexityVisitor;
6
use Hal\Metric\Metrics;
7
use PhpParser\ParserFactory;
8
9
/**
10
 * @group metric
11
 * @group complexity
12
 * @group defect
13
 */
14 View Code Duplication
class SystemComplexityVisitorTest extends \PHPUnit\Framework\TestCase
0 ignored issues
show
Duplication introduced by
This class 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...
15
{
16
    /**
17
     * @dataProvider provideExamples
18
     *
19
     * @param string $filename
20
     * @param string $class
21
     * @param float $rdc
22
     * @param float $rsc
23
     * @param float $rsysc
24
     */
25
    public function testLackOfCohesionOfMethodsIsWellCalculated($filename, $class, $rdc, $rsc, $rsysc)
0 ignored issues
show
Unused Code introduced by
The parameter $class is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
26
    {
27
        $metrics = new Metrics();
28
29
        $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
30
        $traverser = new \PhpParser\NodeTraverser();
31
        $traverser->addVisitor(new \PhpParser\NodeVisitor\NameResolver());
32
        $traverser->addVisitor(new ClassEnumVisitor($metrics));
33
        $traverser->addVisitor(new SystemComplexityVisitor($metrics));
34
35
        $code = file_get_contents($filename);
36
        $this->assertNotFalse($code);
37
38
        $stmts = $parser->parse($code);
39
        $this->assertNotNull($stmts);
40
41
        $traverser->traverse($stmts);
0 ignored issues
show
Bug introduced by
It seems like $stmts defined by $parser->parse($code) on line 38 can also be of type null; however, PhpParser\NodeTraverser::traverse() does only seem to accept array<integer,object<PhpParser\Node>>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
42
43
        $metric = $metrics->get('A');
44
        $this->assertNotNull($metric);
45
46
        $this->assertSame($rdc, $metric->get('relativeDataComplexity'));
47
        $this->assertSame($rsc, $metric->get('relativeStructuralComplexity'));
48
        $this->assertSame($rsysc, $metric->get('relativeSystemComplexity'));
49
    }
50
51
    /** @return mixed[] */
52
    public function provideExamples()
53
    {
54
        return [
55
            [ __DIR__ . '/../../examples/systemcomplexity1.php', 'A', 2.5, 1.0, 3.5],
56
        ];
57
    }
58
}
59