Skip to content

Commit

Permalink
- Fixed #176: Calculation of CIS metric is incorrect.
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelpichler committed Nov 23, 2010
1 parent 72f3ea7 commit 1193f4a
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 58 deletions.
4 changes: 2 additions & 2 deletions PHP/Depend/Metrics/ClassLevel/Analyzer.php
Expand Up @@ -259,14 +259,14 @@ public function visitMethod(PHP_Depend_Code_Method $method)
// Increment Weighted Methods Per Class(WMC) value
$this->_nodeMetrics[$uuid][self::M_WEIGHTED_METHODS] += $ccn;
// Increment Class Size(CSZ) value
$this->_nodeMetrics[$uuid][self::M_CLASS_SIZE] += $ccn;
++$this->_nodeMetrics[$uuid][self::M_CLASS_SIZE];

// Increment Non Private values
if ($method->isPublic()) {
// Increment Non Private WMC value
$this->_nodeMetrics[$uuid][self::M_WEIGHTED_METHODS_NON_PRIVATE] += $ccn;
// Increment Class Interface Size(CIS) value
$this->_nodeMetrics[$uuid][self::M_CLASS_INTERFACE_SIZE] += $ccn;
++$this->_nodeMetrics[$uuid][self::M_CLASS_INTERFACE_SIZE];
}

$this->fireEndMethod($method);
Expand Down
2 changes: 2 additions & 0 deletions tests/PHP/Depend/Bugs/AllTests.php
Expand Up @@ -88,6 +88,7 @@
require_once dirname(__FILE__) . '/StringWithDollarStringLiteralBug162Test.php';
require_once dirname(__FILE__) . '/AlternativeSyntaxClosingTagBug163Test.php';
require_once dirname(__FILE__) . '/ClassAndInterfaceNamesBug169Test.php';
require_once dirname(__FILE__) . '/ClassInterfaceSizeShouldNotSumComplexityBug176Test.php';
require_once dirname(__FILE__) . '/UnexpectedTokenAsciiChar39Bug181Test.php';
require_once dirname(__FILE__) . '/CloneIsValidNameInOlderPhpVersionsBug182Test.php';

Expand Down Expand Up @@ -159,6 +160,7 @@ public static function suite()
$suite->addTestSuite('PHP_Depend_Bugs_StringWithDollarStringLiteralBug162Test');
$suite->addTestSuite('PHP_Depend_Bug_AlternativeSyntaxClosingTagBug163Test');
$suite->addTestSuite('PHP_Depend_Bugs_ClassAndInterfaceNamesBug169Test');
$suite->addTestSuite('PHP_Depend_Bugs_ClassInterfaceSizeShouldNotSumComplexityBug176Test');
$suite->addTestSuite('PHP_Depend_Bugs_UnexpectedTokenAsciiChar39Bug181Test');
$suite->addTestSuite('PHP_Depend_Bugs_CloneIsValidNameInOlderPhpVersionsBug182Test');

Expand Down
@@ -0,0 +1,117 @@
<?php
/**
* This file is part of PHP_Depend.
*
* PHP Version 5
*
* Copyright (c) 2008-2010, Manuel Pichler <mapi@pdepend.org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Manuel Pichler nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category PHP
* @package PHP_Depend
* @subpackage Bugs
* @author Manuel Pichler <mapi@pdepend.org>
* @copyright 2008-2010 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://tracker.pdepend.org/pdepend/issue_tracker/issue/176
*/

require_once dirname(__FILE__) . '/AbstractTest.php';

/**
* Test case for ticket #176.
*
* @category PHP
* @package PHP_Depend
* @subpackage Bugs
* @author Manuel Pichler <mapi@pdepend.org>
* @copyright 2008-2010 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version Release: @package_version@
* @link http://tracker.pdepend.org/pdepend/issue_tracker/issue/176
*
* @covers stdClass
*/
class PHP_Depend_Bugs_ClassInterfaceSizeShouldNotSumComplexityBug176Test
extends PHP_Depend_Bugs_AbstractTest
{
/**
* testAnalyzerCountsNumberOfMethodsForClassInterfaceSize
*
* @return void
* @group pdepend
* @group pdepend::bugs
* @group regressiontest
*/
public function testAnalyzerCountsNumberOfMethodsForClassInterfaceSize()
{
$packages = self::parseCodeResourceForTest();

$class = $packages->current()
->getClasses()
->current();

$analyzer = new PHP_Depend_Metrics_ClassLevel_Analyzer();
$analyzer->addAnalyzer(new PHP_Depend_Metrics_CyclomaticComplexity_Analyzer());
$analyzer->analyze($packages);

$metrics = $analyzer->getNodeMetrics($class);

self::assertEquals(2, $metrics['cis']);
}

/**
* testAnalyzerCountsNumberOfMethodsForClassSize
*
* @return void
* @group pdepend
* @group pdepend::bugs
* @group regressiontest
*/
public function testAnalyzerCountsNumberOfMethodsForClassSize()
{
$packages = self::parseCodeResourceForTest();

$class = $packages->current()
->getClasses()
->current();

$analyzer = new PHP_Depend_Metrics_ClassLevel_Analyzer();
$analyzer->addAnalyzer(new PHP_Depend_Metrics_CyclomaticComplexity_Analyzer());
$analyzer->analyze($packages);

$metrics = $analyzer->getNodeMetrics($class);

self::assertEquals(6, $metrics['csz']);
}
}
4 changes: 2 additions & 2 deletions tests/PHP/Depend/Log/Phpunit/_expected/phpunit-log.xml
@@ -1,7 +1,7 @@
<?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="31" 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" cis="8" cloc="46" cr="0.15" csz="11" 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" 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>
Expand All @@ -10,7 +10,7 @@
</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" cis="7" cloc="26" cr="0.15" csz="7" 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" 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>
Expand Down

0 comments on commit 1193f4a

Please sign in to comment.