From 2dd3ebff4ecfce6627225ac9ca4f7def263e9fa4 Mon Sep 17 00:00:00 2001 From: Manuel Pichler Date: Wed, 16 Feb 2011 21:22:38 +0100 Subject: [PATCH] [implements #9997915] Implement Number of Public Methods metric --- .../Depend/Metrics/ClassLevel/Analyzer.php | 7 +- .../Log/Phpunit/_expected/phpunit-log.xml | 6 +- .../Metrics/ClassLevel/AnalyzerTest.php | 161 ++++++++++++++---- .../Depend/Metrics/Coupling/AnalyzerTest.php | 4 +- ...MetricForClassWithAllVisibilityMethods.php | 18 ++ ...lateNpmMetricForClassWithPrivateMethod.php | 8 + ...teNpmMetricForClassWithProtectedMethod.php | 8 + ...ulateNpmMetricForClassWithPublicMethod.php | 8 + ...lateNpmMetricForClassWithPublicMethods.php | 18 ++ ...pmMetricForClassWithPublicStaticMethod.php | 8 + .../testCalculateNpmMetricForEmptyClass.php | 5 + ...csReturnsArrayWithExpectedSetOfMetrics.php | 5 + ...sReturnsArrayWithExpectedSetOfMetrics.php} | 2 +- 13 files changed, 216 insertions(+), 42 deletions(-) create mode 100644 src/test/php/PHP/Depend/_code/metrics/ClassLevel/testCalculateNpmMetricForClassWithAllVisibilityMethods.php create mode 100644 src/test/php/PHP/Depend/_code/metrics/ClassLevel/testCalculateNpmMetricForClassWithPrivateMethod.php create mode 100644 src/test/php/PHP/Depend/_code/metrics/ClassLevel/testCalculateNpmMetricForClassWithProtectedMethod.php create mode 100644 src/test/php/PHP/Depend/_code/metrics/ClassLevel/testCalculateNpmMetricForClassWithPublicMethod.php create mode 100644 src/test/php/PHP/Depend/_code/metrics/ClassLevel/testCalculateNpmMetricForClassWithPublicMethods.php create mode 100644 src/test/php/PHP/Depend/_code/metrics/ClassLevel/testCalculateNpmMetricForClassWithPublicStaticMethod.php create mode 100644 src/test/php/PHP/Depend/_code/metrics/ClassLevel/testCalculateNpmMetricForEmptyClass.php create mode 100644 src/test/php/PHP/Depend/_code/metrics/ClassLevel/testGetNodeMetricsReturnsArrayWithExpectedSetOfMetrics.php rename src/test/php/PHP/Depend/_code/metrics/Coupling/{testGetNodeMetricsReturnsArrayWithExpectedKeys.php => testGetNodeMetricsReturnsArrayWithExpectedSetOfMetrics.php} (51%) diff --git a/src/main/php/PHP/Depend/Metrics/ClassLevel/Analyzer.php b/src/main/php/PHP/Depend/Metrics/ClassLevel/Analyzer.php index 80b1775a9a..267705687c 100644 --- a/src/main/php/PHP/Depend/Metrics/ClassLevel/Analyzer.php +++ b/src/main/php/PHP/Depend/Metrics/ClassLevel/Analyzer.php @@ -78,6 +78,7 @@ class PHP_Depend_Metrics_ClassLevel_Analyzer const M_IMPLEMENTED_INTERFACES = 'impl', M_CLASS_INTERFACE_SIZE = 'cis', M_CLASS_SIZE = 'csz', + M_NUMBER_OF_PUBLIC_METHODS = 'npm', M_PROPERTIES = 'vars', M_PROPERTIES_INHERIT = 'varsi', M_PROPERTIES_NON_PRIVATE = 'varsnp', @@ -103,14 +104,14 @@ class PHP_Depend_Metrics_ClassLevel_Analyzer * ) * * - * @var array(string=>array) $_nodeMetrics + * @var array(string=>array) */ private $_nodeMetrics = null; /** * The internal used cyclomatic complexity analyzer. * - * @var PHP_Depend_Metrics_CyclomaticComplexity_Analyzer $_cyclomaticAnalyzer + * @var PHP_Depend_Metrics_CyclomaticComplexity_Analyzer */ private $_cyclomaticAnalyzer = null; @@ -208,6 +209,7 @@ public function visitClass(PHP_Depend_Code_Class $class) self::M_IMPLEMENTED_INTERFACES => $class->getInterfaces()->count(), self::M_CLASS_INTERFACE_SIZE => 0, self::M_CLASS_SIZE => 0, + self::M_NUMBER_OF_PUBLIC_METHODS => 0, self::M_PROPERTIES => 0, self::M_PROPERTIES_INHERIT => $this->_calculateVARSi($class), self::M_PROPERTIES_NON_PRIVATE => 0, @@ -263,6 +265,7 @@ public function visitMethod(PHP_Depend_Code_Method $method) // Increment Non Private values if ($method->isPublic()) { + ++$this->_nodeMetrics[$uuid][self::M_NUMBER_OF_PUBLIC_METHODS]; // Increment Non Private WMC value $this->_nodeMetrics[$uuid][self::M_WEIGHTED_METHODS_NON_PRIVATE] += $ccn; // Increment Class Interface Size(CIS) value diff --git a/src/test/php/PHP/Depend/Log/Phpunit/_expected/phpunit-log.xml b/src/test/php/PHP/Depend/Log/Phpunit/_expected/phpunit-log.xml index 53ab923ed6..277abc7404 100644 --- a/src/test/php/PHP/Depend/Log/Phpunit/_expected/phpunit-log.xml +++ b/src/test/php/PHP/Depend/Log/Phpunit/_expected/phpunit-log.xml @@ -1,7 +1,7 @@ - + @@ -10,7 +10,7 @@ - + @@ -19,7 +19,7 @@ - + diff --git a/src/test/php/PHP/Depend/Metrics/ClassLevel/AnalyzerTest.php b/src/test/php/PHP/Depend/Metrics/ClassLevel/AnalyzerTest.php index 6bef4e5b84..d6cba76371 100644 --- a/src/test/php/PHP/Depend/Metrics/ClassLevel/AnalyzerTest.php +++ b/src/test/php/PHP/Depend/Metrics/ClassLevel/AnalyzerTest.php @@ -110,6 +110,19 @@ public function testGetRequiredAnalyzersReturnsExpectedClassNames() $analyzer->getRequiredAnalyzers() ); } + + /** + * testGetNodeMetricsReturnsArrayWithExpectedSetOfMetrics + * + * @return void + */ + public function testGetNodeMetricsReturnsArrayWithExpectedSetOfMetrics() + { + self::assertEquals( + array('impl', 'cis', 'csz', 'npm', 'vars', 'varsi', 'varsnp', 'wmc', 'wmci', 'wmcnp'), + array_keys($this->_calculateClassMetrics()) + ); + } /** * Tests that the analyzer calculates the correct IMPL values. @@ -118,7 +131,7 @@ public function testGetRequiredAnalyzersReturnsExpectedClassNames() */ public function testCalculateIMPLMetric() { - self::assertEquals(4, $this->_calculateMetric(__METHOD__, 'impl')); + self::assertEquals(4, $this->_calculateClassMetric('impl')); } /** @@ -128,7 +141,7 @@ public function testCalculateIMPLMetric() */ public function testCalculateIMPLMetric1() { - self::assertEquals(6, $this->_calculateMetric(__METHOD__, 'impl')); + self::assertEquals(6, $this->_calculateClassMetric('impl')); } /** @@ -138,7 +151,7 @@ public function testCalculateIMPLMetric1() */ public function testCalculateIMPLMetric2() { - self::assertEquals(2, $this->_calculateMetric(__METHOD__, 'impl')); + self::assertEquals(2, $this->_calculateClassMetric('impl')); } /** @@ -148,7 +161,7 @@ public function testCalculateIMPLMetric2() */ public function testCalculateIMPLMetricContainsUnknownImplementedInterface() { - self::assertEquals(1, $this->_calculateMetric(__METHOD__, 'impl')); + self::assertEquals(1, $this->_calculateClassMetric('impl')); } /** @@ -158,7 +171,7 @@ public function testCalculateIMPLMetricContainsUnknownImplementedInterface() */ public function testCalculateIMPLMetricContainsUnknownIndirectImplementedInterface() { - self::assertEquals(1, $this->_calculateMetric(__METHOD__, 'impl')); + self::assertEquals(1, $this->_calculateClassMetric('impl')); } /** @@ -168,7 +181,7 @@ public function testCalculateIMPLMetricContainsUnknownIndirectImplementedInterfa */ public function testCalculateIMPLMetricContainsInternalImplementedInterface() { - self::assertEquals(1, $this->_calculateMetric(__METHOD__, 'impl')); + self::assertEquals(1, $this->_calculateClassMetric('impl')); } /** @@ -178,7 +191,7 @@ public function testCalculateIMPLMetricContainsInternalImplementedInterface() */ public function testCalculateCISMetricZeroInheritance() { - self::assertEquals(2, $this->_calculateMetric(__METHOD__, 'cis')); + self::assertEquals(2, $this->_calculateClassMetric('cis')); } /** @@ -188,7 +201,7 @@ public function testCalculateCISMetricZeroInheritance() */ public function testCalculateCISMetricOneLevelInheritance() { - self::assertEquals(2, $this->_calculateMetric(__METHOD__, 'cis')); + self::assertEquals(2, $this->_calculateClassMetric('cis')); } /** @@ -198,7 +211,7 @@ public function testCalculateCISMetricOneLevelInheritance() */ public function testCalculateCISMetricTwoLevelInheritance() { - self::assertEquals(3, $this->_calculateMetric(__METHOD__, 'cis')); + self::assertEquals(3, $this->_calculateClassMetric('cis')); } /** @@ -208,7 +221,7 @@ public function testCalculateCISMetricTwoLevelInheritance() */ public function testCalculateCISMetricOnlyCountsMethodsAndNotSumsComplexity() { - self::assertEquals(2, $this->_calculateMetric(__METHOD__, 'cis')); + self::assertEquals(2, $this->_calculateClassMetric('cis')); } /** @@ -218,7 +231,7 @@ public function testCalculateCISMetricOnlyCountsMethodsAndNotSumsComplexity() */ public function testCalculateCSZMetricZeroInheritance() { - self::assertEquals(6, $this->_calculateMetric(__METHOD__, 'csz')); + self::assertEquals(6, $this->_calculateClassMetric('csz')); } /** @@ -228,7 +241,7 @@ public function testCalculateCSZMetricZeroInheritance() */ public function testCalculateCSZMetricOneLevelInheritance() { - self::assertEquals(4, $this->_calculateMetric(__METHOD__, 'csz')); + self::assertEquals(4, $this->_calculateClassMetric('csz')); } /** @@ -238,7 +251,77 @@ public function testCalculateCSZMetricOneLevelInheritance() */ public function testCalculateCSZMetricOnlyCountsMethodsAndNotSumsComplexity() { - self::assertEquals(2, $this->_calculateMetric(__METHOD__, 'csz')); + self::assertEquals(2, $this->_calculateClassMetric('csz')); + } + + /** + * testCalculateNpmMetricForEmptyClass + * + * @return void + */ + public function testCalculateNpmMetricForEmptyClass() + { + self::assertEquals(0, $this->_calculateClassMetric('npm')); + } + + /** + * testCalculateNpmMetricForClassWithPublicMethod + * + * @return void + */ + public function testCalculateNpmMetricForClassWithPublicMethod() + { + self::assertEquals(1, $this->_calculateClassMetric('npm')); + } + + /** + * testCalculateNpmMetricForClassWithPublicMethods + * + * @return void + */ + public function testCalculateNpmMetricForClassWithPublicMethods() + { + self::assertEquals(3, $this->_calculateClassMetric('npm')); + } + + /** + * testCalculateNpmMetricForClassWithPublicStaticMethod + * + * @return void + */ + public function testCalculateNpmMetricForClassWithPublicStaticMethod() + { + self::assertEquals(1, $this->_calculateClassMetric('npm')); + } + + /** + * testCalculateNpmMetricForClassWithProtectedMethod + * + * @return void + */ + public function testCalculateNpmMetricForClassWithProtectedMethod() + { + self::assertEquals(0, $this->_calculateClassMetric('npm')); + } + + /** + * testCalculateNpmMetricForClassWithPrivateMethod + * + * @return void + */ + public function testCalculateNpmMetricForClassWithPrivateMethod() + { + self::assertEquals(0, $this->_calculateClassMetric('npm')); + } + + /** + * testCalculateNpmMetricForClassWithAllVisibilityMethods + * + * @return void + */ + public function testCalculateNpmMetricForClassWithAllVisibilityMethods() + { + self::assertEquals(1, $this->_calculateClassMetric('npm')); } /** @@ -248,7 +331,7 @@ public function testCalculateCSZMetricOnlyCountsMethodsAndNotSumsComplexity() */ public function testCalculateVARSMetricZeroInheritance() { - self::assertEquals(1, $this->_calculateMetric(__METHOD__, 'vars')); + self::assertEquals(1, $this->_calculateClassMetric('vars')); } /** @@ -258,7 +341,7 @@ public function testCalculateVARSMetricZeroInheritance() */ public function testCalculateVARSMetricOneLevelInheritance() { - self::assertEquals(3, $this->_calculateMetric(__METHOD__, 'vars')); + self::assertEquals(3, $this->_calculateClassMetric('vars')); } /** @@ -268,7 +351,7 @@ public function testCalculateVARSMetricOneLevelInheritance() */ public function testCalculateVARSiMetric() { - self::assertEquals(4, $this->_calculateMetric(__METHOD__, 'varsi')); + self::assertEquals(4, $this->_calculateClassMetric('varsi')); } /** @@ -278,7 +361,7 @@ public function testCalculateVARSiMetric() */ public function testCalculateVARSiMetricWithInheritance() { - self::assertEquals(5, $this->_calculateMetric(__METHOD__, 'varsi')); + self::assertEquals(5, $this->_calculateClassMetric('varsi')); } /** @@ -288,7 +371,7 @@ public function testCalculateVARSiMetricWithInheritance() */ public function testCalculateVARSnpMetric() { - self::assertEquals(2, $this->_calculateMetric(__METHOD__, 'varsnp')); + self::assertEquals(2, $this->_calculateClassMetric('varsnp')); } /** @@ -298,7 +381,7 @@ public function testCalculateVARSnpMetric() */ public function testCalculateVARSnpMetricWithInheritance() { - self::assertEquals(1, $this->_calculateMetric(__METHOD__, 'varsnp')); + self::assertEquals(1, $this->_calculateClassMetric('varsnp')); } /** @@ -308,7 +391,7 @@ public function testCalculateVARSnpMetricWithInheritance() */ public function testCalculateWMCMetric() { - self::assertEquals(3, $this->_calculateMetric(__METHOD__, 'wmc')); + self::assertEquals(3, $this->_calculateClassMetric('wmc')); } /** @@ -318,7 +401,7 @@ public function testCalculateWMCMetric() */ public function testCalculateWMCMetricOneLevelInheritance() { - self::assertEquals(3, $this->_calculateMetric(__METHOD__, 'wmc')); + self::assertEquals(3, $this->_calculateClassMetric('wmc')); } /** @@ -328,7 +411,7 @@ public function testCalculateWMCMetricOneLevelInheritance() */ public function testCalculateWMCMetricTwoLevelInheritance() { - self::assertEquals(3, $this->_calculateMetric(__METHOD__, 'wmc')); + self::assertEquals(3, $this->_calculateClassMetric('wmc')); } /** @@ -338,7 +421,7 @@ public function testCalculateWMCMetricTwoLevelInheritance() */ public function testCalculateWMCiMetric() { - self::assertEquals(3, $this->_calculateMetric(__METHOD__, 'wmci')); + self::assertEquals(3, $this->_calculateClassMetric('wmci')); } /** @@ -348,7 +431,7 @@ public function testCalculateWMCiMetric() */ public function testCalculateWMCiMetricOneLevelInheritance() { - self::assertEquals(4, $this->_calculateMetric(__METHOD__, 'wmci')); + self::assertEquals(4, $this->_calculateClassMetric('wmci')); } /** @@ -358,7 +441,7 @@ public function testCalculateWMCiMetricOneLevelInheritance() */ public function testCalculateWMCiMetricTwoLevelInheritance() { - self::assertEquals(5, $this->_calculateMetric(__METHOD__, 'wmci')); + self::assertEquals(5, $this->_calculateClassMetric('wmci')); } /** @@ -368,7 +451,7 @@ public function testCalculateWMCiMetricTwoLevelInheritance() */ public function testCalculateWMCnpMetric() { - self::assertEquals(1, $this->_calculateMetric(__METHOD__, 'wmcnp')); + self::assertEquals(1, $this->_calculateClassMetric('wmcnp')); } /** @@ -378,7 +461,7 @@ public function testCalculateWMCnpMetric() */ public function testCalculateWMCnpMetricOneLevelInheritance() { - self::assertEquals(2, $this->_calculateMetric(__METHOD__, 'wmcnp')); + self::assertEquals(2, $this->_calculateClassMetric('wmcnp')); } /** @@ -388,28 +471,38 @@ public function testCalculateWMCnpMetricOneLevelInheritance() */ public function testCalculateWMCnpMetricTwoLevelInheritance() { - self::assertEquals(1, $this->_calculateMetric(__METHOD__, 'wmcnp')); + self::assertEquals(1, $this->_calculateClassMetric('wmcnp')); } /** * Analyzes the source code associated with the given test case and returns * a single measured metric. * - * @param string $testCase Name of the calling test case. - * @param string $metric Name of the searched metric. + * @param string $name Name of the searched metric. + * + * @return mixed + */ + private function _calculateClassMetric($name) + { + $metrics = $this->_calculateClassMetrics(); + return $metrics[$name]; + } + + /** + * Analyzes the source code associated with the calling test method and + * returns all measured metrics. * * @return mixed */ - private function _calculateMetric($testCase, $metric) + private function _calculateClassMetrics() { - $packages = self::parseTestCaseSource($testCase); + $packages = self::parseTestCaseSource(self::getCallingTestMethod()); $package = $packages->current(); $analyzer = new PHP_Depend_Metrics_ClassLevel_Analyzer(); $analyzer->addAnalyzer(new PHP_Depend_Metrics_CyclomaticComplexity_Analyzer()); $analyzer->analyze($packages); - $metrics = $analyzer->getNodeMetrics($package->getClasses()->current()); - return $metrics[$metric]; + return $analyzer->getNodeMetrics($package->getClasses()->current()); } } \ No newline at end of file diff --git a/src/test/php/PHP/Depend/Metrics/Coupling/AnalyzerTest.php b/src/test/php/PHP/Depend/Metrics/Coupling/AnalyzerTest.php index 62194d7ab1..2663d37dba 100644 --- a/src/test/php/PHP/Depend/Metrics/Coupling/AnalyzerTest.php +++ b/src/test/php/PHP/Depend/Metrics/Coupling/AnalyzerTest.php @@ -82,11 +82,11 @@ public function testGetNodeMetricsReturnsAnEmptyArrayByDefault() } /** - * testGetNodeMetricsReturnsArrayWithExpectedKeys + * testGetNodeMetricsReturnsArrayWithExpectedSetOfMetrics * * @return void */ - public function testGetNodeMetricsReturnsArrayWithExpectedKeys() + public function testGetNodeMetricsReturnsArrayWithExpectedSetOfMetrics() { $packages = self::parseTestCaseSource(__METHOD__); diff --git a/src/test/php/PHP/Depend/_code/metrics/ClassLevel/testCalculateNpmMetricForClassWithAllVisibilityMethods.php b/src/test/php/PHP/Depend/_code/metrics/ClassLevel/testCalculateNpmMetricForClassWithAllVisibilityMethods.php new file mode 100644 index 0000000000..1cbcbae177 --- /dev/null +++ b/src/test/php/PHP/Depend/_code/metrics/ClassLevel/testCalculateNpmMetricForClassWithAllVisibilityMethods.php @@ -0,0 +1,18 @@ +