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

testLackOfCohesionOfMethodsIsWellCalculated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 7
dl 0
loc 42
rs 9.248
c 0
b 0
f 0
1
<?php
2
namespace Test\Hal\Metric\Class_\Structural;
3
4
use Hal\Metric\Class_\Component\MaintainabilityIndexVisitor;
5
use Hal\Metric\Metrics;
6
use PhpParser\ParserFactory;
7
8
/**
9
 * @group metric
10
 * @group mi
11
 * @group complex
12
 */
13
class MaintainabilityIndexVisitorTest extends \PHPUnit\Framework\TestCase
14
{
15
    /**
16
     * @dataProvider provideValues
17
     *
18
     * @param int $ccn
19
     * @param int $lloc
20
     * @param int $cloc
21
     * @param float $volume
22
     * @param float $mIwoC
23
     * @param float $mi
24
     * @param float $weight
25
     */
26
    public function testLackOfCohesionOfMethodsIsWellCalculated($ccn, $lloc, $cloc, $volume, $mIwoC, $mi, $weight)
27
    {
28
        $metrics = new Metrics();
29
        $prophet = $this->prophesize('Hal\Metric\ClassMetric');
30
        $prophet->getName()->willReturn('A');
31
        $prophet->get('lloc')->willReturn($lloc);
32
        $prophet->get('loc')->willReturn($lloc + $cloc);
33
        $prophet->get('ccn')->willReturn($ccn);
34
        $prophet->get('cloc')->willReturn($cloc);
35
        $prophet->get('volume')->willReturn($volume);
36
37
        // spy
38
        $prophet->set('mIwoC', $mIwoC)->will(function () use ($prophet) {
39
            return $prophet->reveal();
40
        })->shouldBeCalled();
41
        $prophet->set('mi', $mi)->will(function () use ($prophet) {
42
            return $prophet->reveal();
43
        })->shouldBeCalled();
44
        $prophet->set('commentWeight', $weight)->will(function () use ($prophet) {
45
            return $prophet->reveal();
46
        })->shouldBeCalled();
47
48
        $class = $prophet->reveal();
49
        $metrics->attach($class);
50
51
        $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
52
        $traverser = new \PhpParser\NodeTraverser();
53
        $traverser->addVisitor(new \PhpParser\NodeVisitor\NameResolver());
54
        $traverser->addVisitor(new MaintainabilityIndexVisitor($metrics));
55
56
        $code = <<<EOT
57
<?php class A {
58
    public function foo() {
59
60
    }
61
}
62
EOT;
63
        $stmts = $parser->parse($code);
64
        $this->assertNotNull($stmts);
65
66
        $traverser->traverse($stmts);
0 ignored issues
show
Bug introduced by
It seems like $stmts defined by $parser->parse($code) on line 63 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...
67
    }
68
69
    /** @return mixed[] */
70
    public function provideValues()
71
    {
72
        return [
73
            //    CC    LLOC    CLOC        Volume      MIwoC      mi          commentWeight
74
            [5     , 50    , 20       , 10         , 55.26,   92.1,      36.83 ],
75
            [11    , 45   , 26       , 1777.49    , 39.7,     80.01,      40.3 ]
76
        ];
77
    }
78
}
79