Skip to content

Commit

Permalink
Merge branch 'goto-statement-rule'
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelpichler committed Mar 11, 2011
2 parents 659231f + 6679e3a commit 2745a20
Show file tree
Hide file tree
Showing 15 changed files with 510 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
@@ -1,3 +1,9 @@
phpmd-1.1.0 (2011/03/ )
========================

- Implemented #10474873: Add rule for PHP's goto statement. Implemented
with commit #.

phpmd-1.0.1 (2011/02/12)
========================

Expand Down
4 changes: 2 additions & 2 deletions src/bin/phpmd.php
Expand Up @@ -4,9 +4,9 @@
// PEAR installation workaround
if (strpos('@package_version@', '@package_version') === 0) {
set_include_path(
dirname(__FILE__) . '/src/main/php' .
dirname(__FILE__) . '/../main/php' .
PATH_SEPARATOR .
dirname(__FILE__) . '/lib/pdepend/src/main/php' .
dirname(__FILE__) . '/../../lib/pdepend/src/main/php' .
PATH_SEPARATOR .
'.'
);
Expand Down
87 changes: 87 additions & 0 deletions src/main/php/PHP/PMD/Rule/Design/GotoStatement.php
@@ -0,0 +1,87 @@
<?php
/**
* This file is part of PHP_PMD.
*
* PHP Version 5
*
* Copyright (c) 2009-2011, Manuel Pichler <mapi@phpmd.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_PMD
* @subpackage Rule_Design
* @author Manuel Pichler <mapi@phpmd.org>
* @copyright 2009-2011 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://phpmd.org
* @since 1.1.0
*/

require_once 'PHP/PMD/AbstractRule.php';
require_once 'PHP/PMD/Rule/IMethodAware.php';
require_once 'PHP/PMD/Rule/IFunctionAware.php';

/**
* This rule class detects the usage of PHP's goto statement.
*
* @category PHP
* @package PHP_PMD
* @subpackage Rule_Design
* @author Manuel Pichler <mapi@phpmd.org>
* @copyright 2009-2011 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version Release: @package_version@
* @link http://phpmd.org
* @since 1.1.0
*/
class PHP_PMD_Rule_Design_GotoStatement
extends PHP_PMD_AbstractRule
implements PHP_PMD_Rule_IMethodAware,
PHP_PMD_Rule_IFunctionAware
{
/**
* This method should implement the violation analysis algorithm of concrete
* rule implementations. All extending classes must implement this method.
*
* @param PHP_PMD_AbstractNode $node The current context for analysis.
*
* @return void
*/
public function apply(PHP_PMD_AbstractNode $node)
{
foreach ($node->findChildrenOfType('GotoStatement') as $exit) {
$this->addViolation($exit, array($node->getType(), $node->getName()));
}
}

}
47 changes: 43 additions & 4 deletions src/main/resources/rulesets/design.xml
Expand Up @@ -14,7 +14,7 @@ The Code Size Ruleset contains a collection of rules that find software design r
since="0.2"
message = "The {0} {1}() contains an exit expression."
class="PHP_PMD_Rule_Design_ExitExpression"
externalInfoUrl="http://phpmd.org/rules/codesize.html#exitexpression">
externalInfoUrl="http://phpmd.org/rules/design.html#exitexpression">
<description>
<![CDATA[
An exit-expression within regular code is untestable and therefore it should
Expand All @@ -41,7 +41,7 @@ class Foo {
since="0.2"
message = "The {0} {1}() contains an eval expression."
class="PHP_PMD_Rule_Design_EvalExpression"
externalInfoUrl="http://phpmd.org/rules/codesize.html#evalexpression">
externalInfoUrl="http://phpmd.org/rules/design.html#evalexpression">
<description>
<![CDATA[
An eval-expression is untestable, a security risk and bad practice. Therefore
Expand All @@ -64,11 +64,50 @@ class Foo {
</example>
</rule>

<rule name="GotoStatement"
since="1.1.0"
message="The {0} {1}() utilizes a goto statement."
class="PHP_PMD_Rule_Design_GotoStatement"
externalInfoUrl="http://phpmd.org/rules/design.html#gotostatement">
<description>
<![CDATA[
Goto makes code harder to read and it is nearly impossible to understand the
control flow of an application that uses this language construct. Therefore it
should be avoided. Consider to replace Goto with regular control structures and
separate methods/function, which are easier to read.
]]>
</description>
<priority>1</priority>
<properties />
<example>
<![CDATA[
class Foo {
public function bar($param) {
A:
if ($param === 42) {
goto X;
}
Y:
if (time() % 42 === 23) {
goto Z;
}
X:
if (time() % 23 === 42) {
goto Y;
}
Z:
return 42;
}
}
]]>
</example>
</rule>

<rule name="NumberOfChildren"
since="0.2"
message = "The class {0} has {1} children. Consider to rebalance this class hierarchy."
class="PHP_PMD_Rule_Design_NumberOfChildren"
externalInfoUrl="http://phpmd.org/rules/codesize.html#numberofchildren">
externalInfoUrl="http://phpmd.org/rules/design.html#numberofchildren">
<description>
<![CDATA[
A class with an excessive number of children is an indicator for an unbalanced
Expand All @@ -86,7 +125,7 @@ class hierarchy. You should consider to refactor this class hierarchy.
since="0.2"
message = "The class {0} has {1} parents. Consider to reduce the depth of this class hierarchy."
class="PHP_PMD_Rule_Design_DepthOfInheritance"
externalInfoUrl="http://phpmd.org/rules/codesize.html#depthofinheritance">
externalInfoUrl="http://phpmd.org/rules/design.html#depthofinheritance">
<description>
<![CDATA[
A class with many parents is an indicator for an unbalanced and wrong class
Expand Down
22 changes: 16 additions & 6 deletions src/test/php/PHP/PMD/AbstractTest.php
Expand Up @@ -176,12 +176,12 @@ protected function getFunction()
}

/**
* Parses the source code for the calling test method and returns the first
* package node found in the parsed file.
* Returns the absolute path for a test resource for the current test.
*
* @return PHP_Depend_Code_Package
* @return string
* @since 1.1.0
*/
private function _parseTestCaseSource()
protected function createCodeResourceUriForTest()
{
$frame = $this->_getCallingTestCase();

Expand All @@ -191,13 +191,23 @@ private function _parseTestCaseSource()
$localPath = strtr(substr($frame['class'], 8, -4), '_', '/');
}

$sourceFile = sprintf(
return sprintf(
'%s/../../../resources/files/%s/%s.php',
dirname(__FILE__),
$localPath,
$frame['function']
);
return $this->_parseSource($sourceFile);
}

/**
* Parses the source code for the calling test method and returns the first
* package node found in the parsed file.
*
* @return PHP_Depend_Code_Package
*/
private function _parseTestCaseSource()
{
return $this->_parseSource($this->createCodeResourceUriForTest());
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/test/php/PHP/PMD/AllTests.php
Expand Up @@ -55,6 +55,7 @@
require_once dirname(__FILE__) . '/RuleSetTest.php';
require_once dirname(__FILE__) . '/RuleTest.php';
require_once dirname(__FILE__) . '/RuleViolationTest.php';
require_once dirname(__FILE__) . '/Integration/AllTests.php';
require_once dirname(__FILE__) . '/Node/AllTests.php';
require_once dirname(__FILE__) . '/Regression/AllTests.php';
require_once dirname(__FILE__) . '/Renderer/AllTests.php';
Expand Down Expand Up @@ -92,6 +93,7 @@ public static function suite()
$suite->addTestSuite('PHP_PMD_RuleTest');
$suite->addTestSuite('PHP_PMD_RuleViolationTest');

$suite->addTest(PHP_PMD_Integration_AllTests::suite());
$suite->addTest(PHP_PMD_Node_AllTests::suite());
$suite->addTest(PHP_PMD_Regression_AllTests::suite());
$suite->addTest(PHP_PMD_Renderer_AllTests::suite());
Expand Down
88 changes: 88 additions & 0 deletions src/test/php/PHP/PMD/Integration/AllTests.php
@@ -0,0 +1,88 @@
<?php
/**
* This file is part of PHP_PMD.
*
* PHP Version 5
*
* Copyright (c) 2009-2011, Manuel Pichler <mapi@phpmd.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_PMD
* @subpackage Integration
* @author Manuel Pichler <mapi@phpmd.org>
* @copyright 2009-2011 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://phpmd.org
* @since 1.1.0
*/

require_once 'PHPUnit/Framework/TestSuite.php';

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

/**
* Main test suite for all PHP_PMD integration tests
*
* @category PHP
* @package PHP_PMD
* @subpackage Integration
* @author Manuel Pichler <mapi@phpmd.org>
* @copyright 2009-2011 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version Release: @package_version@
* @link http://phpmd.org
* @since 1.1.0
*/
class PHP_PMD_Integration_AllTests extends PHPUnit_Framework_TestSuite
{
/**
* Constructs a new test suite instance.
*/
public function __construct()
{
parent::__construct('PHP_PMD_Integration - Tests');

$this->addTestSuite('PHP_PMD_Integration_GotoStatementIntegrationTest');
}

/**
* Creates a phpunit test suite.
*
* @return PHPUnit_Framework_TestSuite
*/
public static function suite()
{
return new self();
}
}

0 comments on commit 2745a20

Please sign in to comment.