Skip to content

Commit

Permalink
[implements #9069871] Implement efferent- and afferent-coupling for c…
Browse files Browse the repository at this point in the history
…lasses.
  • Loading branch information
manuelpichler committed Feb 16, 2011
1 parent f710b7e commit 07537c2
Show file tree
Hide file tree
Showing 14 changed files with 222 additions and 11 deletions.
36 changes: 36 additions & 0 deletions src/main/php/PHP/Depend/Metrics/Coupling/Analyzer.php
Expand Up @@ -253,6 +253,7 @@ private function _postProcessTemporaryCouplingMap()

$this->_fanout += $efferentCoupling;
}

$this->_temporaryCouplingMap = array();
}

Expand Down Expand Up @@ -280,21 +281,56 @@ public function visitFunction(PHP_Depend_Code_Function $function)
}
foreach ($function->getDependencies() as $type) {
if (in_array($type, $fanouts, true) === false) {
$fanouts[] = $type;
++$this->_fanout;
}
}

foreach ($fanouts as $fanout) {
$this->_initClassOrInterfaceDependencyMap($fanout);

$this->_temporaryCouplingMap[
$fanout->getUUID()
]['ca'][
$function->getUUID()
] = true;
}

$this->_countCalls($function);

$this->fireEndFunction($function);
}

/**
* Visit method for classes that will be called by PHP_Depend during the
* analysis phase with the current context class.
*
* @param PHP_Depend_Code_Class $class The currently analyzed class.
*
* @return void
* @since 0.10.2
*/
public function visitClass(PHP_Depend_Code_Class $class)
{
$this->_initClassOrInterfaceDependencyMap($class);
return parent::visitClass($class);
}

/**
* Visit method for interfaces that will be called by PHP_Depend during the
* analysis phase with the current context interface.
*
* @param PHP_Depend_Code_Interface $interface The currently analyzed interface.
*
* @return void
* @since 0.10.2
*/
public function visitInterface(PHP_Depend_Code_Interface $interface)
{
$this->_initClassOrInterfaceDependencyMap($interface);
return parent::visitInterface($interface);
}

/**
* Visits a method node.
*
Expand Down
10 changes: 5 additions & 5 deletions src/test/php/PHP/Depend/Log/Phpunit/_expected/phpunit-log.xml
@@ -1,20 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<metrics ahh="0" andc="0" calls="30" ccn="21" classes="3" cloc="217" clsa="0" clsc="3" fanout="26" files="4" functions="2" interfs="2" leafs="3" lloc="57" loc="336" locExecutable="101" maxdit="0" ncloc="119" nom="6" nop="1" roots="0">
<metrics ahh="0" andc="0" calls="30" ccn="21" classes="3" cloc="217" clsa="0" clsc="3" fanout="29" files="4" functions="2" interfs="2" leafs="3" lloc="57" loc="336" locExecutable="101" maxdit="0" ncloc="119" nom="6" nop="1" roots="0">
<file name="/home/manu/Desktop/Projects/workspace.xplib.de/PHP_Depend/trunk/tests/PHP/Depend/_code/log/phpunit/testPHPUnitLoggerResult/class.php" classes="2" cloc="84" functions="0" lloc="21" loc="128" locExecutable="36" ncloc="44">
<class name="MyCouplingClass" ca="0" cbo="7" ce="7" cis="3" cloc="46" cr="0.15" csz="6" dit="0" impl="0" lloc="13" loc="82" locExecutable="22" ncloc="36" noam="0" nocc="0" nom="2" noom="0" rcr="0.15" vars="4" varsi="4" varsnp="1" wmc="7" wmci="7" wmcnp="7">
<class name="MyCouplingClass" ca="0" cbo="8" ce="8" cis="3" cloc="46" cr="0.15" csz="6" dit="0" impl="0" lloc="13" loc="82" locExecutable="22" ncloc="36" noam="0" nocc="0" nom="2" noom="0" rcr="0.15" vars="4" varsi="4" varsnp="1" wmc="7" wmci="7" wmcnp="7">
<method name="removeItemAt" ccn="3" cloc="0" lloc="5" loc="10" locExecutable="9" ncloc="10"/>
<method name="getItemAt" ccn="4" cloc="0" lloc="8" loc="14" locExecutable="13" ncloc="14"/>
</class>
<class name="MyCouplingInterface" ca="0" cbo="2" ce="2" cloc="13" cr="0.15" lloc="0" loc="17" locExecutable="0" ncloc="4" nom="1" rcr="0.15">
<class name="MyCouplingInterface" ca="2" cbo="2" ce="2" cloc="13" cr="0.15" lloc="0" loc="17" locExecutable="0" ncloc="4" nom="1" rcr="0.15">
<method name="removeItemAt" cloc="0" lloc="0" loc="1" locExecutable="0" ncloc="1"/>
</class>
</file>
<file name="/home/manu/Desktop/Projects/workspace.xplib.de/PHP_Depend/trunk/tests/PHP/Depend/_code/log/phpunit/testPHPUnitLoggerResult/method.php" classes="2" cloc="64" functions="0" lloc="17" loc="100" locExecutable="32" ncloc="36">
<class name="MyMethodCouplingClass" ca="0" cbo="5" ce="5" cis="2" cloc="26" cr="0.15" csz="2" dit="0" impl="0" lloc="13" loc="54" locExecutable="22" ncloc="28" noam="0" nocc="0" nom="2" noom="0" rcr="0.15" vars="0" varsi="0" varsnp="0" wmc="7" wmci="7" wmcnp="7">
<class name="MyMethodCouplingClass" ca="0" cbo="6" ce="6" cis="2" cloc="26" cr="0.15" csz="2" dit="0" impl="0" lloc="13" loc="54" locExecutable="22" ncloc="28" noam="0" nocc="0" nom="2" noom="0" rcr="0.15" vars="0" varsi="0" varsnp="0" wmc="7" wmci="7" wmcnp="7">
<method name="getItemAt" ccn="4" cloc="0" lloc="8" loc="14" locExecutable="13" ncloc="14"/>
<method name="removeItemAt" ccn="3" cloc="0" lloc="5" loc="10" locExecutable="9" ncloc="10"/>
</class>
<class name="MyMethodCouplingInterface" ca="0" cbo="2" ce="2" cloc="13" cr="0.15" lloc="0" loc="17" locExecutable="0" ncloc="4" nom="1" rcr="0.15">
<class name="MyMethodCouplingInterface" ca="1" cbo="2" ce="2" cloc="13" cr="0.15" lloc="0" loc="17" locExecutable="0" ncloc="4" nom="1" rcr="0.15">
<method name="removeItemAt" cloc="0" lloc="0" loc="1" locExecutable="0" ncloc="1"/>
</class>
</file>
Expand Down
60 changes: 60 additions & 0 deletions src/test/php/PHP/Depend/Metrics/Coupling/AnalyzerTest.php
Expand Up @@ -203,6 +203,66 @@ public function testGetNodeMetricsReturnsExpectedCaForChildTypeReference()
self::assertEquals(2, $this->_getNodeMetricForType('ca'));
}

/**
* testGetNodeMetricsReturnsExpectedCaForFunctionReference
*
* @return void
*/
public function testGetNodeMetricsReturnsExpectedCaForFunctionReference()
{
self::assertEquals(1, $this->_getNodeMetricForType('ca'));
}

/**
* testGetNodeMetricsReturnsExpectedCaForFunctionException
*
* @return void
*/
public function testGetNodeMetricsReturnsExpectedCaForFunctionException()
{
self::assertEquals(1, $this->_getNodeMetricForType('ca'));
}

/**
* testGetNodeMetricsReturnsExpectedCaForFunctionReturnType
*
* @return void
*/
public function testGetNodeMetricsReturnsExpectedCaForFunctionReturnType()
{
self::assertEquals(1, $this->_getNodeMetricForType('ca'));
}

/**
* testGetNodeMetricsReturnsExpectedCaForFunctionParameter
*
* @return void
*/
public function testGetNodeMetricsReturnsExpectedCaForFunctionParameter()
{
self::assertEquals(1, $this->_getNodeMetricForType('ca'));
}

/**
* testGetNodeMetricsReturnsExpectedCaForFunctions
*
* @return void
*/
public function testGetNodeMetricsReturnsExpectedCaForFunctions()
{
self::assertEquals(3, $this->_getNodeMetricForType('ca'));
}

/**
* testGetNodeMetricsReturnsExpectedCaForFunctionCountsTypeOnce
*
* @return void
*/
public function testGetNodeMetricsReturnsExpectedCaForFunctionCountsTypeOnce()
{
self::assertEquals(2, $this->_getNodeMetricForType('ca'));
}

/**
* testGetNodeMetricsReturnsExpectedCboWithoutDependencies
*
Expand Down
Expand Up @@ -106,7 +106,7 @@ public function removeItemAt(ArrayObject $items, $index)
* @param ArrayAccess $items The input items.
* @param integer $index The requested index.
*
* @return MyObjectItem
* @return MyCouplingInterface
* @throws OutOfRangeException For invalid index values.
* @throws InvalidArgumentException For invalid index values.
*/
Expand Down
Expand Up @@ -16,7 +16,7 @@
* @param ArrayAccess $items The input items.
* @param integer $index The requested index.
*
* @return MyObjectItem
* @return MyCouplingInterface
* @throws OutOfRangeException For invalid index values.
* @throws InvalidArgumentException For invalid index values.
*/
Expand Down
Expand Up @@ -27,7 +27,7 @@ class MyMethodCouplingClass
* @param ArrayAccess $items The input items.
* @param integer $index The requested index.
*
* @return OutOfRangeException
* @return MyMethodCouplingInterface
* @throws OutOfRangeException For invalid index values.
* @throws InvalidArgumentException For invalid index values.
*/
Expand Down
@@ -1,5 +1,5 @@
<?php
class testGetNodeMetricsReturnsExpectedCboForChildTypeReference_factory
class testGetNodeMetricsReturnsExpectedCaForChildTypeReference_factory
{
public static function create($type)
{
Expand All @@ -24,6 +24,22 @@ class testGetNodeMetricsReturnsExpectedCaForChildTypeReference_foo
{
public function setFactory(testGetNodeMetricsReturnsExpectedCaForChildTypeReference_factory $factory)
{
return new testGetNodeMetricsReturnsExpectedCaForChildTypeReference_factory();

}
}

class testGetNodeMetricsReturnsExpectedCaForChildTypeReference_bar
{
public function setFactory(testGetNodeMetricsReturnsExpectedCaForChildTypeReference_factory $factory)
{

}
}

class testGetNodeMetricsReturnsExpectedCaForChildTypeReference_baz
{
public function setFactory(testGetNodeMetricsReturnsExpectedCaForChildTypeReference_factory $factory)
{

}
}
@@ -0,0 +1,29 @@
<?php
class testGetNodeMetricsReturnsExpectedCaForCountsSameTypeOnlyOnce extends Exception
{

}

/**
* @throws testGetNodeMetricsReturnsExpectedCaForCountsSameTypeOnlyOnce
* @param testGetNodeMetricsReturnsExpectedCaForCountsSameTypeOnlyOnce $o
* @return testGetNodeMetricsReturnsExpectedCaForCountsSameTypeOnlyOnce
*/
function foo(testGetNodeMetricsReturnsExpectedCaForCountsSameTypeOnlyOnce $o)
{
if ($o instanceof testGetNodeMetricsReturnsExpectedCaForCountsSameTypeOnlyOnce) {
throw new testGetNodeMetricsReturnsExpectedCaForCountsSameTypeOnlyOnce();
}
return new testGetNodeMetricsReturnsExpectedCaForCountsSameTypeOnlyOnce();
}

/**
* @return void
* @throws testGetNodeMetricsReturnsExpectedCaForCountsSameTypeOnlyOnce
*/
function bar(testGetNodeMetricsReturnsExpectedCaForCountsSameTypeOnlyOnce $o)
{
if ($o instanceof testGetNodeMetricsReturnsExpectedCaForCountsSameTypeOnlyOnce) {
throw new testGetNodeMetricsReturnsExpectedCaForCountsSameTypeOnlyOnce();
}
}
@@ -0,0 +1,14 @@
<?php
/**
* @return void
* @throws testGetNodeMetricsReturnsExpectedCaForFunctionException
*/
function foo()
{

}

class testGetNodeMetricsReturnsExpectedCaForFunctionException extends Exception
{

}
@@ -0,0 +1,10 @@
<?php
class testGetNodeMetricsReturnsExpectedCaForFunctionParameter
{

}

function foo(testGetNodeMetricsReturnsExpectedCaForFunctionParameter $o)
{

}
@@ -0,0 +1,10 @@
<?php
class testGetNodeMetricsReturnsExpectedCaForFunctionReference
{

}

function foo()
{
return new testGetNodeMetricsReturnsExpectedCaForFunctionReference();
}
@@ -0,0 +1,13 @@
<?php
/**
* @return testGetNodeMetricsReturnsExpectedCaForFunctionReturnType
*/
function foo()
{

}

class testGetNodeMetricsReturnsExpectedCaForFunctionReturnType
{

}
@@ -0,0 +1,23 @@
<?php
class testGetNodeMetricsReturnsExpectedCaForFunctions
{

}

function foo(testGetNodeMetricsReturnsExpectedCaForFunctions $o)
{

}

function bar()
{
return new testGetNodeMetricsReturnsExpectedCaForFunctions();
}

/**
* @return testGetNodeMetricsReturnsExpectedCaForFunctions
*/
function baz()
{

}
@@ -1,5 +1,5 @@
<?php
class testGetNodeMetricsReturnsExpectedCboWithObjectInstantiation
class testGetNodeMetricsReturnsExpectedCaWithObjectInstantiation
{

}
Expand Down

0 comments on commit 07537c2

Please sign in to comment.