Skip to content

Commit

Permalink
Support for constants with array values implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Pichler committed Sep 20, 2015
1 parent 5b48bf3 commit 1209b0e
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 36 deletions.
13 changes: 7 additions & 6 deletions src/main/php/PDepend/Source/AST/ASTConstant.php
Expand Up @@ -43,26 +43,27 @@

namespace PDepend\Source\AST;

use PDepend\Source\ASTVisitor\ASTVisitor;

/**
* This class represents a constant node.
*
* @copyright 2008-2015 Manuel Pichler. All rights reserved.
* @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);
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/php/PDepend/Source/AST/ASTConstantDeclarator.php
Expand Up @@ -42,6 +42,8 @@

namespace PDepend\Source\AST;

use PDepend\Source\ASTVisitor\ASTVisitor;

/**
* This class represents a single constant declarator within a constant
* definition.
Expand Down Expand Up @@ -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 <b>null</b>.
Expand Down Expand Up @@ -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);
}
Expand Down
18 changes: 15 additions & 3 deletions src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php
Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/php/PDepend/Source/Language/PHP/PHPParserGeneric.php
Expand Up @@ -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();
}


}
Expand Up @@ -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.
*
Expand Down
Expand Up @@ -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);
}
}
@@ -0,0 +1,9 @@
<?php
class testConstantSupportForArrayWithKeyValuePairs
{
const FOO = [
'January' => 'January',
'February' => 'February',
// …
];
}
@@ -0,0 +1,8 @@
<?php
class testConstantSupportForArrayWithSelfReferenceInClass
{
const A = 42;
const B = 23;

const C = [self::A, self::B];
}
@@ -0,0 +1,8 @@
<?php
interface testConstantSupportForArrayWithSelfReferenceInInterface
{
const A = 42;
const B = 23;

const C = [self::A, self::B];
}
@@ -0,0 +1,10 @@
<?php
class testConstantSupportForScalarArrayValues
{
const FOO = [
'string',
'int',
'float',
'bool'
];
}

This file was deleted.

0 comments on commit 1209b0e

Please sign in to comment.