Stop accepting strings as types

For types the use of a string is ambiguous -- it could be either
an Identifier or a Name. Don't guess.

Retain the implicit promotion to Identifier in places where only
Identifier is legal, e.g. various symbol names.
This commit is contained in:
Nikita Popov 2023-05-20 22:12:04 +02:00
parent a9dad5c54e
commit c23976a299
9 changed files with 25 additions and 25 deletions

View File

@ -30,7 +30,7 @@ class ArrowFunction extends Expr implements FunctionLike {
* static?: bool,
* byRef?: bool,
* params?: Node\Param[],
* returnType?: null|string|Node\Identifier|Node\Name|Node\ComplexType,
* returnType?: null|Node\Identifier|Node\Name|Node\ComplexType,
* attrGroups?: Node\AttributeGroup[]
* } $subNodes Array of the following subnodes:
* 'expr' : Expression body
@ -46,8 +46,7 @@ class ArrowFunction extends Expr implements FunctionLike {
$this->static = $subNodes['static'] ?? false;
$this->byRef = $subNodes['byRef'] ?? false;
$this->params = $subNodes['params'] ?? [];
$returnType = $subNodes['returnType'] ?? null;
$this->returnType = \is_string($returnType) ? new Node\Identifier($returnType) : $returnType;
$this->returnType = $subNodes['returnType'] ?? null;
$this->expr = $subNodes['expr'];
$this->attrGroups = $subNodes['attrGroups'] ?? [];
}

View File

@ -31,7 +31,7 @@ class Closure extends Expr implements FunctionLike {
* byRef?: bool,
* params?: Node\Param[],
* uses?: ClosureUse[],
* returnType?: null|string|Node\Identifier|Node\Name|Node\ComplexType,
* returnType?: null|Node\Identifier|Node\Name|Node\ComplexType,
* stmts?: Node\Stmt[],
* attrGroups?: Node\AttributeGroup[],
* } $subNodes Array of the following optional subnodes:
@ -50,8 +50,7 @@ class Closure extends Expr implements FunctionLike {
$this->byRef = $subNodes['byRef'] ?? false;
$this->params = $subNodes['params'] ?? [];
$this->uses = $subNodes['uses'] ?? [];
$returnType = $subNodes['returnType'] ?? null;
$this->returnType = \is_string($returnType) ? new Node\Identifier($returnType) : $returnType;
$this->returnType = $subNodes['returnType'] ?? null;
$this->stmts = $subNodes['stmts'] ?? [];
$this->attrGroups = $subNodes['attrGroups'] ?? [];
}

View File

@ -2,6 +2,8 @@
namespace PhpParser\Node;
use PhpParser\Node;
class NullableType extends ComplexType {
/** @var Identifier|Name Type */
public $type;
@ -9,12 +11,12 @@ class NullableType extends ComplexType {
/**
* Constructs a nullable type (wrapping another type).
*
* @param string|Identifier|Name $type Type
* @param Identifier|Name $type Type
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct($type, array $attributes = []) {
public function __construct(Node $type, array $attributes = []) {
$this->attributes = $attributes;
$this->type = \is_string($type) ? new Identifier($type) : $type;
$this->type = $type;
}
public function getSubNodeNames(): array {

View File

@ -3,6 +3,7 @@
namespace PhpParser\Node;
use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\NodeAbstract;
class Param extends NodeAbstract {
@ -26,7 +27,7 @@ class Param extends NodeAbstract {
*
* @param Expr\Variable|Expr\Error $var Parameter variable
* @param null|Expr $default Default value
* @param null|string|Identifier|Name|ComplexType $type Type declaration
* @param null|Identifier|Name|ComplexType $type Type declaration
* @param bool $byRef Whether is passed by reference
* @param bool $variadic Whether this is a variadic argument
* @param array<string, mixed> $attributes Additional attributes
@ -34,14 +35,14 @@ class Param extends NodeAbstract {
* @param list<AttributeGroup> $attrGroups PHP attribute groups
*/
public function __construct(
$var, ?Expr $default = null, $type = null,
$var, ?Expr $default = null, ?Node $type = null,
bool $byRef = false, bool $variadic = false,
array $attributes = [],
int $flags = 0,
array $attrGroups = []
) {
$this->attributes = $attributes;
$this->type = \is_string($type) ? new Identifier($type) : $type;
$this->type = $type;
$this->byRef = $byRef;
$this->variadic = $variadic;
$this->var = $var;

View File

@ -22,20 +22,20 @@ class ClassConst extends Node\Stmt {
* @param int $flags Modifiers
* @param array<string, mixed> $attributes Additional attributes
* @param list<Node\AttributeGroup> $attrGroups PHP attribute groups
* @param null|string|Node\Identifier|Node\Name|Node\ComplexType $type Type declaration
* @param null|Node\Identifier|Node\Name|Node\ComplexType $type Type declaration
*/
public function __construct(
array $consts,
int $flags = 0,
array $attributes = [],
array $attrGroups = [],
$type = null
?Node $type = null
) {
$this->attributes = $attributes;
$this->flags = $flags;
$this->consts = $consts;
$this->attrGroups = $attrGroups;
$this->type = \is_string($type) ? new Node\Identifier($type) : $type;
$this->type = $type;
}
public function getSubNodeNames(): array {

View File

@ -51,7 +51,7 @@ class ClassMethod extends Node\Stmt implements FunctionLike {
* flags?: int,
* byRef?: bool,
* params?: Node\Param[],
* returnType?: null|string|Node\Identifier|Node\Name|Node\ComplexType,
* returnType?: null|Node\Identifier|Node\Name|Node\ComplexType,
* stmts?: Node\Stmt[]|null,
* attrGroups?: Node\AttributeGroup[],
* } $subNodes Array of the following optional subnodes:
@ -69,8 +69,7 @@ class ClassMethod extends Node\Stmt implements FunctionLike {
$this->byRef = $subNodes['byRef'] ?? false;
$this->name = \is_string($name) ? new Node\Identifier($name) : $name;
$this->params = $subNodes['params'] ?? [];
$returnType = $subNodes['returnType'] ?? null;
$this->returnType = \is_string($returnType) ? new Node\Identifier($returnType) : $returnType;
$this->returnType = $subNodes['returnType'] ?? null;
$this->stmts = array_key_exists('stmts', $subNodes) ? $subNodes['stmts'] : [];
$this->attrGroups = $subNodes['attrGroups'] ?? [];
}

View File

@ -29,7 +29,7 @@ class Function_ extends Node\Stmt implements FunctionLike {
* @param array{
* byRef?: bool,
* params?: Node\Param[],
* returnType?: null|string|Node\Identifier|Node\Name|Node\ComplexType,
* returnType?: null|Node\Identifier|Node\Name|Node\ComplexType,
* stmts?: Node\Stmt[],
* attrGroups?: Node\AttributeGroup[],
* } $subNodes Array of the following optional subnodes:
@ -45,8 +45,7 @@ class Function_ extends Node\Stmt implements FunctionLike {
$this->byRef = $subNodes['byRef'] ?? false;
$this->name = \is_string($name) ? new Node\Identifier($name) : $name;
$this->params = $subNodes['params'] ?? [];
$returnType = $subNodes['returnType'] ?? null;
$this->returnType = \is_string($returnType) ? new Node\Identifier($returnType) : $returnType;
$this->returnType = $subNodes['returnType'] ?? null;
$this->stmts = $subNodes['stmts'] ?? [];
$this->attrGroups = $subNodes['attrGroups'] ?? [];
}

View File

@ -25,14 +25,14 @@ class Property extends Node\Stmt {
* @param int $flags Modifiers
* @param PropertyItem[] $props Properties
* @param array<string, mixed> $attributes Additional attributes
* @param null|string|Identifier|Name|ComplexType $type Type declaration
* @param null|Identifier|Name|ComplexType $type Type declaration
* @param Node\AttributeGroup[] $attrGroups PHP attribute groups
*/
public function __construct(int $flags, array $props, array $attributes = [], $type = null, array $attrGroups = []) {
public function __construct(int $flags, array $props, array $attributes = [], ?Node $type = null, array $attrGroups = []) {
$this->attributes = $attributes;
$this->flags = $flags;
$this->props = $props;
$this->type = \is_string($type) ? new Identifier($type) : $type;
$this->type = $type;
$this->attrGroups = $attrGroups;
}

View File

@ -3,6 +3,7 @@
namespace PhpParser;
use PhpParser\Builder\Class_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt;
use PhpParser\Node\Expr;
@ -136,7 +137,7 @@ class BuilderHelpersTest extends \PHPUnit\Framework\TestCase {
$intName = new Node\Name('int');
$this->assertSame($intName, BuilderHelpers::normalizeType($intName));
$intNullable = new Node\NullableType('int');
$intNullable = new Node\NullableType(new Identifier('int'));
$this->assertSame($intNullable, BuilderHelpers::normalizeType($intNullable));
$unionType = new Node\UnionType([new Node\Identifier('int'), new Node\Identifier('string')]);