diff --git a/setup b/setup index 64bc46490..126806a55 160000 --- a/setup +++ b/setup @@ -1 +1 @@ -Subproject commit 64bc4649094bbf0dc87884d1d6ed48296e617b5c +Subproject commit 126806a556b6935f5f54a7ac3ef758940e636e46 diff --git a/src/main/php/PHP/Depend/Parser.php b/src/main/php/PHP/Depend/Parser.php index 510565c8f..b098c2194 100644 --- a/src/main/php/PHP/Depend/Parser.php +++ b/src/main/php/PHP/Depend/Parser.php @@ -429,13 +429,22 @@ protected function reset($modifiers = 0) $this->modifiers = $modifiers; } + /** + * Tests if the given token type is a reserved keyword in the supported PHP + * version. + * + * @param $tokenType + * @return boolean + * @since 1.1.1 + */ + abstract protected function isKeyword($tokenType); + /** * Will return true if the given $tokenType is a valid class * name part. * * @param integer $tokenType The type of a parsed token. - * - * @return string + * @return boolean * @since 0.10.6 */ protected abstract function isClassName($tokenType); @@ -4818,13 +4827,14 @@ protected abstract function parseIntegerNumber(); * @return PHP_Depend_Code_ASTArray * @since 1.0.0 */ - private function doParseArray() + private function doParseArray($static = false) { $this->tokenStack->push(); return $this->setNodePositionsAndReturn( $this->parseArray( - $this->builder->buildAstArray() + $this->builder->buildAstArray(), + $static ) ); } @@ -4841,29 +4851,32 @@ protected abstract function isArrayStartDelimiter(); /** * Parses a php array declaration. * - * @param PHP_Depend_Code_ASTArray $array The context array node. + * @param PHP_Depend_Code_ASTArray $array + * @param boolean $static * * @return PHP_Depend_Code_ASTArray * @since 1.0.0 */ - protected abstract function parseArray(PHP_Depend_Code_ASTArray $array); + protected abstract function parseArray(PHP_Depend_Code_ASTArray $array, $static = false); /** * Parses all elements in an array. * - * @param PHP_Depend_Code_ASTArray $array The context array node. - * @param integer $endDelimiter The version specific delimiter. + * @param PHP_Depend_Code_ASTArray $array + * @param integer $endDelimiter + * @param boolean $static * * @return PHP_Depend_Code_ASTArray * @since 1.0.0 */ protected function parseArrayElements( PHP_Depend_Code_ASTArray $array, - $endDelimiter + $endDelimiter, + $static = false ) { $this->consumeComments(); while ($endDelimiter !== $this->tokenizer->peek()) { - $array->addChild($this->parseArrayElement()); + $array->addChild($this->parseArrayElement($static)); $this->consumeComments(); if (self::T_COMMA === $this->tokenizer->peek()) { @@ -4880,10 +4893,11 @@ protected function parseArrayElements( * An array element can have a simple value, a key/value pair, a value by * reference or a key/value pair with a referenced value. * + * @param boolean $static * @return PHP_Depend_Code_ASTArrayElement * @since 1.0.0 */ - protected function parseArrayElement() + protected function parseArrayElement($static = false) { $this->consumeComments(); @@ -4891,8 +4905,27 @@ protected function parseArrayElement() $element = $this->builder->buildAstArrayElement(); if ($this->parseOptionalByReference()) { + + if ($static) { + $tokens = $this->tokenStack->pop(); + + throw new PHP_Depend_Parser_UnexpectedTokenException( + end($tokens), + $this->sourceFile->getFileName() + ); + } + $element->setByReference(); } + + $this->consumeComments(); + if ($this->isKeyword($this->tokenizer->peek())) { + throw new PHP_Depend_Parser_UnexpectedTokenException( + $this->tokenizer->next(), + $this->sourceFile->getFileName() + ); + } + $element->addChild($this->parseExpression()); $this->consumeComments(); @@ -6182,8 +6215,14 @@ private function parseVariableDeclarator() private function parseStaticValueOrStaticArray() { $this->consumeComments(); - if ($this->tokenizer->peek() === self::T_ARRAY) { - return $this->parseStaticArray(); + if ($this->isArrayStartDelimiter()) { + // TODO: Use default value as value! + $defaultValue = $this->doParseArray(true); + + $value = new PHP_Depend_Code_Value(); + $value->setValue(array()); + + return $value; } return $this->parseStaticValue(); } @@ -6304,95 +6343,6 @@ private function parseStaticValue() throw new PHP_Depend_Parser_TokenStreamEndException($this->tokenizer); } - /** - * This method parses an array as it is used for for parameter or property - * default values. - * - * Note: At the moment the implementation of this method only returns an - * empty array, but consumes all tokens that belong to the array - * declaration. - * - * TODO: Implement array content/value handling, but how should we handle - * constant values like array(self::FOO, FOOBAR)? - * - * @return array - * @since 0.9.5 - */ - private function parseStaticArray() - { - $staticValue = array(); - - // Fetch all tokens that belong to this array - $this->consumeToken(self::T_ARRAY); - $this->consumeComments(); - $this->consumeToken(self::T_PARENTHESIS_OPEN); - - $parenthesis = 1; - - $tokenType = $this->tokenizer->peek(); - while ($tokenType !== self::T_EOF) { - - switch ($tokenType) { - - case self::T_PARENTHESIS_CLOSE: - if (--$parenthesis === 0) { - break 2; - } - $this->consumeToken(self::T_PARENTHESIS_CLOSE); - break; - - case self::T_PARENTHESIS_OPEN: - $this->consumeToken(self::T_PARENTHESIS_OPEN); - ++$parenthesis; - break; - - case self::T_DIR: - case self::T_NULL: - case self::T_TRUE: - case self::T_FILE: - case self::T_LINE: - case self::T_NS_C: - case self::T_PLUS: - case self::T_SELF: - case self::T_ARRAY: - case self::T_FALSE: - case self::T_EQUAL: - case self::T_COMMA: - case self::T_MINUS: - case self::T_COMMENT: - case self::T_DOC_COMMENT: - case self::T_DOUBLE_COLON: - case self::T_STRING: - case self::T_BACKSLASH: - case self::T_DNUMBER: - case self::T_LNUMBER: - case self::T_FUNC_C: - case self::T_CLASS_C: - case self::T_METHOD_C: - case self::T_STATIC: - case self::T_PARENT: - case self::T_NUM_STRING: - case self::T_DOUBLE_ARROW: - case self::T_CONSTANT_ENCAPSED_STRING: - $this->consumeToken($tokenType); - break; - - default: - break 2; - } - - $tokenType = $this->tokenizer->peek(); - } - - // Read closing parenthesis - $this->consumeToken(self::T_PARENTHESIS_CLOSE); - - $defaultValue = new PHP_Depend_Code_Value(); - $defaultValue->setValue($staticValue); - - return $defaultValue; - } - /** * Checks if the given expression is a read/write variable as defined in * the PHP zend_language_parser.y definition. diff --git a/src/main/php/PHP/Depend/Parser/VersionAllParser.php b/src/main/php/PHP/Depend/Parser/VersionAllParser.php index 6c0b8b02f..96a07549c 100644 --- a/src/main/php/PHP/Depend/Parser/VersionAllParser.php +++ b/src/main/php/PHP/Depend/Parser/VersionAllParser.php @@ -64,6 +64,24 @@ */ class PHP_Depend_Parser_VersionAllParser extends PHP_Depend_Parser { + /** + * Tests if the given token type is a reserved keyword in the supported PHP + * version. + * + * @param $tokenType + * @return boolean + * @since 1.1.1 + */ + protected function isKeyword($tokenType) + { + switch ($tokenType) { + case self::T_CLASS: + case self::T_INTERFACE: + case self::T_FUNCTION: + return true; + } + return false; + } /** * Will return true if the given $tokenType is a valid class @@ -77,21 +95,20 @@ class PHP_Depend_Parser_VersionAllParser extends PHP_Depend_Parser protected function isClassName($tokenType) { switch ($tokenType) { - - case self::T_DIR: - case self::T_USE: - case self::T_GOTO: - case self::T_NULL: - case self::T_NS_C: - case self::T_TRUE: - case self::T_CLONE: - case self::T_FALSE: - case self::T_TRAIT: - case self::T_STRING: - case self::T_TRAIT_C: - case self::T_INSTEADOF: - case self::T_NAMESPACE: - return true; + case self::T_DIR: + case self::T_USE: + case self::T_GOTO: + case self::T_NULL: + case self::T_NS_C: + case self::T_TRUE: + case self::T_CLONE: + case self::T_FALSE: + case self::T_TRAIT: + case self::T_STRING: + case self::T_TRAIT_C: + case self::T_INSTEADOF: + case self::T_NAMESPACE: + return true; } return false; } @@ -324,12 +341,12 @@ protected function isArrayStartDelimiter() /** * Parses a php array declaration. * - * @param PHP_Depend_Code_ASTArray $array The context array node. - * + * @param PHP_Depend_Code_ASTArray $array + * @param boolean $static * @return PHP_Depend_Code_ASTArray * @since 1.0.0 */ - protected function parseArray(PHP_Depend_Code_ASTArray $array) + protected function parseArray(PHP_Depend_Code_ASTArray $array, $static = false) { switch ($this->tokenizer->peek()) { @@ -337,13 +354,13 @@ protected function parseArray(PHP_Depend_Code_ASTArray $array) $this->consumeToken(self::T_ARRAY); $this->consumeComments(); $this->consumeToken(self::T_PARENTHESIS_OPEN); - $this->parseArrayElements($array, self::T_PARENTHESIS_CLOSE); + $this->parseArrayElements($array, self::T_PARENTHESIS_CLOSE, $static); $this->consumeToken(self::T_PARENTHESIS_CLOSE); break; default: $this->consumeToken(self::T_SQUARED_BRACKET_OPEN); - $this->parseArrayElements($array, self::T_SQUARED_BRACKET_CLOSE); + $this->parseArrayElements($array, self::T_SQUARED_BRACKET_CLOSE, $static); $this->consumeToken(self::T_SQUARED_BRACKET_CLOSE); break; } diff --git a/src/site/docx/changes.xml b/src/site/docx/changes.xml index 349aa04ea..1e4868615 100644 --- a/src/site/docx/changes.xml +++ b/src/site/docx/changes.xml @@ -9,6 +9,15 @@ + + + + PHP 5.4 array syntax is not supported in property initialization. + + + + * @copyright 2008-2012 Manuel Pichler. All rights reserved. + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + * @version SVN: $Id$ + * @link https://github.com/pdepend/pdepend/issues/95 + * @link https://github.com/pdepend/pdepend/issues/104 + */ + +require_once dirname(__FILE__) . '/AbstractTest.php'; + +/** + * Test case for bug #104 and #95. + * + * @category PHP + * @package PHP_Depend + * @subpackage Bugs + * @author Manuel Pichler + * @copyright 2008-2012 Manuel Pichler. All rights reserved. + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + * @version Release: @package_version@ + * @link https://github.com/pdepend/pdepend/issues/95 + * @link https://github.com/pdepend/pdepend/issues/104 + * + * @ticket 104 + * @ticket 95 + * @covers stdClass + * @group pdepend + * @group pdepend::bugs + * @group regressiontest + */ +class PHP_Depend_Bugs_ShortArraySyntaxInitializerBug00000104Test extends PHP_Depend_Bugs_AbstractTest +{ + /** + * testPropertyDefaultValue + * + * @return void + */ + public function testPropertyDefaultValue() + { + $this->parseCodeResourceForTest(); + } +} diff --git a/src/test/resources/files/Parser/testParserHandlesParentKeywordInMethodParameterDefaultValue.php b/src/test/resources/files/Parser/testParserHandlesParentKeywordInMethodParameterDefaultValue.php index a6e1480bd..88e573fc0 100644 --- a/src/test/resources/files/Parser/testParserHandlesParentKeywordInMethodParameterDefaultValue.php +++ b/src/test/resources/files/Parser/testParserHandlesParentKeywordInMethodParameterDefaultValue.php @@ -1,5 +1,5 @@ diff --git a/src/test/resources/files/bugs/00000104/testPropertyDefaultValue.php b/src/test/resources/files/bugs/00000104/testPropertyDefaultValue.php new file mode 100644 index 000000000..bc696c714 --- /dev/null +++ b/src/test/resources/files/bugs/00000104/testPropertyDefaultValue.php @@ -0,0 +1,8 @@ + 23, + 'B' => [ 42 ] + ]; +}