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

KanDefectVisitorTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 41
loc 41
rs 10
c 0
b 0
f 0
wmc 2
lcom 1
cbo 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testLackOfCohesionOfMethodsIsWellCalculated() 23 23 1
A provideExamples() 6 6 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
namespace Test\Hal\Metric\Class_\Coupling;
3
4
use Hal\Metric\Class_\ClassEnumVisitor;
5
use Hal\Metric\Class_\Complexity\KanDefectVisitor;
6
use Hal\Metric\Metrics;
7
use PhpParser\ParserFactory;
8
9
/**
10
 * @group metric
11
 * @group kan
12
 * @group defect
13
 */
14 View Code Duplication
class KanDefectVisitorTest 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 $example
20
     * @param string $classname
21
     * @param float $expected
22
     */
23
    public function testLackOfCohesionOfMethodsIsWellCalculated($example, $classname, $expected)
24
    {
25
        $metrics = new Metrics();
26
27
        $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
28
        $traverser = new \PhpParser\NodeTraverser();
29
        $traverser->addVisitor(new \PhpParser\NodeVisitor\NameResolver());
30
        $traverser->addVisitor(new ClassEnumVisitor($metrics));
31
        $traverser->addVisitor(new KanDefectVisitor($metrics));
32
33
        $code = file_get_contents($example);
34
        $this->assertNotFalse($code);
35
36
        $stmts = $parser->parse($code);
37
        $this->assertNotNull($stmts);
38
39
        $traverser->traverse($stmts);
0 ignored issues
show
Bug introduced by
It seems like $stmts defined by $parser->parse($code) on line 36 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...
40
41
        $metric =  $metrics->get($classname);
42
        $this->assertNotNull($metric);
43
44
        $this->assertSame($expected, $metric->get('kanDefect'));
45
    }
46
47
    /** @return mixed[]*/
0 ignored issues
show
Documentation introduced by
The doc-type mixed[]*/ could not be parsed: Expected "|" or "end of type", but got "*/" at position 7. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
48
    public function provideExamples()
49
    {
50
        return [
51
            [ __DIR__ . '/../../examples/kan1.php', 'A', .89],
52
        ];
53
    }
54
}
55