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

KanDefectVisitorTest::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_\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