Skip to content

Commit 14a1356

Browse files
committed
[PHP7] cyclomatic complexity and coupling
1 parent 3fcf995 commit 14a1356

File tree

7 files changed

+86
-2
lines changed

7 files changed

+86
-2
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ provision.sh
88
Makefile
99
/nbproject
1010
phpmetrics.yml.dist
11-
log
11+
log

src/Hal/Component/OOP/Reflected/ReflectedMethod.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,16 @@ public function getDependencies()
273273
foreach($this->dependencies as $name) {
274274
array_push($dependencies, $this->nameResolver->resolve($name, null));
275275
}
276+
277+
// returned values
278+
$resolver = new TypeResolver();
279+
foreach($this->returns as $return) {
280+
$name = $return->getType();
281+
if(!$resolver->isNative($name)) {
282+
array_push($dependencies, $this->nameResolver->resolve($name, null));
283+
}
284+
}
285+
276286
return array_unique($dependencies);
277287
}
278288

tests/Hal/Component/OOP/MethodExtractorTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ public function providesForMethodsArgs() {
110110

111111
/**
112112
* @dataProvider provideCodeForReturns
113-
* @group wip
114113
*/
115114
public function testReturnsAreFound($expected, $code) {
116115
$searcher = new Searcher();

tests/Hal/Metrics/Complexity/Component/McCabe/McCabeTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,21 @@ public function testMcCaybeResultCanBeConvertedToArray() {
3535

3636
$this->assertArrayHasKey('cyclomaticComplexity', $array);
3737
}
38+
39+
/**
40+
* @dataProvider provideCCNPHP7
41+
* @group php7
42+
*/
43+
public function testICanCountComplexityOfAnonymousClass($filename, $expectedCCN) {
44+
45+
$loc = new McCabe(new \Hal\Component\Token\Tokenizer());
46+
$r = $loc->calculate($filename);
47+
$this->assertEquals($expectedCCN, $r->getCyclomaticComplexityNumber());
48+
}
49+
50+
public function provideCCNPHP7() {
51+
return array(
52+
array(__DIR__.'/../../../../../resources/mccaybe/php7-1.php', 4)
53+
);
54+
}
3855
}

tests/Hal/Metrics/Complexity/Structural/HenryAndKafura/CouplingTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22
namespace Test\Hal\Metrics\Complexity\Structural\HenryAndKafura;
3+
use Hal\Component\OOP\Extractor\ClassMap;
4+
use Hal\Component\OOP\Extractor\Extractor;
5+
use Hal\Component\Token\Tokenizer;
36
use Hal\Metrics\Complexity\Structural\HenryAndKafura\Coupling;
47

58
/**
@@ -57,4 +60,18 @@ public function testICanExportCoupling() {
5760
);
5861
$this->assertEquals($expected, $this->result->get('\Ns1\Class1')->asArray());
5962
}
63+
64+
public function testCouplingTakesCareOfReturnedValues() {
65+
$filename = __DIR__.'/../../../../../resources/coupling/f1.php';
66+
$extractor = new Extractor(new Tokenizer());
67+
$classmap = new ClassMap();
68+
$classmap->push($filename, $extractor->extract($filename));
69+
$coupling = new Coupling();
70+
$this->result = $coupling->calculate($classmap);
71+
72+
$this->assertEquals(0, $this->result->get('\\ClassWhoUseAnother')->getAfferentCoupling());
73+
$this->assertEquals(1, $this->result->get('\\ClassWhoUseAnother')->getEfferentCoupling());
74+
$this->assertEquals(1, $this->result->get('\\ClassWhoIsUsed')->getAfferentCoupling());
75+
$this->assertEquals(0, $this->result->get('\\ClassWhoIsUsed')->getEfferentCoupling());
76+
}
6077
}

tests/resources/coupling/f1.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
class ClassWhoUseAnother {
3+
public function demo()
4+
{
5+
return new ClassWhoIsUsed;
6+
}
7+
}

tests/resources/mccaybe/php7-1.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
class A {
3+
public function foo() {
4+
5+
$a = new class {
6+
public function bar () {
7+
if( c1() )
8+
f1();
9+
else
10+
f2();
11+
12+
if( c2() )
13+
f3();
14+
else
15+
f4();
16+
}
17+
}
18+
19+
$b = new class {
20+
public function bar () {
21+
if( c1() )
22+
f1();
23+
else
24+
f2();
25+
26+
if( c2() )
27+
f3();
28+
else
29+
f4();
30+
}
31+
}
32+
33+
}
34+
}

0 commit comments

Comments
 (0)