Skip to content

Commit

Permalink
Stop accepting strings as types
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikic committed May 20, 2023
1 parent a9dad5c commit c23976a
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 25 deletions.
5 changes: 2 additions & 3 deletions lib/PhpParser/Node/Expr/ArrowFunction.php
Expand Up @@ -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
Expand All @@ -46,8 +46,7 @@ public function __construct(array $subNodes, array $attributes = []) {
$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'] ?? [];
}
Expand Down
5 changes: 2 additions & 3 deletions lib/PhpParser/Node/Expr/Closure.php
Expand Up @@ -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:
Expand All @@ -50,8 +50,7 @@ public function __construct(array $subNodes = [], array $attributes = []) {
$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'] ?? [];
}
Expand Down
8 changes: 5 additions & 3 deletions lib/PhpParser/Node/NullableType.php
Expand Up @@ -2,19 +2,21 @@

namespace PhpParser\Node;

use PhpParser\Node;

class NullableType extends ComplexType {
/** @var Identifier|Name Type */
public $type;

/**
* 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 {
Expand Down
7 changes: 4 additions & 3 deletions lib/PhpParser/Node/Param.php
Expand Up @@ -3,6 +3,7 @@
namespace PhpParser\Node;

use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\NodeAbstract;

class Param extends NodeAbstract {
Expand All @@ -26,22 +27,22 @@ 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
* @param int $flags Optional visibility flags
* @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;
Expand Down
6 changes: 3 additions & 3 deletions lib/PhpParser/Node/Stmt/ClassConst.php
Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions lib/PhpParser/Node/Stmt/ClassMethod.php
Expand Up @@ -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:
Expand All @@ -69,8 +69,7 @@ public function __construct($name, array $subNodes = [], array $attributes = [])
$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'] ?? [];
}
Expand Down
5 changes: 2 additions & 3 deletions lib/PhpParser/Node/Stmt/Function_.php
Expand Up @@ -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:
Expand All @@ -45,8 +45,7 @@ public function __construct($name, array $subNodes = [], array $attributes = [])
$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'] ?? [];
}
Expand Down
6 changes: 3 additions & 3 deletions lib/PhpParser/Node/Stmt/Property.php
Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion test/PhpParser/BuilderHelpersTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -136,7 +137,7 @@ public function testNormalizeType() {
$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')]);
Expand Down

0 comments on commit c23976a

Please sign in to comment.