Skip to content

Commit

Permalink
- Implemented #19874825: Implement the short array syntax introduced …
Browse files Browse the repository at this point in the history
…with PHP 5.4

[Story19874825 state:finished]
  • Loading branch information
manuelpichler committed Nov 27, 2011
1 parent 0d8b3d9 commit 338bca2
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 32 deletions.
36 changes: 4 additions & 32 deletions src/main/php/PHP/Depend/Parser.php
Expand Up @@ -4433,44 +4433,17 @@ private function _parseArray()
* @return boolean
* @since 0.11.0
*/
protected function isArrayStartDelimiter()
{
switch ($this->tokenizer->peek()) {
case self::T_ARRAY:
case self::T_SQUARED_BRACKET_OPEN:
return true;
}
return false;
}
protected abstract function isArrayStartDelimiter();

/**
* Parses a php array declaration.
*
* @param PHP_Depend_Code_ASTArray $array
* @param PHP_Depend_Code_ASTArray $array The context array node.
*
* @return PHP_Depend_Code_ASTArray
* @since 0.11.0
*/
protected function parseArray(PHP_Depend_Code_ASTArray $array)
{
switch ($this->tokenizer->peek()) {

case self::T_ARRAY:
$this->consumeToken(self::T_ARRAY);
$this->consumeComments();
$this->consumeToken(self::T_PARENTHESIS_OPEN);
$this->parseArrayElements($array, self::T_PARENTHESIS_CLOSE);
$this->consumeToken(self::T_PARENTHESIS_CLOSE);
break;

default:
$this->consumeToken(self::T_SQUARED_BRACKET_OPEN);
$this->parseArrayElements($array, self::T_SQUARED_BRACKET_CLOSE);
$this->consumeToken(self::T_SQUARED_BRACKET_CLOSE);
break;
}
return $array;
}
protected abstract function parseArray(PHP_Depend_Code_ASTArray $array);

/**
* Parses all elements in an array.
Expand All @@ -4484,8 +4457,7 @@ protected function parseArray(PHP_Depend_Code_ASTArray $array)
protected function parseArrayElements(
PHP_Depend_Code_ASTArray $array,
$endDelimiter
)
{
) {
$this->consumeComments();
while ($endDelimiter !== $this->tokenizer->peek()) {
$array->addChild($this->parseArrayElement());
Expand Down
47 changes: 47 additions & 0 deletions src/main/php/PHP/Depend/Parser/VersionAllParser.php
Expand Up @@ -270,4 +270,51 @@ protected function parsePostfixIdentifier()
}
return $this->parseOptionalIndexExpression($node);
}

/**
* Tests if the next token is a valid array start delimiter in the supported
* PHP version.
*
* @return boolean
* @since 0.11.0
*/
protected function isArrayStartDelimiter()
{
switch ($this->tokenizer->peek()) {

case self::T_ARRAY:
case self::T_SQUARED_BRACKET_OPEN:
return true;
}
return false;
}

/**
* Parses a php array declaration.
*
* @param PHP_Depend_Code_ASTArray $array The context array node.
*
* @return PHP_Depend_Code_ASTArray
* @since 0.11.0
*/
protected function parseArray(PHP_Depend_Code_ASTArray $array)
{
switch ($this->tokenizer->peek()) {

case self::T_ARRAY:
$this->consumeToken(self::T_ARRAY);
$this->consumeComments();
$this->consumeToken(self::T_PARENTHESIS_OPEN);
$this->parseArrayElements($array, self::T_PARENTHESIS_CLOSE);
$this->consumeToken(self::T_PARENTHESIS_CLOSE);
break;

default:
$this->consumeToken(self::T_SQUARED_BRACKET_OPEN);
$this->parseArrayElements($array, self::T_SQUARED_BRACKET_CLOSE);
$this->consumeToken(self::T_SQUARED_BRACKET_CLOSE);
break;
}
return $array;
}
}
30 changes: 30 additions & 0 deletions src/test/php/PHP/Depend/Parser/VersionAllParserTest.php
Expand Up @@ -478,6 +478,36 @@ public function testParserThrowsExpectedExceptionForTokenStreamEnd()
self::parseCodeResourceForTest();
}

/**
* testParserHandlesRegularArraySyntax
*
* @return void
* @since 0.11.0
*/
public function testParserHandlesRegularArraySyntax()
{
$this->assertInstanceOf(
PHP_Depend_Code_ASTArray::CLAZZ,
$this->getFirstMethodForTestCase()
->getFirstChildOfType(PHP_Depend_Code_ASTArray::CLAZZ)
);
}

/**
* testParserHandlesShortArraySyntax
*
* @return void
* @since 0.11.0
*/
public function testParserHandlesShortArraySyntax()
{
$this->assertInstanceOf(
PHP_Depend_Code_ASTArray::CLAZZ,
$this->getFirstMethodForTestCase()
->getFirstChildOfType(PHP_Depend_Code_ASTArray::CLAZZ)
);
}

/**
* Returns the first class or interface that could be found in the code under
* test for the calling test case.
Expand Down
@@ -0,0 +1,18 @@
<?php
class testParserHandlesRegularArraySyntaxClass
{
function testParserHandlesRegularArraySyntax($x)
{
return array(
1 => 2,
2 => array(
new stdClass(),
'foo' => &$x,
'bar' => $x,
array(
'a', 'b', 'c', new stdClass()
)
)
);
}
}
@@ -0,0 +1,18 @@
<?php
class testParserHandlesShortArraySyntaxClass
{
function testParserHandlesShortArraySyntax()
{
return [
1 => 2,
2 => [
new stdClass(),
'foo' => &$x,
'bar' => $x,
[
'a', 'b', 'c', new stdClass()
]
]
];
}
}

0 comments on commit 338bca2

Please sign in to comment.