diff --git a/CHANGELOG b/CHANGELOG index 774d5c9b3..ffc7137d4 100644 --- a/CHANGELOG +++ b/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) ======================== diff --git a/src/bin/phpmd.php b/src/bin/phpmd.php index b0eacbbfd..1fc4cb638 100755 --- a/src/bin/phpmd.php +++ b/src/bin/phpmd.php @@ -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 . '.' ); diff --git a/src/main/php/PHP/PMD/Rule/Design/GotoStatement.php b/src/main/php/PHP/PMD/Rule/Design/GotoStatement.php new file mode 100644 index 000000000..e4530c69c --- /dev/null +++ b/src/main/php/PHP/PMD/Rule/Design/GotoStatement.php @@ -0,0 +1,87 @@ +. + * 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 + * @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 + * @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())); + } + } + +} \ No newline at end of file diff --git a/src/main/resources/rulesets/design.xml b/src/main/resources/rulesets/design.xml index 0e0dbe61e..17d860493 100644 --- a/src/main/resources/rulesets/design.xml +++ b/src/main/resources/rulesets/design.xml @@ -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"> + externalInfoUrl="http://phpmd.org/rules/design.html#evalexpression"> + + + + + 1 + + + + + + + externalInfoUrl="http://phpmd.org/rules/design.html#numberofchildren"> + externalInfoUrl="http://phpmd.org/rules/design.html#depthofinheritance"> _getCallingTestCase(); @@ -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()); } /** diff --git a/src/test/php/PHP/PMD/AllTests.php b/src/test/php/PHP/PMD/AllTests.php index 77c61f9ad..f806a0f14 100644 --- a/src/test/php/PHP/PMD/AllTests.php +++ b/src/test/php/PHP/PMD/AllTests.php @@ -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'; @@ -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()); diff --git a/src/test/php/PHP/PMD/Integration/AllTests.php b/src/test/php/PHP/PMD/Integration/AllTests.php new file mode 100644 index 000000000..500aec04a --- /dev/null +++ b/src/test/php/PHP/PMD/Integration/AllTests.php @@ -0,0 +1,88 @@ +. + * 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 + * @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 + * @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(); + } +} diff --git a/src/test/php/PHP/PMD/Integration/GotoStatementIntegrationTest.php b/src/test/php/PHP/PMD/Integration/GotoStatementIntegrationTest.php new file mode 100644 index 000000000..3d55a5954 --- /dev/null +++ b/src/test/php/PHP/PMD/Integration/GotoStatementIntegrationTest.php @@ -0,0 +1,96 @@ +. + * 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 + * @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 dirname(__FILE__) . '/../AbstractTest.php'; + +require_once 'PHP/PMD/TextUI/Command.php'; + +/** + * Test case for the goto statement GotoStatementIntegrationTest. + * + * @category PHP + * @package PHP_PMD + * @subpackage Integration + * @author Manuel Pichler + * @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 + * + * @group phpmd + * @group phpmd::integration + * @group integrationtest + */ +class PHP_PMD_Integration_GotoStatementIntegrationTest extends PHP_PMD_AbstractTest +{ + /** + * testReportContainsGotoStatementWarning + * + * @return void + * @outputBuffering enabled + */ + public function testReportContainsGotoStatementWarning() + { + $file = self::createTempFileUri(); + + PHP_PMD_TextUI_Command::main( + array( + __FILE__, + $this->createCodeResourceUriForTest(), + 'text', + 'design', + '--reportfile', + $file + ) + ); + + self::assertContains('utilizes a goto statement.', file_get_contents($file)); + } +} \ No newline at end of file diff --git a/src/test/php/PHP/PMD/Rule/Design/AllTests.php b/src/test/php/PHP/PMD/Rule/Design/AllTests.php index ff0d98f18..cc842261e 100644 --- a/src/test/php/PHP/PMD/Rule/Design/AllTests.php +++ b/src/test/php/PHP/PMD/Rule/Design/AllTests.php @@ -51,6 +51,7 @@ require_once dirname(__FILE__) . '/DepthOfInheritanceTest.php'; require_once dirname(__FILE__) . '/EvalExpressionTest.php'; require_once dirname(__FILE__) . '/ExitExpressionTest.php'; +require_once dirname(__FILE__) . '/GotoStatementTest.php'; require_once dirname(__FILE__) . '/LongClassTest.php'; require_once dirname(__FILE__) . '/LongMethodTest.php'; require_once dirname(__FILE__) . '/LongParameterListTest.php'; @@ -86,6 +87,7 @@ public static function suite() $suite->addTestSuite('PHP_PMD_Rule_Design_DepthOfInheritanceTest'); $suite->addTestSuite('PHP_PMD_Rule_Design_EvalExpressionTest'); $suite->addTestSuite('PHP_PMD_Rule_Design_ExitExpressionTest'); + $suite->addTestSuite('PHP_PMD_Rule_Design_GotoStatementTest'); $suite->addTestSuite('PHP_PMD_Rule_Design_LongClassTest'); $suite->addTestSuite('PHP_PMD_Rule_Design_LongMethodTest'); $suite->addTestSuite('PHP_PMD_Rule_Design_LongParameterListTest'); diff --git a/src/test/php/PHP/PMD/Rule/Design/GotoStatementTest.php b/src/test/php/PHP/PMD/Rule/Design/GotoStatementTest.php new file mode 100644 index 000000000..cd8180664 --- /dev/null +++ b/src/test/php/PHP/PMD/Rule/Design/GotoStatementTest.php @@ -0,0 +1,122 @@ +. + * 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 + * @copyright 2009-2011 Manuel Pichler. All rights reserved. + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + * @version SVN: $Id$ + * @link https://www.pivotaltracker.com/story/show/10474873 + * @ticket 10474873 + */ + +require_once dirname(__FILE__) . '/../../AbstractTest.php'; + +require_once 'PHP/PMD/Rule/Design/GotoStatement.php'; + +/** + * Test case for the {@link PHP_PMD_Rule_Design_GotoStatement} class. + * + * @category PHP + * @package PHP_PMD + * @subpackage Rule_Design + * @author Manuel Pichler + * @copyright 2009-2011 Manuel Pichler. All rights reserved. + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + * @version Release: @package_version@ + * @link https://www.pivotaltracker.com/story/show/10474873 + * @ticket 10474873 + * + * @covers PHP_PMD_Rule_Design_GotoStatement + * @group phpmd + * @group phpmd::rule + * @group phpmd::rule::design + * @group unittest + */ +class PHP_PMD_Rule_Design_GotoStatementTest extends PHP_PMD_AbstractTest +{ + /** + * testRuleNotAppliesToMethodWithoutGotoStatement + * + * @return void + */ + public function testRuleNotAppliesToMethodWithoutGotoStatement() + { + $rule = new PHP_PMD_Rule_Design_GotoStatement(); + $rule->setReport($this->getReportMock(0)); + $rule->apply($this->getMethod()); + } + + /** + * testRuleAppliesToMethodWithGotoStatement + * + * @return void + */ + public function testRuleAppliesToMethodWithGotoStatement() + { + $rule = new PHP_PMD_Rule_Design_GotoStatement(); + $rule->setReport($this->getReportMock(1)); + $rule->apply($this->getMethod()); + } + + /** + * testRuleNotAppliesToFunctionWithoutGotoStatement + * + * @return void + */ + public function testRuleNotAppliesToFunctionWithoutGotoStatement() + { + $rule = new PHP_PMD_Rule_Design_GotoStatement(); + $rule->setReport($this->getReportMock(0)); + $rule->apply($this->getFunction()); + } + + /** + * testRuleAppliesToFunctionWithGotoStatement + * + * @return void + */ + public function testRuleAppliesToFunctionWithGotoStatement() + { + $rule = new PHP_PMD_Rule_Design_GotoStatement(); + $rule->setReport($this->getReportMock(1)); + $rule->apply($this->getFunction()); + } +} \ No newline at end of file diff --git a/src/test/resources/files/Integration/GotoStatementIntegration/testReportContainsGotoStatementWarning.php b/src/test/resources/files/Integration/GotoStatementIntegration/testReportContainsGotoStatementWarning.php new file mode 100644 index 000000000..bbc29e8ae --- /dev/null +++ b/src/test/resources/files/Integration/GotoStatementIntegration/testReportContainsGotoStatementWarning.php @@ -0,0 +1,10 @@ +