Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow null and false in union type for typed properties #551

Merged
merged 2 commits into from
Jul 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/main/php/PDepend/Source/Language/PHP/PHPParserVersion74.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,22 @@
*/
abstract class PHPParserVersion74 extends PHPParserVersion73
{
protected $possiblePropertyTypes = array(
Tokens::T_STRING,
Tokens::T_ARRAY,
Tokens::T_QUESTION_MARK,
Tokens::T_BACKSLASH,
Tokens::T_CALLABLE,
Tokens::T_SELF,
);

protected function parseUnknownDeclaration($tokenType, $modifiers)
{
/**
* Typed properties
* https://www.php.net/manual/en/migration74.new-features.php#migration74.new-features.core.typed-properties
*/
if (in_array($tokenType, array(
Tokens::T_STRING,
Tokens::T_ARRAY,
Tokens::T_QUESTION_MARK,
Tokens::T_BACKSLASH,
Tokens::T_CALLABLE,
Tokens::T_SELF,
))) {
if (in_array($tokenType, $this->possiblePropertyTypes, true)) {
$type = $this->parseTypeHint();
$declaration = $this->parseFieldDeclaration();
$declaration->prependChild($type);
Expand Down
11 changes: 11 additions & 0 deletions src/main/php/PDepend/Source/Language/PHP/PHPParserVersion80.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@
*/
abstract class PHPParserVersion80 extends PHPParserVersion74
{
protected $possiblePropertyTypes = array(
Tokens::T_STRING,
Tokens::T_ARRAY,
Tokens::T_QUESTION_MARK,
Tokens::T_BACKSLASH,
Tokens::T_CALLABLE,
Tokens::T_SELF,
Tokens::T_NULL,
Tokens::T_FALSE,
);

/**
* Will return <b>true</b> if the given <b>$tokenType</b> is a valid class
* name part.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
namespace PDepend\Source\Language\PHP;

use PDepend\AbstractTest;
use PDepend\Source\AST\ASTClass;
use PDepend\Source\AST\ASTFieldDeclaration;
use PDepend\Source\Builder\Builder;
use PDepend\Source\Tokenizer\Tokenizer;
use PDepend\Util\Cache\CacheDriver;
Expand Down Expand Up @@ -128,6 +130,45 @@ public function testTrailingCommaInParameterList()
$this->assertCount(2, $method->getParameters());
}

public function testNullableTypedProperties()
{
/** @var ASTClass $class */
$class = $this->getFirstClassForTestCase();
$children = $class->getChildren();

$this->assertTrue($children[0]->hasType());

/** @var array[] $declarations */
$declarations = array_map(function (ASTFieldDeclaration $child) {
$childChildren = $child->getChildren();

return array(
$child->hasType() ? $child->getType() : null,
$childChildren[1],
);
}, $children);

foreach (array(
array('null|int|float', '$number', 'PDepend\\Source\\AST\\ASTUnionType'),
) as $index => $expected) {
list($expectedType, $expectedVariable, $expectedTypeClass) = $expected;
list($type, $variable) = $declarations[$index];

$this->assertInstanceOf(
$expectedTypeClass,
$type,
"Wrong type for $expectedType $expectedVariable"
);
$this->assertSame(ltrim($expectedType, '?'), $type->getImage());
$this->assertInstanceOf(
'PDepend\\Source\\AST\\ASTVariableDeclarator',
$variable,
"Wrong variable for $expectedType $expectedVariable"
);
$this->assertSame($expectedVariable, $variable->getImage());
}
}

/**
* @param \PDepend\Source\Tokenizer\Tokenizer $tokenizer
* @param \PDepend\Source\Builder\Builder $builder
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
class User {
public null|int|float $number;
}