Skip to content
This repository
Browse code

[implements #10474987] Implement rule for CBO metric

  • Loading branch information...
commit 14277b41a6bf3cd3ff8e2788fa87c9a89558df99 1 parent e549b1c
Manuel Pichler authored March 13, 2011
86  src/main/php/PHP/PMD/Rule/Design/CouplingBetweenObjects.php
... ...
@@ -0,0 +1,86 @@
  1
+<?php
  2
+/**
  3
+ * This file is part of PHP_PMD.
  4
+ *
  5
+ * PHP Version 5
  6
+ *
  7
+ * Copyright (c) 2009-2011, Manuel Pichler <mapi@phpmd.org>.
  8
+ * All rights reserved.
  9
+ *
  10
+ * Redistribution and use in source and binary forms, with or without
  11
+ * modification, are permitted provided that the following conditions
  12
+ * are met:
  13
+ *
  14
+ *   * Redistributions of source code must retain the above copyright
  15
+ *     notice, this list of conditions and the following disclaimer.
  16
+ *
  17
+ *   * Redistributions in binary form must reproduce the above copyright
  18
+ *     notice, this list of conditions and the following disclaimer in
  19
+ *     the documentation and/or other materials provided with the
  20
+ *     distribution.
  21
+ *
  22
+ *   * Neither the name of Manuel Pichler nor the names of his
  23
+ *     contributors may be used to endorse or promote products derived
  24
+ *     from this software without specific prior written permission.
  25
+ *
  26
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37
+ * POSSIBILITY OF SUCH DAMAGE.
  38
+ *
  39
+ * @category   PHP
  40
+ * @package    PHP_PMD
  41
+ * @subpackage Rule_Design
  42
+ * @author     Manuel Pichler <mapi@phpmd.org>
  43
+ * @copyright  2009-2011 Manuel Pichler. All rights reserved.
  44
+ * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  45
+ * @version    SVN: $Id$
  46
+ * @link       http://phpmd.org
  47
+ * @since      1.1.0
  48
+ */
  49
+
  50
+require_once 'PHP/PMD/AbstractRule.php';
  51
+require_once 'PHP/PMD/Rule/IClassAware.php';
  52
+
  53
+/**
  54
+ * This rule class detects the usage of PHP's goto statement.
  55
+ *
  56
+ * @category   PHP
  57
+ * @package    PHP_PMD
  58
+ * @subpackage Rule_Design
  59
+ * @author     Manuel Pichler <mapi@phpmd.org>
  60
+ * @copyright  2009-2011 Manuel Pichler. All rights reserved.
  61
+ * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  62
+ * @version    Release: @package_version@
  63
+ * @link       http://phpmd.org
  64
+ * @since      1.1.0
  65
+ */
  66
+class PHP_PMD_Rule_Design_CouplingBetweenObjects
  67
+       extends PHP_PMD_AbstractRule
  68
+    implements PHP_PMD_Rule_IClassAware
  69
+{
  70
+    /**
  71
+     * This method should implement the violation analysis algorithm of concrete
  72
+     * rule implementations. All extending classes must implement this method.
  73
+     *
  74
+     * @param PHP_PMD_AbstractNode $node The current context for analysis.
  75
+     *
  76
+     * @return void
  77
+     */
  78
+    public function apply(PHP_PMD_AbstractNode $node)
  79
+    {
  80
+        $cbo = $node->getMetric('cbo');
  81
+        if ($cbo >= $this->getIntProperty('minimum')) {
  82
+            $this->addViolation($node, array($node->getName(), $cbo));
  83
+        }
  84
+    }
  85
+
  86
+}
2  src/test/php/PHP/PMD/Rule/Design/AllTests.php
@@ -48,6 +48,7 @@
48 48
 
49 49
 require_once 'PHPUnit/Framework/TestSuite.php';
50 50
 
  51
+require_once dirname(__FILE__) . '/CouplingBetweenObjectsTest.php';
51 52
 require_once dirname(__FILE__) . '/DepthOfInheritanceTest.php';
52 53
 require_once dirname(__FILE__) . '/EvalExpressionTest.php';
53 54
 require_once dirname(__FILE__) . '/ExitExpressionTest.php';
@@ -84,6 +85,7 @@ public static function suite()
84 85
     {
85 86
         $suite = new PHPUnit_Framework_TestSuite('PHP_PMD_Rule_Design - Tests');
86 87
 
  88
+        $suite->addTestSuite('PHP_PMD_Rule_Design_CouplingBetweenObjectsTest');
87 89
         $suite->addTestSuite('PHP_PMD_Rule_Design_DepthOfInheritanceTest');
88 90
         $suite->addTestSuite('PHP_PMD_Rule_Design_EvalExpressionTest');
89 91
         $suite->addTestSuite('PHP_PMD_Rule_Design_ExitExpressionTest');
111  src/test/php/PHP/PMD/Rule/Design/CouplingBetweenObjectsTest.php
... ...
@@ -0,0 +1,111 @@
  1
+<?php
  2
+/**
  3
+ * This file is part of PHP_PMD.
  4
+ *
  5
+ * PHP Version 5
  6
+ *
  7
+ * Copyright (c) 2009-2011, Manuel Pichler <mapi@phpmd.org>.
  8
+ * All rights reserved.
  9
+ *
  10
+ * Redistribution and use in source and binary forms, with or without
  11
+ * modification, are permitted provided that the following conditions
  12
+ * are met:
  13
+ *
  14
+ *   * Redistributions of source code must retain the above copyright
  15
+ *     notice, this list of conditions and the following disclaimer.
  16
+ *
  17
+ *   * Redistributions in binary form must reproduce the above copyright
  18
+ *     notice, this list of conditions and the following disclaimer in
  19
+ *     the documentation and/or other materials provided with the
  20
+ *     distribution.
  21
+ *
  22
+ *   * Neither the name of Manuel Pichler nor the names of his
  23
+ *     contributors may be used to endorse or promote products derived
  24
+ *     from this software without specific prior written permission.
  25
+ *
  26
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37
+ * POSSIBILITY OF SUCH DAMAGE.
  38
+ *
  39
+ * @category   PHP
  40
+ * @package    PHP_PMD
  41
+ * @subpackage Rule_Design
  42
+ * @author     Manuel Pichler <mapi@phpmd.org>
  43
+ * @copyright  2009-2011 Manuel Pichler. All rights reserved.
  44
+ * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  45
+ * @version    SVN: $Id$
  46
+ * @link       http://phpmd.org
  47
+ */
  48
+
  49
+require_once dirname(__FILE__) . '/../../AbstractTest.php';
  50
+
  51
+require_once 'PHP/PMD/Rule/Design/CouplingBetweenObjects.php';
  52
+
  53
+/**
  54
+ * Test case for the {@link PHP_PMD_Rule_Design_CouplingBetweenObjects} class.
  55
+ *
  56
+ * @category   PHP
  57
+ * @package    PHP_PMD
  58
+ * @subpackage Rule_Design
  59
+ * @author     Manuel Pichler <mapi@phpmd.org>
  60
+ * @copyright  2009-2011 Manuel Pichler. All rights reserved.
  61
+ * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  62
+ * @version    Release: @package_version@
  63
+ * @link       http://phpmd.org
  64
+ *
  65
+ * @ticket 10474987
  66
+ * @covers PHP_PMD_Rule_Design_CouplingBetweenObjects
  67
+ * @group phpmd
  68
+ * @group phpmd::rule
  69
+ * @group phpmd::rule::design
  70
+ */
  71
+class PHP_PMD_Rule_Design_CouplingBetweenObjectsTest extends PHP_PMD_AbstractTest
  72
+{
  73
+    /**
  74
+     * testRuleNotAppliesToClassWithCboLessThanThreshold
  75
+     * 
  76
+     * @return void
  77
+     */
  78
+    public function testRuleNotAppliesToClassWithCboLessThanThreshold()
  79
+    {
  80
+        $rule = new PHP_PMD_Rule_Design_CouplingBetweenObjects();
  81
+        $rule->setReport($this->getReportMock(0));
  82
+        $rule->addProperty('minimum', '42');
  83
+        $rule->apply($this->getClassMock('cbo', 41));
  84
+    }
  85
+
  86
+    /**
  87
+     * testRuleAppliesToClassWithCboEqualToThreshold
  88
+     *
  89
+     * @return void
  90
+     */
  91
+    public function testRuleAppliesToClassWithCboEqualToThreshold()
  92
+    {
  93
+        $rule = new PHP_PMD_Rule_Design_CouplingBetweenObjects();
  94
+        $rule->setReport($this->getReportMock(1));
  95
+        $rule->addProperty('minimum', '42');
  96
+        $rule->apply($this->getClassMock('cbo', 42));        
  97
+    }
  98
+
  99
+    /**
  100
+     * testRuleAppliesToClassWithCboGreaterThanThreshold
  101
+     *
  102
+     * @return void
  103
+     */
  104
+    public function testRuleAppliesToClassWithCboGreaterThanThreshold()
  105
+    {
  106
+        $rule = new PHP_PMD_Rule_Design_CouplingBetweenObjects();
  107
+        $rule->setReport($this->getReportMock(1));
  108
+        $rule->addProperty('minimum', '41');
  109
+        $rule->apply($this->getClassMock('cbo', 42));
  110
+    }
  111
+}

0 notes on commit 14277b4

Please sign in to comment.
Something went wrong with that request. Please try again.