Skip to content
This repository
Browse code

Merge branch 'goto-statement-rule'

  • Loading branch information...
commit 2745a2082fccff7b35e0b797ec8b317eaad9c9ae 2 parents 659231f + 6679e3a
Manuel Pichler authored March 11, 2011
6  CHANGELOG
... ...
@@ -1,3 +1,9 @@
  1
+phpmd-1.1.0 (2011/03/  )
  2
+========================
  3
+
  4
+- Implemented #10474873: Add rule for PHP's goto statement. Implemented
  5
+  with commit #.
  6
+
1 7
 phpmd-1.0.1 (2011/02/12)
2 8
 ========================
3 9
 
4  src/bin/phpmd.php
@@ -4,9 +4,9 @@
4 4
 // PEAR installation workaround
5 5
 if (strpos('@package_version@', '@package_version') === 0) {
6 6
     set_include_path(
7  
-        dirname(__FILE__) . '/src/main/php' .
  7
+        dirname(__FILE__) . '/../main/php' .
8 8
         PATH_SEPARATOR .
9  
-        dirname(__FILE__) . '/lib/pdepend/src/main/php' .
  9
+        dirname(__FILE__) . '/../../lib/pdepend/src/main/php' .
10 10
         PATH_SEPARATOR .
11 11
         '.'
12 12
     );
87  src/main/php/PHP/PMD/Rule/Design/GotoStatement.php
... ...
@@ -0,0 +1,87 @@
  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/IMethodAware.php';
  52
+require_once 'PHP/PMD/Rule/IFunctionAware.php';
  53
+
  54
+/**
  55
+ * This rule class detects the usage of PHP's goto statement.
  56
+ *
  57
+ * @category   PHP
  58
+ * @package    PHP_PMD
  59
+ * @subpackage Rule_Design
  60
+ * @author     Manuel Pichler <mapi@phpmd.org>
  61
+ * @copyright  2009-2011 Manuel Pichler. All rights reserved.
  62
+ * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  63
+ * @version    Release: @package_version@
  64
+ * @link       http://phpmd.org
  65
+ * @since      1.1.0
  66
+ */
  67
+class PHP_PMD_Rule_Design_GotoStatement
  68
+       extends PHP_PMD_AbstractRule
  69
+    implements PHP_PMD_Rule_IMethodAware,
  70
+               PHP_PMD_Rule_IFunctionAware
  71
+{
  72
+    /**
  73
+     * This method should implement the violation analysis algorithm of concrete
  74
+     * rule implementations. All extending classes must implement this method.
  75
+     *
  76
+     * @param PHP_PMD_AbstractNode $node The current context for analysis.
  77
+     *
  78
+     * @return void
  79
+     */
  80
+    public function apply(PHP_PMD_AbstractNode $node)
  81
+    {
  82
+        foreach ($node->findChildrenOfType('GotoStatement') as $exit) {
  83
+            $this->addViolation($exit, array($node->getType(), $node->getName()));
  84
+        }
  85
+    }
  86
+    
  87
+}
47  src/main/resources/rulesets/design.xml
@@ -14,7 +14,7 @@ The Code Size Ruleset contains a collection of rules that find software design r
14 14
           since="0.2"
15 15
           message = "The {0} {1}() contains an exit expression."
16 16
           class="PHP_PMD_Rule_Design_ExitExpression"
17  
-          externalInfoUrl="http://phpmd.org/rules/codesize.html#exitexpression">
  17
+          externalInfoUrl="http://phpmd.org/rules/design.html#exitexpression">
18 18
         <description>
19 19
             <![CDATA[
20 20
 An exit-expression within regular code is untestable and therefore it should
@@ -41,7 +41,7 @@ class Foo {
41 41
           since="0.2"
42 42
           message = "The {0} {1}() contains an eval expression."
43 43
           class="PHP_PMD_Rule_Design_EvalExpression"
44  
-          externalInfoUrl="http://phpmd.org/rules/codesize.html#evalexpression">
  44
+          externalInfoUrl="http://phpmd.org/rules/design.html#evalexpression">
45 45
         <description>
46 46
             <![CDATA[
47 47
 An eval-expression is untestable, a security risk and bad practice. Therefore
@@ -64,11 +64,50 @@ class Foo {
64 64
         </example>
65 65
     </rule>
66 66
 
  67
+    <rule name="GotoStatement"
  68
+          since="1.1.0"
  69
+          message="The {0} {1}() utilizes a goto statement."
  70
+          class="PHP_PMD_Rule_Design_GotoStatement"
  71
+          externalInfoUrl="http://phpmd.org/rules/design.html#gotostatement">
  72
+        <description>
  73
+            <![CDATA[
  74
+Goto makes code harder to read and it is nearly impossible to understand the
  75
+control flow of an application that uses this language construct. Therefore it
  76
+should be avoided. Consider to replace Goto with regular control structures and
  77
+separate methods/function, which are easier to read.
  78
+            ]]>
  79
+        </description>
  80
+        <priority>1</priority>
  81
+        <properties />
  82
+        <example>
  83
+            <![CDATA[
  84
+class Foo {
  85
+    public function bar($param)  {
  86
+        A:
  87
+        if ($param === 42) {
  88
+            goto X;
  89
+        }
  90
+        Y:
  91
+        if (time() % 42 === 23) {
  92
+            goto Z;
  93
+        }
  94
+        X:
  95
+        if (time() % 23 === 42) {
  96
+            goto Y;
  97
+        }
  98
+        Z:
  99
+        return 42;                 
  100
+    }
  101
+}
  102
+            ]]>
  103
+        </example>
  104
+    </rule>
  105
+
67 106
     <rule name="NumberOfChildren"
68 107
           since="0.2"
69 108
           message = "The class {0} has {1} children. Consider to rebalance this class hierarchy."
70 109
           class="PHP_PMD_Rule_Design_NumberOfChildren"
71  
-          externalInfoUrl="http://phpmd.org/rules/codesize.html#numberofchildren">
  110
+          externalInfoUrl="http://phpmd.org/rules/design.html#numberofchildren">
72 111
         <description>
73 112
             <![CDATA[
74 113
 A class with an excessive number of children is an indicator for an unbalanced
@@ -86,7 +125,7 @@ class hierarchy. You should consider to refactor this class hierarchy.
86 125
           since="0.2"
87 126
           message = "The class {0} has {1} parents. Consider to reduce the depth of this class hierarchy."
88 127
           class="PHP_PMD_Rule_Design_DepthOfInheritance"
89  
-          externalInfoUrl="http://phpmd.org/rules/codesize.html#depthofinheritance">
  128
+          externalInfoUrl="http://phpmd.org/rules/design.html#depthofinheritance">
90 129
         <description>
91 130
             <![CDATA[
92 131
 A class with many parents is an indicator for an unbalanced and wrong class
22  src/test/php/PHP/PMD/AbstractTest.php
@@ -176,12 +176,12 @@ protected function getFunction()
176 176
     }
177 177
 
178 178
     /**
179  
-     * Parses the source code for the calling test method and returns the first
180  
-     * package node found in the parsed file.
  179
+     * Returns the absolute path for a test resource for the current test.
181 180
      *
182  
-     * @return PHP_Depend_Code_Package
  181
+     * @return string
  182
+     * @since 1.1.0
183 183
      */
184  
-    private function _parseTestCaseSource()
  184
+    protected function createCodeResourceUriForTest()
185 185
     {
186 186
         $frame = $this->_getCallingTestCase();
187 187
 
@@ -191,13 +191,23 @@ private function _parseTestCaseSource()
191 191
             $localPath = strtr(substr($frame['class'], 8, -4), '_', '/');
192 192
         }
193 193
 
194  
-        $sourceFile = sprintf(
  194
+        return sprintf(
195 195
             '%s/../../../resources/files/%s/%s.php',
196 196
             dirname(__FILE__),
197 197
             $localPath,
198 198
             $frame['function']
199 199
         );
200  
-        return $this->_parseSource($sourceFile);
  200
+    }
  201
+
  202
+    /**
  203
+     * Parses the source code for the calling test method and returns the first
  204
+     * package node found in the parsed file.
  205
+     *
  206
+     * @return PHP_Depend_Code_Package
  207
+     */
  208
+    private function _parseTestCaseSource()
  209
+    {
  210
+        return $this->_parseSource($this->createCodeResourceUriForTest());
201 211
     }
202 212
 
203 213
     /**
2  src/test/php/PHP/PMD/AllTests.php
@@ -55,6 +55,7 @@
55 55
 require_once dirname(__FILE__) . '/RuleSetTest.php';
56 56
 require_once dirname(__FILE__) . '/RuleTest.php';
57 57
 require_once dirname(__FILE__) . '/RuleViolationTest.php';
  58
+require_once dirname(__FILE__) . '/Integration/AllTests.php';
58 59
 require_once dirname(__FILE__) . '/Node/AllTests.php';
59 60
 require_once dirname(__FILE__) . '/Regression/AllTests.php';
60 61
 require_once dirname(__FILE__) . '/Renderer/AllTests.php';
@@ -92,6 +93,7 @@ public static function suite()
92 93
         $suite->addTestSuite('PHP_PMD_RuleTest');
93 94
         $suite->addTestSuite('PHP_PMD_RuleViolationTest');
94 95
 
  96
+        $suite->addTest(PHP_PMD_Integration_AllTests::suite());
95 97
         $suite->addTest(PHP_PMD_Node_AllTests::suite());
96 98
         $suite->addTest(PHP_PMD_Regression_AllTests::suite());
97 99
         $suite->addTest(PHP_PMD_Renderer_AllTests::suite());
88  src/test/php/PHP/PMD/Integration/AllTests.php
... ...
@@ -0,0 +1,88 @@
  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 Integration
  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 'PHPUnit/Framework/TestSuite.php';
  51
+
  52
+require_once dirname(__FILE__) . '/GotoStatementIntegrationTest.php';
  53
+
  54
+/**
  55
+ * Main test suite for all PHP_PMD integration tests
  56
+ *
  57
+ * @category   PHP
  58
+ * @package    PHP_PMD
  59
+ * @subpackage Integration
  60
+ * @author     Manuel Pichler <mapi@phpmd.org>
  61
+ * @copyright  2009-2011 Manuel Pichler. All rights reserved.
  62
+ * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  63
+ * @version    Release: @package_version@
  64
+ * @link       http://phpmd.org
  65
+ * @since      1.1.0
  66
+ */
  67
+class PHP_PMD_Integration_AllTests extends PHPUnit_Framework_TestSuite
  68
+{
  69
+    /**
  70
+     * Constructs a new test suite instance.
  71
+     */
  72
+    public function __construct()
  73
+    {
  74
+        parent::__construct('PHP_PMD_Integration - Tests');
  75
+
  76
+        $this->addTestSuite('PHP_PMD_Integration_GotoStatementIntegrationTest');
  77
+    }
  78
+
  79
+    /**
  80
+     * Creates a phpunit test suite.
  81
+     *
  82
+     * @return PHPUnit_Framework_TestSuite
  83
+     */
  84
+    public static function suite()
  85
+    {
  86
+        return new self();
  87
+    }
  88
+}
96  src/test/php/PHP/PMD/Integration/GotoStatementIntegrationTest.php
... ...
@@ -0,0 +1,96 @@
  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 Integration
  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 dirname(__FILE__) . '/../AbstractTest.php';
  51
+
  52
+require_once 'PHP/PMD/TextUI/Command.php';
  53
+
  54
+/**
  55
+ * Test case for the goto statement GotoStatementIntegrationTest.
  56
+ *
  57
+ * @category   PHP
  58
+ * @package    PHP_PMD
  59
+ * @subpackage Integration
  60
+ * @author     Manuel Pichler <mapi@phpmd.org>
  61
+ * @copyright  2009-2011 Manuel Pichler. All rights reserved.
  62
+ * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  63
+ * @version    Release: @package_version@
  64
+ * @link       http://phpmd.org
  65
+ * @since      1.1.0
  66
+ *
  67
+ * @group phpmd
  68
+ * @group phpmd::integration
  69
+ * @group integrationtest
  70
+ */
  71
+class PHP_PMD_Integration_GotoStatementIntegrationTest extends PHP_PMD_AbstractTest
  72
+{
  73
+    /**
  74
+     * testReportContainsGotoStatementWarning
  75
+     *
  76
+     * @return void
  77
+     * @outputBuffering enabled
  78
+     */
  79
+    public function testReportContainsGotoStatementWarning()
  80
+    {
  81
+        $file = self::createTempFileUri();
  82
+
  83
+        PHP_PMD_TextUI_Command::main(
  84
+            array(
  85
+                __FILE__,
  86
+                $this->createCodeResourceUriForTest(),
  87
+                'text',
  88
+                'design',
  89
+                '--reportfile',
  90
+                $file
  91
+            )
  92
+        );
  93
+
  94
+        self::assertContains('utilizes a goto statement.', file_get_contents($file));
  95
+    }
  96
+}
2  src/test/php/PHP/PMD/Rule/Design/AllTests.php
@@ -51,6 +51,7 @@
51 51
 require_once dirname(__FILE__) . '/DepthOfInheritanceTest.php';
52 52
 require_once dirname(__FILE__) . '/EvalExpressionTest.php';
53 53
 require_once dirname(__FILE__) . '/ExitExpressionTest.php';
  54
+require_once dirname(__FILE__) . '/GotoStatementTest.php';
54 55
 require_once dirname(__FILE__) . '/LongClassTest.php';
55 56
 require_once dirname(__FILE__) . '/LongMethodTest.php';
56 57
 require_once dirname(__FILE__) . '/LongParameterListTest.php';
@@ -86,6 +87,7 @@ public static function suite()
86 87
         $suite->addTestSuite('PHP_PMD_Rule_Design_DepthOfInheritanceTest');
87 88
         $suite->addTestSuite('PHP_PMD_Rule_Design_EvalExpressionTest');
88 89
         $suite->addTestSuite('PHP_PMD_Rule_Design_ExitExpressionTest');
  90
+        $suite->addTestSuite('PHP_PMD_Rule_Design_GotoStatementTest');
89 91
         $suite->addTestSuite('PHP_PMD_Rule_Design_LongClassTest');
90 92
         $suite->addTestSuite('PHP_PMD_Rule_Design_LongMethodTest');
91 93
         $suite->addTestSuite('PHP_PMD_Rule_Design_LongParameterListTest');
122  src/test/php/PHP/PMD/Rule/Design/GotoStatementTest.php
... ...
@@ -0,0 +1,122 @@
  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       https://www.pivotaltracker.com/story/show/10474873
  47
+ * @ticket     10474873
  48
+ */
  49
+
  50
+require_once dirname(__FILE__) . '/../../AbstractTest.php';
  51
+
  52
+require_once 'PHP/PMD/Rule/Design/GotoStatement.php';
  53
+
  54
+/**
  55
+ * Test case for the {@link PHP_PMD_Rule_Design_GotoStatement} class.
  56
+ *
  57
+ * @category   PHP
  58
+ * @package    PHP_PMD
  59
+ * @subpackage Rule_Design
  60
+ * @author     Manuel Pichler <mapi@phpmd.org>
  61
+ * @copyright  2009-2011 Manuel Pichler. All rights reserved.
  62
+ * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  63
+ * @version    Release: @package_version@
  64
+ * @link       https://www.pivotaltracker.com/story/show/10474873
  65
+ * @ticket     10474873
  66
+ *
  67
+ * @covers PHP_PMD_Rule_Design_GotoStatement
  68
+ * @group phpmd
  69
+ * @group phpmd::rule
  70
+ * @group phpmd::rule::design
  71
+ * @group unittest
  72
+ */
  73
+class PHP_PMD_Rule_Design_GotoStatementTest extends PHP_PMD_AbstractTest
  74
+{
  75
+    /**
  76
+     * testRuleNotAppliesToMethodWithoutGotoStatement
  77
+     *
  78
+     * @return void
  79
+     */
  80
+    public function testRuleNotAppliesToMethodWithoutGotoStatement()
  81
+    {
  82
+        $rule = new PHP_PMD_Rule_Design_GotoStatement();
  83
+        $rule->setReport($this->getReportMock(0));
  84
+        $rule->apply($this->getMethod());
  85
+    }
  86
+
  87
+    /**
  88
+     * testRuleAppliesToMethodWithGotoStatement
  89
+     *
  90
+     * @return void
  91
+     */
  92
+    public function testRuleAppliesToMethodWithGotoStatement()
  93
+    {
  94
+        $rule = new PHP_PMD_Rule_Design_GotoStatement();
  95
+        $rule->setReport($this->getReportMock(1));
  96
+        $rule->apply($this->getMethod());
  97
+    }
  98
+
  99
+    /**
  100
+     * testRuleNotAppliesToFunctionWithoutGotoStatement
  101
+     *
  102
+     * @return void
  103
+     */
  104
+    public function testRuleNotAppliesToFunctionWithoutGotoStatement()
  105
+    {
  106
+        $rule = new PHP_PMD_Rule_Design_GotoStatement();
  107
+        $rule->setReport($this->getReportMock(0));
  108
+        $rule->apply($this->getFunction());
  109
+    }
  110
+
  111
+    /**
  112
+     * testRuleAppliesToFunctionWithGotoStatement
  113
+     *
  114
+     * @return void
  115
+     */
  116
+    public function testRuleAppliesToFunctionWithGotoStatement()
  117
+    {
  118
+        $rule = new PHP_PMD_Rule_Design_GotoStatement();
  119
+        $rule->setReport($this->getReportMock(1));
  120
+        $rule->apply($this->getFunction());
  121
+    }
  122
+}
10  src/test/resources/files/Integration/GotoStatementIntegration/testReportContainsGotoStatementWarning.php
... ...
@@ -0,0 +1,10 @@
  1
+<?php
  2
+function testReportContainsGotoStatementWarning()
  3
+{
  4
+    LABEL:
  5
+        echo 'You goto ' . __LINE__;
  6
+
  7
+    if (time() % 42 === 23) {
  8
+        goto LABEL;
  9
+    }
  10
+}
10  src/test/resources/files/Rule/Design/GotoStatement/testRuleAppliesToFunctionWithGotoStatement.php
... ...
@@ -0,0 +1,10 @@
  1
+<?php
  2
+function testRuleAppliesToFunctionWithGotoStatement()
  3
+{
  4
+    LABEL:
  5
+        echo 'FOOBAR';
  6
+
  7
+    if (time() % 23 === 42) {
  8
+        goto LABEL;
  9
+    }
  10
+}
13  src/test/resources/files/Rule/Design/GotoStatement/testRuleAppliesToMethodWithGotoStatement.php
... ...
@@ -0,0 +1,13 @@
  1
+<?php
  2
+class testRuleAppliesToMethodWithGotoStatementClass
  3
+{
  4
+    public function testRuleAppliesToMethodWithGotoStatement()
  5
+    {
  6
+        LABEL:
  7
+            echo 'YES';
  8
+
  9
+        if (time() % 42 === 0) {
  10
+            goto LABEL;
  11
+        }
  12
+    }
  13
+}
5  src/test/resources/files/Rule/Design/GotoStatement/testRuleNotAppliesToFunctionWithoutGotoStatement.php
... ...
@@ -0,0 +1,5 @@
  1
+<?php
  2
+function testRuleNotAppliesToFunctionWithoutGotoStatement($foo)
  3
+{
  4
+    return 42 * $foo;
  5
+}
8  src/test/resources/files/Rule/Design/GotoStatement/testRuleNotAppliesToMethodWithoutGotoStatement.php
... ...
@@ -0,0 +1,8 @@
  1
+<?php
  2
+class testRuleNotAppliesToMethodWithoutGotoStatementClass
  3
+{
  4
+    public function testRuleNotAppliesToMethodWithoutGotoStatement($foo)
  5
+    {
  6
+        return $foo;
  7
+    }
  8
+}

0 notes on commit 2745a20

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