From 1209b0ece93a755e0298a66ebd8c91d856ca58d2 Mon Sep 17 00:00:00 2001 From: Manuel Pichler Date: Sun, 20 Sep 2015 13:30:02 +0200 Subject: [PATCH] Support for constants with array values implemented. --- .../php/PDepend/Source/AST/ASTConstant.php | 13 +++-- .../Source/AST/ASTConstantDeclarator.php | 11 ++-- .../Source/Language/PHP/AbstractPHPParser.php | 18 +++++- .../Source/Language/PHP/PHPParserGeneric.php | 14 +++++ ...StoreTokensForAllNodeTypesIssue079Test.php | 16 ------ .../PHP/PHPParserGenericVersion56Test.php | 56 +++++++++++++++++++ ...nstantSupportForArrayWithKeyValuePairs.php | 9 +++ ...upportForArrayWithSelfReferenceInClass.php | 8 +++ ...rtForArrayWithSelfReferenceInInterface.php | 8 +++ .../testConstantSupportForArrayWithValues.php | 10 ++++ ...ExceptionForArrayInConstantDeclaration.php | 6 -- 11 files changed, 133 insertions(+), 36 deletions(-) create mode 100644 src/test/resources/files/Source/Language/PHP/PHPParserGenericVersion56/testConstantSupportForArrayWithKeyValuePairs.php create mode 100644 src/test/resources/files/Source/Language/PHP/PHPParserGenericVersion56/testConstantSupportForArrayWithSelfReferenceInClass.php create mode 100644 src/test/resources/files/Source/Language/PHP/PHPParserGenericVersion56/testConstantSupportForArrayWithSelfReferenceInInterface.php create mode 100644 src/test/resources/files/Source/Language/PHP/PHPParserGenericVersion56/testConstantSupportForArrayWithValues.php delete mode 100644 src/test/resources/files/issues/079/testParserThrowsExpectedExceptionForArrayInConstantDeclaration.php diff --git a/src/main/php/PDepend/Source/AST/ASTConstant.php b/src/main/php/PDepend/Source/AST/ASTConstant.php index 876363d5d..330b8a90b 100644 --- a/src/main/php/PDepend/Source/AST/ASTConstant.php +++ b/src/main/php/PDepend/Source/AST/ASTConstant.php @@ -43,6 +43,8 @@ namespace PDepend\Source\AST; +use PDepend\Source\ASTVisitor\ASTVisitor; + /** * This class represents a constant node. * @@ -50,19 +52,18 @@ * @license http://www.opensource.org/licenses/bsd-license.php BSD License * @since 0.9.6 */ -class ASTConstant extends \PDepend\Source\AST\ASTNode +class ASTConstant extends ASTNode { /** * Accept method of the visitor design pattern. This method will be called * by a visitor during tree traversal. * - * @param \PDepend\Source\ASTVisitor\ASTVisitor $visitor The calling visitor instance. - * @param mixed $data - * + * @param \PDepend\Source\ASTVisitor\ASTVisitor $visitor + * @param mixed $data * @return mixed - * @since 0.9.12 + * @since 0.9.12 */ - public function accept(\PDepend\Source\ASTVisitor\ASTVisitor $visitor, $data = null) + public function accept(ASTVisitor $visitor, $data = null) { return $visitor->visitConstant($this, $data); } diff --git a/src/main/php/PDepend/Source/AST/ASTConstantDeclarator.php b/src/main/php/PDepend/Source/AST/ASTConstantDeclarator.php index a1d6756cd..c379ab493 100644 --- a/src/main/php/PDepend/Source/AST/ASTConstantDeclarator.php +++ b/src/main/php/PDepend/Source/AST/ASTConstantDeclarator.php @@ -42,6 +42,8 @@ namespace PDepend\Source\AST; +use PDepend\Source\ASTVisitor\ASTVisitor; + /** * This class represents a single constant declarator within a constant * definition. @@ -77,7 +79,7 @@ * @copyright 2008-2015 Manuel Pichler. All rights reserved. * @license http://www.opensource.org/licenses/bsd-license.php BSD License */ -class ASTConstantDeclarator extends \PDepend\Source\AST\ASTNode +class ASTConstantDeclarator extends ASTNode { /** * The initial declaration value for this node or null. @@ -111,13 +113,12 @@ public function setValue(ASTValue $value) * Accept method of the visitor design pattern. This method will be called * by a visitor during tree traversal. * - * @param \PDepend\Source\ASTVisitor\ASTVisitor $visitor The calling visitor instance. - * @param mixed $data - * + * @param \PDepend\Source\ASTVisitor\ASTVisitor $visitor + * @param mixed $data * @return mixed * @since 0.9.12 */ - public function accept(\PDepend\Source\ASTVisitor\ASTVisitor $visitor, $data = null) + public function accept(ASTVisitor $visitor, $data = null) { return $visitor->visitConstantDeclarator($this, $data); } diff --git a/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php b/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php index d5fbbd24b..8bf1c264c 100644 --- a/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php +++ b/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php @@ -5884,7 +5884,7 @@ private function parseConstantDefinition() * @return \PDepend\Source\AST\ASTConstantDeclarator * @since 0.9.6 */ - private function parseConstantDeclarator() + protected function parseConstantDeclarator() { // Remove leading comments and create a new token stack $this->consumeComments(); @@ -5896,11 +5896,23 @@ private function parseConstantDeclarator() $this->consumeToken(Tokens::T_EQUAL); $declarator = $this->builder->buildAstConstantDeclarator($token->image); - $declarator->setValue($this->parseStaticValue()); + $declarator->setValue($this->parseConstantDeclaratorValue()); return $this->setNodePositionsAndReturn($declarator); } + /** + * Parses the value of a php constant. By default this can be only static + * values that were allowed in the oldest supported PHP version. + * + * @return \PDepend\Source\AST\ASTValue + * @since 2.2.x + */ + protected function parseConstantDeclaratorValue() + { + return $this->parseStaticValue(); + } + /** * This method parses a static variable declaration list, a member primary * prefix invoked in the static context of a class or it parses a static @@ -6066,7 +6078,7 @@ protected function parseVariableDeclarator() * @return \PDepend\Source\AST\ASTValue * @since 0.9.6 */ - private function parseStaticValueOrStaticArray() + protected function parseStaticValueOrStaticArray() { $this->consumeComments(); if ($this->isArrayStartDelimiter()) { diff --git a/src/main/php/PDepend/Source/Language/PHP/PHPParserGeneric.php b/src/main/php/PDepend/Source/Language/PHP/PHPParserGeneric.php index 67a45960f..c7136984a 100644 --- a/src/main/php/PDepend/Source/Language/PHP/PHPParserGeneric.php +++ b/src/main/php/PDepend/Source/Language/PHP/PHPParserGeneric.php @@ -405,4 +405,18 @@ protected function parseFormalParameter() return $parameter; } + + /** + * Parses constant default values as they are supported by the most recent + * PHP version. + * + * @return \PDepend\Source\AST\ASTValue + * @since 2.2.x + */ + protected function parseConstantDeclaratorValue() + { + return $this->parseStaticValueOrStaticArray(); + } + + } diff --git a/src/test/php/PDepend/Issues/StoreTokensForAllNodeTypesIssue079Test.php b/src/test/php/PDepend/Issues/StoreTokensForAllNodeTypesIssue079Test.php index 124e24057..41ade46bf 100644 --- a/src/test/php/PDepend/Issues/StoreTokensForAllNodeTypesIssue079Test.php +++ b/src/test/php/PDepend/Issues/StoreTokensForAllNodeTypesIssue079Test.php @@ -93,22 +93,6 @@ public function testParameterContainsEndLineOfLastToken() $this->assertEquals(11, $parameters[0]->getEndLine()); } - /** - * Tests that the parser throws an exception when a constant declaration - * contains an invalid token. - * - * @return void - */ - public function testParserThrowsExpectedExceptionForArrayInConstantDeclaration() - { - $this->setExpectedException( - '\\PDepend\\Source\\Parser\\UnexpectedTokenException', - 'Unexpected token: array, line: 4, col: 17, file: ' - ); - - self::parseTestCaseSource(__METHOD__); - } - /** * Tests that the parser stores the expected function tokens. * diff --git a/src/test/php/PDepend/Source/Language/PHP/PHPParserGenericVersion56Test.php b/src/test/php/PDepend/Source/Language/PHP/PHPParserGenericVersion56Test.php index fd67fcb60..4497e5184 100644 --- a/src/test/php/PDepend/Source/Language/PHP/PHPParserGenericVersion56Test.php +++ b/src/test/php/PDepend/Source/Language/PHP/PHPParserGenericVersion56Test.php @@ -110,4 +110,60 @@ public function testMultipleShiftRightInConstantInitializer() $this->assertInstanceOf('PDepend\\Source\\AST\\ASTConstantDefinition', $const); } + + /** + * testConstantSupportForScalarArrayValues + * + * @return void + * @link https://github.com/pdepend/pdepend/issues/209 + */ + public function testConstantSupportForArrayWithValues() + { + $class = $this->getFirstClassForTestCase(); + $const = $class->getChild(0); + + $this->assertInstanceOf('PDepend\\Source\\AST\\ASTConstantDefinition', $const); + } + + /** + * testConstantSupportForArrayWithKeyValuePairs + * + * @return void + * @link https://github.com/pdepend/pdepend/issues/209 + */ + public function testConstantSupportForArrayWithKeyValuePairs() + { + $class = $this->getFirstClassForTestCase(); + $const = $class->getChild(0); + + $this->assertInstanceOf('PDepend\\Source\\AST\\ASTConstantDefinition', $const); + } + + /** + * testConstantSupportForArrayWithSelfReferenceInClass + * + * @return void + * @link https://github.com/pdepend/pdepend/issues/192 + */ + public function testConstantSupportForArrayWithSelfReferenceInClass() + { + $class = $this->getFirstClassForTestCase(); + $const = $class->getChild(0); + + $this->assertInstanceOf('PDepend\\Source\\AST\\ASTConstantDefinition', $const); + } + + /** + * testConstantSupportForArrayWithSelfReferenceInInterface + * + * @return void + * @link https://github.com/pdepend/pdepend/issues/192 + */ + public function testConstantSupportForArrayWithSelfReferenceInInterface() + { + $class = $this->getFirstInterfaceForTestCase(); + $const = $class->getChild(0); + + $this->assertInstanceOf('PDepend\\Source\\AST\\ASTConstantDefinition', $const); + } } diff --git a/src/test/resources/files/Source/Language/PHP/PHPParserGenericVersion56/testConstantSupportForArrayWithKeyValuePairs.php b/src/test/resources/files/Source/Language/PHP/PHPParserGenericVersion56/testConstantSupportForArrayWithKeyValuePairs.php new file mode 100644 index 000000000..4ee3497a4 --- /dev/null +++ b/src/test/resources/files/Source/Language/PHP/PHPParserGenericVersion56/testConstantSupportForArrayWithKeyValuePairs.php @@ -0,0 +1,9 @@ + 'January', + 'February' => 'February', + // … + ]; +} diff --git a/src/test/resources/files/Source/Language/PHP/PHPParserGenericVersion56/testConstantSupportForArrayWithSelfReferenceInClass.php b/src/test/resources/files/Source/Language/PHP/PHPParserGenericVersion56/testConstantSupportForArrayWithSelfReferenceInClass.php new file mode 100644 index 000000000..35ed5afa4 --- /dev/null +++ b/src/test/resources/files/Source/Language/PHP/PHPParserGenericVersion56/testConstantSupportForArrayWithSelfReferenceInClass.php @@ -0,0 +1,8 @@ +