Skip to content

Commit

Permalink
Closes #153: Support for new finally keyword implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Pichler committed Apr 23, 2014
1 parent 337b381 commit e536e7a
Show file tree
Hide file tree
Showing 15 changed files with 344 additions and 6 deletions.
17 changes: 15 additions & 2 deletions src/main/php/PDepend/Source/AST/ASTFinallyStatement.php
Expand Up @@ -42,13 +42,26 @@

namespace PDepend\Source\AST;

use PDepend\Source\ASTVisitor\ASTVisitor;

/**
* This node class represents a finally-statement.
*
* @copyright 2008-2013 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
*/
class ASTFinallyStatement extends \PDepend\Source\AST\ASTStatement
class ASTFinallyStatement extends ASTStatement
{

/**
* Accept method of the visitor design pattern. This method will be called
* by a visitor during tree traversal.
*
* @param \PDepend\Source\ASTVisitor\ASTVisitor $visitor
* @param mixed $data
* @return mixed
*/
public function accept(ASTVisitor $visitor, $data = null)
{
return $visitor->visitFinallyStatement($this, $data);
}
}
8 changes: 8 additions & 0 deletions src/main/php/PDepend/Source/Builder/Builder.php
Expand Up @@ -630,6 +630,14 @@ public function buildAstSwitchLabel($image);
*/
public function buildAstCatchStatement($image);

/**
* Builds a new finally-statement node.
*
* @return \PDepend\Source\AST\ASTFinallyStatement
* @since 2.0.0
*/
public function buildAstFinallyStatement();

/**
* Builds a new if statement node.
*
Expand Down
37 changes: 35 additions & 2 deletions src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php
Expand Up @@ -3037,10 +3037,24 @@ private function parseTryStatement()
$stmt = $this->builder->buildAstTryStatement($token->image);
$stmt->addChild($this->parseRegularScope());

do {
$this->consumeComments();

if (false === in_array($this->tokenizer->peek(), array(Tokens::T_CATCH, Tokens::T_FINALLY))) {
throw new UnexpectedTokenException(
$this->tokenizer->next(),
$this->tokenizer->getSourceFile()
);
}

while ($this->tokenizer->peek() === Tokens::T_CATCH) {
$stmt->addChild($this->parseCatchStatement());
$this->consumeComments();
} while ($this->tokenizer->peek() === Tokens::T_CATCH);
}

while ($this->tokenizer->peek() === Tokens::T_FINALLY) {
$stmt->addChild($this->parseFinallyStatement());
$this->consumeComments();
}

return $this->setNodePositionsAndReturn($stmt);
}
Expand Down Expand Up @@ -3182,6 +3196,25 @@ private function parseCatchStatement()
return $this->setNodePositionsAndReturn($catch);
}

/**
* This method parses a finally-statement.
*
* @return \PDepend\Source\AST\ASTFinallyStatement
* @since 2.0.0
*/
private function parseFinallyStatement()
{
$this->tokenStack->push();
$this->consumeComments();

$token = $this->consumeToken(Tokens::T_FINALLY);

$finally = $this->builder->buildAstFinallyStatement($token->image);
$finally->addChild($this->parseRegularScope());

return $this->setNodePositionsAndReturn($finally);
}

/**
* This method parses a single if-statement node.
*
Expand Down
14 changes: 12 additions & 2 deletions src/main/php/PDepend/Source/Language/PHP/PHPBuilder.php
Expand Up @@ -1068,8 +1068,7 @@ public function buildAstUnsetStatement()
/**
* Builds a new catch-statement node.
*
* @param string $image The source image of this statement.
*
* @param string $image
* @return \PDepend\Source\AST\ASTCatchStatement
* @since 0.9.8
*/
Expand All @@ -1078,6 +1077,17 @@ public function buildAstCatchStatement($image)
return $this->buildAstNodeInstance('ASTCatchStatement', $image);
}

/**
* Builds a new finally-statement node.
*
* @return \PDepend\Source\AST\ASTFinallyStatement
* @since 2.0.0
*/
public function buildAstFinallyStatement()
{
return $this->buildAstNodeInstance('ASTFinallyStatement', 'finally');
}

/**
* Builds a new if statement node.
*
Expand Down
170 changes: 170 additions & 0 deletions src/test/php/PDepend/Source/AST/ASTFinallyStatementTest.php
@@ -0,0 +1,170 @@
<?php
/**
* This file is part of PDepend.
*
* PHP Version 5
*
* Copyright (c) 2008-2013, Manuel Pichler <mapi@pdepend.org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Manuel Pichler nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @copyright 2008-2013 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
*/

namespace PDepend\Source\AST;

/**
* Test case for the {@link \PDepend\Source\AST\ASTFinallyStatement} class.
*
* @copyright 2008-2013 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
*
* @covers \PDepend\Source\Language\PHP\AbstractPHPParser
* @covers \PDepend\Source\AST\ASTFinallyStatement
* @group unittest
*/
class ASTFinallyStatementTest extends ASTNodeTest
{
/**
* Tests the start line value.
*
* @return void
*/
public function testFinallyStatementHasExpectedStartLine()
{
$stmt = $this->_getFirstFinallyStatementInFunction(__METHOD__);
$this->assertEquals(8, $stmt->getStartLine());
}

/**
* Tests the start column value.
*
* @return void
*/
public function testFinallyStatementHasExpectedStartColumn()
{
$stmt = $this->_getFirstFinallyStatementInFunction(__METHOD__);
$this->assertEquals(7, $stmt->getStartColumn());
}

/**
* Tests the end line value.
*
* @return void
*/
public function testFinallyStatementHasExpectedEndLine()
{
$stmt = $this->_getFirstFinallyStatementInFunction(__METHOD__);
$this->assertEquals(10, $stmt->getEndLine());
}

/**
* Tests the end column value.
*
* @return void
*/
public function testFinallyStatementHasExpectedEndColumn()
{
$stmt = $this->_getFirstFinallyStatementInFunction(__METHOD__);
$this->assertEquals(5, $stmt->getEndColumn());
}

/**
* testOnlyFinallyStatementHasExpectedStartLine
*
* @return void
*/
public function testOnlyFinallyStatementHasExpectedStartLine()
{
$stmt = $this->_getFirstFinallyStatementInFunction(__METHOD__);
$this->assertEquals(6, $stmt->getStartLine());
}

/**
* testOnlyFinallyStatementHasExpectedStartColumn
*
* @return void
*/
public function testOnlyFinallyStatementHasExpectedStartColumn()
{
$stmt = $this->_getFirstFinallyStatementInFunction(__METHOD__);
$this->assertEquals(7, $stmt->getStartColumn());
}

/**
* testOnlyFinallyStatementHasExpectedEndLine
*
* @return void
*/
public function testOnlyFinallyStatementHasExpectedEndLine()
{
$stmt = $this->_getFirstFinallyStatementInFunction(__METHOD__);
$this->assertEquals(8, $stmt->getEndLine());
}

/**
* testOnlyFinallyStatementHasExpectedEndColumn
*
* @return void
*/
public function testOnlyFinallyStatementHasExpectedEndColumn()
{
$stmt = $this->_getFirstFinallyStatementInFunction(__METHOD__);
$this->assertEquals(5, $stmt->getEndColumn());
}

/**
* testThirdChildOfFinallyStatementIsScopeStatement
*
* @return void
*/
public function testChildOfFinallyStatementIsScopeStatement()
{
$stmt = $this->_getFirstFinallyStatementInFunction(__METHOD__);
$this->assertInstanceOf('PDepend\\Source\\AST\\ASTScopeStatement', $stmt->getChild(0));
}

/**
* Returns a node instance for the currently executed test case.
*
* @param string $testCase Name of the calling test case.
*
* @return \PDepend\Source\AST\ASTFinallyStatement
*/
private function _getFirstFinallyStatementInFunction($testCase)
{
return $this->getFirstNodeOfTypeInFunction(
$testCase,
'PDepend\\Source\\AST\\ASTFinallyStatement'
);
}
}
13 changes: 13 additions & 0 deletions src/test/php/PDepend/Source/Language/PHP/PHPBuilderTest.php
Expand Up @@ -1354,6 +1354,19 @@ public function testBuildASTCatchStatementReturnsExpectedType()
);
}

/**
* testBuildASTFinallyStatementReturnsExpectedType
*
* @return void
*/
public function testBuildASTFinallyStatementReturnsExpectedType()
{
$this->assertInstanceOf(
'PDepend\\Source\\AST\\ASTFinallyStatement',
$this->createBuilder()->buildAstFinallyStatement()
);
}

/**
* testBuildASTDeclareStatementReturnsExpectedType
*
Expand Down
@@ -0,0 +1,11 @@
<?php
function testChildOfFinallyStatementIsScopeStatement()
{
try {
fooBar();
} catch (Exception $e) {
// Hello World
} finally {
echo "FOO";
}
}
@@ -0,0 +1,11 @@
<?php
function testFinallyStatementHasExpectedEndColumn()
{
try {
throw Exception();
} catch (Exception $e) {

} finally {
echo "FOO";
}
}
@@ -0,0 +1,11 @@
<?php
function testFinallyStatementHasExpectedEndLine()
{
try {
throw Exception();
} catch (Exception $e) {

} finally {
echo "FOO";
}
}
@@ -0,0 +1,11 @@
<?php
function testFinallyStatementHasExpectedStartColumn()
{
try {
throw Exception();
} catch (Exception $e) {

} finally {
echo "FOO";
}
}
@@ -0,0 +1,11 @@
<?php
function testFinallyStatementHasExpectedStartLine()
{
try {
throw Exception();
} catch (Exception $e) {

} finally {
echo "FOO";
}
}
@@ -0,0 +1,9 @@
<?php
function testFinallyStatementHasExpectedEndColumn()
{
try {
throw Exception();
} finally {
echo "FOO";
}
}
@@ -0,0 +1,9 @@
<?php
function testFinallyStatementHasExpectedEndLine()
{
try {
throw Exception();
} finally {
echo "FOO";
}
}

0 comments on commit e536e7a

Please sign in to comment.