|
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 |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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
|
|
|
|
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.