Skip to content

Commit f6ee217

Browse files
committed
Fixed #95: PHP 5.4 array syntax is not supported in property init
1 parent 95e057b commit f6ee217

File tree

7 files changed

+191
-124
lines changed

7 files changed

+191
-124
lines changed

setup

Submodule setup updated from 64bc464 to 126806a

src/main/php/PHP/Depend/Parser.php

Lines changed: 52 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,22 @@ protected function reset($modifiers = 0)
429429
$this->modifiers = $modifiers;
430430
}
431431

432+
/**
433+
* Tests if the given token type is a reserved keyword in the supported PHP
434+
* version.
435+
*
436+
* @param $tokenType
437+
* @return boolean
438+
* @since 1.1.1
439+
*/
440+
abstract protected function isKeyword($tokenType);
441+
432442
/**
433443
* Will return <b>true</b> if the given <b>$tokenType</b> is a valid class
434444
* name part.
435445
*
436446
* @param integer $tokenType The type of a parsed token.
437-
*
438-
* @return string
447+
* @return boolean
439448
* @since 0.10.6
440449
*/
441450
protected abstract function isClassName($tokenType);
@@ -4818,13 +4827,14 @@ protected abstract function parseIntegerNumber();
48184827
* @return PHP_Depend_Code_ASTArray
48194828
* @since 1.0.0
48204829
*/
4821-
private function doParseArray()
4830+
private function doParseArray($static = false)
48224831
{
48234832
$this->tokenStack->push();
48244833

48254834
return $this->setNodePositionsAndReturn(
48264835
$this->parseArray(
4827-
$this->builder->buildAstArray()
4836+
$this->builder->buildAstArray(),
4837+
$static
48284838
)
48294839
);
48304840
}
@@ -4841,29 +4851,32 @@ protected abstract function isArrayStartDelimiter();
48414851
/**
48424852
* Parses a php array declaration.
48434853
*
4844-
* @param PHP_Depend_Code_ASTArray $array The context array node.
4854+
* @param PHP_Depend_Code_ASTArray $array
4855+
* @param boolean $static
48454856
*
48464857
* @return PHP_Depend_Code_ASTArray
48474858
* @since 1.0.0
48484859
*/
4849-
protected abstract function parseArray(PHP_Depend_Code_ASTArray $array);
4860+
protected abstract function parseArray(PHP_Depend_Code_ASTArray $array, $static = false);
48504861

48514862
/**
48524863
* Parses all elements in an array.
48534864
*
4854-
* @param PHP_Depend_Code_ASTArray $array The context array node.
4855-
* @param integer $endDelimiter The version specific delimiter.
4865+
* @param PHP_Depend_Code_ASTArray $array
4866+
* @param integer $endDelimiter
4867+
* @param boolean $static
48564868
*
48574869
* @return PHP_Depend_Code_ASTArray
48584870
* @since 1.0.0
48594871
*/
48604872
protected function parseArrayElements(
48614873
PHP_Depend_Code_ASTArray $array,
4862-
$endDelimiter
4874+
$endDelimiter,
4875+
$static = false
48634876
) {
48644877
$this->consumeComments();
48654878
while ($endDelimiter !== $this->tokenizer->peek()) {
4866-
$array->addChild($this->parseArrayElement());
4879+
$array->addChild($this->parseArrayElement($static));
48674880

48684881
$this->consumeComments();
48694882
if (self::T_COMMA === $this->tokenizer->peek()) {
@@ -4880,19 +4893,39 @@ protected function parseArrayElements(
48804893
* An array element can have a simple value, a key/value pair, a value by
48814894
* reference or a key/value pair with a referenced value.
48824895
*
4896+
* @param boolean $static
48834897
* @return PHP_Depend_Code_ASTArrayElement
48844898
* @since 1.0.0
48854899
*/
4886-
protected function parseArrayElement()
4900+
protected function parseArrayElement($static = false)
48874901
{
48884902
$this->consumeComments();
48894903

48904904
$this->tokenStack->push();
48914905

48924906
$element = $this->builder->buildAstArrayElement();
48934907
if ($this->parseOptionalByReference()) {
4908+
4909+
if ($static) {
4910+
$tokens = $this->tokenStack->pop();
4911+
4912+
throw new PHP_Depend_Parser_UnexpectedTokenException(
4913+
end($tokens),
4914+
$this->sourceFile->getFileName()
4915+
);
4916+
}
4917+
48944918
$element->setByReference();
48954919
}
4920+
4921+
$this->consumeComments();
4922+
if ($this->isKeyword($this->tokenizer->peek())) {
4923+
throw new PHP_Depend_Parser_UnexpectedTokenException(
4924+
$this->tokenizer->next(),
4925+
$this->sourceFile->getFileName()
4926+
);
4927+
}
4928+
48964929
$element->addChild($this->parseExpression());
48974930

48984931
$this->consumeComments();
@@ -6182,8 +6215,14 @@ private function parseVariableDeclarator()
61826215
private function parseStaticValueOrStaticArray()
61836216
{
61846217
$this->consumeComments();
6185-
if ($this->tokenizer->peek() === self::T_ARRAY) {
6186-
return $this->parseStaticArray();
6218+
if ($this->isArrayStartDelimiter()) {
6219+
// TODO: Use default value as value!
6220+
$defaultValue = $this->doParseArray(true);
6221+
6222+
$value = new PHP_Depend_Code_Value();
6223+
$value->setValue(array());
6224+
6225+
return $value;
61876226
}
61886227
return $this->parseStaticValue();
61896228
}
@@ -6304,95 +6343,6 @@ private function parseStaticValue()
63046343
throw new PHP_Depend_Parser_TokenStreamEndException($this->tokenizer);
63056344
}
63066345

6307-
/**
6308-
* This method parses an array as it is used for for parameter or property
6309-
* default values.
6310-
*
6311-
* Note: At the moment the implementation of this method only returns an
6312-
* empty array, but consumes all tokens that belong to the array
6313-
* declaration.
6314-
*
6315-
* TODO: Implement array content/value handling, but how should we handle
6316-
* constant values like array(self::FOO, FOOBAR)?
6317-
*
6318-
* @return array
6319-
* @since 0.9.5
6320-
*/
6321-
private function parseStaticArray()
6322-
{
6323-
$staticValue = array();
6324-
6325-
// Fetch all tokens that belong to this array
6326-
$this->consumeToken(self::T_ARRAY);
6327-
$this->consumeComments();
6328-
$this->consumeToken(self::T_PARENTHESIS_OPEN);
6329-
6330-
$parenthesis = 1;
6331-
6332-
$tokenType = $this->tokenizer->peek();
6333-
while ($tokenType !== self::T_EOF) {
6334-
6335-
switch ($tokenType) {
6336-
6337-
case self::T_PARENTHESIS_CLOSE:
6338-
if (--$parenthesis === 0) {
6339-
break 2;
6340-
}
6341-
$this->consumeToken(self::T_PARENTHESIS_CLOSE);
6342-
break;
6343-
6344-
case self::T_PARENTHESIS_OPEN:
6345-
$this->consumeToken(self::T_PARENTHESIS_OPEN);
6346-
++$parenthesis;
6347-
break;
6348-
6349-
case self::T_DIR:
6350-
case self::T_NULL:
6351-
case self::T_TRUE:
6352-
case self::T_FILE:
6353-
case self::T_LINE:
6354-
case self::T_NS_C:
6355-
case self::T_PLUS:
6356-
case self::T_SELF:
6357-
case self::T_ARRAY:
6358-
case self::T_FALSE:
6359-
case self::T_EQUAL:
6360-
case self::T_COMMA:
6361-
case self::T_MINUS:
6362-
case self::T_COMMENT:
6363-
case self::T_DOC_COMMENT:
6364-
case self::T_DOUBLE_COLON:
6365-
case self::T_STRING:
6366-
case self::T_BACKSLASH:
6367-
case self::T_DNUMBER:
6368-
case self::T_LNUMBER:
6369-
case self::T_FUNC_C:
6370-
case self::T_CLASS_C:
6371-
case self::T_METHOD_C:
6372-
case self::T_STATIC:
6373-
case self::T_PARENT:
6374-
case self::T_NUM_STRING:
6375-
case self::T_DOUBLE_ARROW:
6376-
case self::T_CONSTANT_ENCAPSED_STRING:
6377-
$this->consumeToken($tokenType);
6378-
break;
6379-
6380-
default:
6381-
break 2;
6382-
}
6383-
6384-
$tokenType = $this->tokenizer->peek();
6385-
}
6386-
6387-
// Read closing parenthesis
6388-
$this->consumeToken(self::T_PARENTHESIS_CLOSE);
6389-
6390-
$defaultValue = new PHP_Depend_Code_Value();
6391-
$defaultValue->setValue($staticValue);
6392-
6393-
return $defaultValue;
6394-
}
6395-
63966346
/**
63976347
* Checks if the given expression is a read/write variable as defined in
63986348
* the PHP zend_language_parser.y definition.

src/main/php/PHP/Depend/Parser/VersionAllParser.php

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@
6464
*/
6565
class PHP_Depend_Parser_VersionAllParser extends PHP_Depend_Parser
6666
{
67+
/**
68+
* Tests if the given token type is a reserved keyword in the supported PHP
69+
* version.
70+
*
71+
* @param $tokenType
72+
* @return boolean
73+
* @since 1.1.1
74+
*/
75+
protected function isKeyword($tokenType)
76+
{
77+
switch ($tokenType) {
78+
case self::T_CLASS:
79+
case self::T_INTERFACE:
80+
case self::T_FUNCTION:
81+
return true;
82+
}
83+
return false;
84+
}
6785

6886
/**
6987
* Will return <b>true</b> if the given <b>$tokenType</b> is a valid class
@@ -77,21 +95,20 @@ class PHP_Depend_Parser_VersionAllParser extends PHP_Depend_Parser
7795
protected function isClassName($tokenType)
7896
{
7997
switch ($tokenType) {
80-
81-
case self::T_DIR:
82-
case self::T_USE:
83-
case self::T_GOTO:
84-
case self::T_NULL:
85-
case self::T_NS_C:
86-
case self::T_TRUE:
87-
case self::T_CLONE:
88-
case self::T_FALSE:
89-
case self::T_TRAIT:
90-
case self::T_STRING:
91-
case self::T_TRAIT_C:
92-
case self::T_INSTEADOF:
93-
case self::T_NAMESPACE:
94-
return true;
98+
case self::T_DIR:
99+
case self::T_USE:
100+
case self::T_GOTO:
101+
case self::T_NULL:
102+
case self::T_NS_C:
103+
case self::T_TRUE:
104+
case self::T_CLONE:
105+
case self::T_FALSE:
106+
case self::T_TRAIT:
107+
case self::T_STRING:
108+
case self::T_TRAIT_C:
109+
case self::T_INSTEADOF:
110+
case self::T_NAMESPACE:
111+
return true;
95112
}
96113
return false;
97114
}
@@ -324,26 +341,26 @@ protected function isArrayStartDelimiter()
324341
/**
325342
* Parses a php array declaration.
326343
*
327-
* @param PHP_Depend_Code_ASTArray $array The context array node.
328-
*
344+
* @param PHP_Depend_Code_ASTArray $array
345+
* @param boolean $static
329346
* @return PHP_Depend_Code_ASTArray
330347
* @since 1.0.0
331348
*/
332-
protected function parseArray(PHP_Depend_Code_ASTArray $array)
349+
protected function parseArray(PHP_Depend_Code_ASTArray $array, $static = false)
333350
{
334351
switch ($this->tokenizer->peek()) {
335352

336353
case self::T_ARRAY:
337354
$this->consumeToken(self::T_ARRAY);
338355
$this->consumeComments();
339356
$this->consumeToken(self::T_PARENTHESIS_OPEN);
340-
$this->parseArrayElements($array, self::T_PARENTHESIS_CLOSE);
357+
$this->parseArrayElements($array, self::T_PARENTHESIS_CLOSE, $static);
341358
$this->consumeToken(self::T_PARENTHESIS_CLOSE);
342359
break;
343360

344361
default:
345362
$this->consumeToken(self::T_SQUARED_BRACKET_OPEN);
346-
$this->parseArrayElements($array, self::T_SQUARED_BRACKET_CLOSE);
363+
$this->parseArrayElements($array, self::T_SQUARED_BRACKET_CLOSE, $static);
347364
$this->consumeToken(self::T_SQUARED_BRACKET_CLOSE);
348365
break;
349366
}

src/site/docx/changes.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
</properties>
1010

1111
<body>
12+
<release version="1.1.1"
13+
date=""
14+
description="">
15+
16+
<action date="2b23ff9" dev="mapi" type="fix" due-to="Karol" issue="95" system="github">
17+
PHP 5.4 array syntax is not supported in property initialization.
18+
</action>
19+
</release>
20+
1221
<release version="1.1.0"
1322
date="2012/09/12"
1423
description="This release closes a critical issue in the context

0 commit comments

Comments
 (0)