Skip to content

Commit

Permalink
Merge pull request pdepend#342 from KacerCZ/php71-types
Browse files Browse the repository at this point in the history
Added support for PHP 7.1 types - iterable and void
  • Loading branch information
Emir Beganović authored Nov 30, 2017
2 parents d885930 + b4cde60 commit bb77500
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 3 deletions.
94 changes: 94 additions & 0 deletions src/main/php/PDepend/Source/AST/ASTTypeIterable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* This file is part of PDepend.
*
* PHP Version 5
*
* Copyright (c) 2008-2017 Manuel Pichler <[email protected]>.
* 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-2017 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @since 2.5.1
*/

namespace PDepend\Source\AST;

/**
* This class represents an array type node.
*
* @copyright 2008-2017 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @since 2.5.1
*/
class ASTTypeIterable extends ASTType
{
/**
* The visual image for this node type.
*/
const IMAGE = 'iterable';

/**
* @return string
*/
public function getImage()
{
return self::IMAGE;
}

/**
* This method will return <b>true</b> when the underlying type is an array.
* For this concrete type implementation the returned value will be always
* <b>true</b>.
*
* @return boolean
*/
public function isArray()
{
return true;
}

/**
* 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
*
* @return mixed
* @since 0.9.12
*/
public function accept(\PDepend\Source\ASTVisitor\ASTVisitor $visitor, $data = null)
{
return $visitor->visitTypeIterable($this, $data);
}
}
8 changes: 8 additions & 0 deletions src/main/php/PDepend/Source/Builder/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,14 @@ public function buildAstTypeArray();
*/
public function buildAstTypeCallable();

/**
* Builds a new node for the iterable type.
*
* @return \PDepend\Source\AST\ASTTypeIterable
* @since 2.5.1
*/
public function buildAstTypeIterable();

/**
* Builds a new primitive type node.
*
Expand Down
13 changes: 12 additions & 1 deletion src/main/php/PDepend/Source/Language/PHP/PHPBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,18 @@ public function buildAstTypeCallable()
{
return $this->buildAstNodeInstance('ASTTypeCallable');
}


/**
* Builds a new node for the iterable type.
*
* @return \PDepend\Source\AST\ASTTypeIterable
* @since 2.5.1
*/
public function buildAstTypeIterable()
{
return $this->buildAstNodeInstance('ASTTypeIterable');
}

/**
* Builds a new primitive type node.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ protected function parseTypeHint()
* @param string $image
* @return boolean
*/
private function isScalarOrCallableTypeHint($image)
protected function isScalarOrCallableTypeHint($image)
{
switch (strtolower($image)) {
case 'int':
Expand All @@ -268,7 +268,7 @@ private function isScalarOrCallableTypeHint($image)
* @param string $image
* @return \PDepend\Source\AST\ASTType
*/
private function parseScalarOrCallableTypeHint($image)
protected function parseScalarOrCallableTypeHint($image)
{
switch (strtolower($image)) {
case 'int':
Expand Down
36 changes: 36 additions & 0 deletions src/main/php/PDepend/Source/Language/PHP/PHPParserVersion71.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,40 @@ protected function parseUnknownDeclaration($tokenType, $modifiers)
}
return parent::parseUnknownDeclaration($tokenType, $modifiers);
}

/**
* Tests if the given image is a PHP 7 type hint.
*
* @param string $image
* @return boolean
*/
protected function isScalarOrCallableTypeHint($image)
{
switch (strtolower($image)) {
case 'iterable':
case 'void':
return true;
}

return parent::isScalarOrCallableTypeHint($image);
}

/**
* Parses a scalar type hint or a callable type hint.
*
* @param string $image
* @return \PDepend\Source\AST\ASTType
*/
protected function parseScalarOrCallableTypeHint($image)
{
switch (strtolower($image)) {
case 'void':
return $this->builder->buildAstScalarType($image);
case 'iterable':
return $this->builder->buildAstTypeIterable();
}

return parent::parseScalarOrCallableTypeHint($image);
}

}
157 changes: 157 additions & 0 deletions src/test/php/PDepend/Source/AST/ASTTypeIterableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php
/**
* This file is part of PDepend.
*
* PHP Version 5
*
* Copyright (c) 2008-2017 Manuel Pichler <[email protected]>.
* 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-2017 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\ASTTypeIterable} class.
*
* @copyright 2008-2017 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\ASTTypeIterable
* @group unittest
*/
class ASTTypeIterableTest extends ASTNodeTest
{
/**
* testIterableType
*
* @return \PDepend\Source\AST\ASTTypeIterable
* @since 2.5.1
*/
public function testIterableType()
{
$type = $this->_getFirstArrayTypeInFunction();
$this->assertInstanceOf('PDepend\\Source\\AST\\ASTTypeIterable', $type);

return $type;
}

/**
* testIterableTypeHasExpectedStartLine
*
* @param \PDepend\Source\AST\ASTTypeIterable $type
*
* @return void
* @depends testIterableType
*/
public function testIterableTypeHasExpectedStartLine($type)
{
$this->assertEquals(2, $type->getStartLine());
}

/**
* testIterableTypeHasExpectedStartColumn
*
* @param \PDepend\Source\AST\ASTTypeIterable $type
*
* @return void
* @depends testIterableType
*/
public function testIterableTypeHasExpectedStartColumn($type)
{
$this->assertEquals(24, $type->getStartColumn());
}

/**
* testIterableTypeHasExpectedEndLine
*
* @param \PDepend\Source\AST\ASTTypeIterable $type
*
* @return void
* @depends testIterableType
*/
public function testIterableTypeHasExpectedEndLine($type)
{
$this->assertEquals(2, $type->getEndLine());
}

/**
* testIterableTypeHasExpectedEndColumn
*
* @param \PDepend\Source\AST\ASTTypeIterable $type
*
* @return void
* @depends testIterableType
*/
public function testIterableTypeHasExpectedEndColumn($type)
{
$this->assertEquals(31, $type->getEndColumn());
}

/**
* testIsArrayReturnsTrue
*
* @return void
*/
public function testIsArrayReturnsTrue()
{
$type = new \PDepend\Source\AST\ASTTypeIterable();
$this->assertTrue($type->isArray());
}

/**
* testIsPrimitiveReturnsFalse
*
* @return void
*/
public function testIsPrimitiveReturnsFalse()
{
$type = new \PDepend\Source\AST\ASTTypeIterable();
$this->assertFalse($type->isScalar());
}

/**
* Returns a node instance for the currently executed test case.
*
* @return \PDepend\Source\AST\ASTTypeIterable
*/
private function _getFirstArrayTypeInFunction()
{
return $this->getFirstNodeOfTypeInFunction(
$this->getCallingTestMethod(),
'PDepend\\Source\\AST\\ASTTypeIterable'
);
}
}
14 changes: 14 additions & 0 deletions src/test/php/PDepend/Source/Language/PHP/PHPBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,20 @@ public function testBuildASTTypeCallableReturnsExpectedType()
);
}

/**
* testBuildASTTypeCallableReturnsExpectedType
*
* @return void
* @since 1.0.0
*/
public function testBuildASTTypeIterableReturnsExpectedType()
{
$this->assertInstanceOf(
'PDepend\\Source\\AST\\ASTTypeIterable',
$this->createBuilder()->buildAstTypeIterable()
);
}

/**
* testBuildASTHeredocReturnsExpectedType
*
Expand Down
Loading

0 comments on commit bb77500

Please sign in to comment.