Skip to content

Commit

Permalink
Use more precise Use_::TYPE_* types (#945)
Browse files Browse the repository at this point in the history
For better static analysis support in consuming projects.
  • Loading branch information
staabm committed Sep 14, 2023
1 parent 5da5231 commit 263fa80
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
6 changes: 3 additions & 3 deletions lib/PhpParser/Builder/Use_.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
class Use_ implements Builder {
/** @var Node\Name */
protected Node\Name $name;
/** @var int */
/** @var Stmt\Use_::TYPE_* */
protected int $type;
/** @var string|null */
protected ?string $alias = null;

/**
* Creates a name use (alias) builder.
*
* @param Node\Name|string $name Name of the entity (namespace, class, function, constant) to alias
* @param int $type One of the Stmt\Use_::TYPE_* constants
* @param Node\Name|string $name Name of the entity (namespace, class, function, constant) to alias
* @param Stmt\Use_::TYPE_* $type One of the Stmt\Use_::TYPE_* constants
*/
public function __construct($name, int $type) {
$this->name = BuilderHelpers::normalizeName($name);
Expand Down
18 changes: 9 additions & 9 deletions lib/PhpParser/NameContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public function startNamespace(?Name $namespace = null): void {
/**
* Add an alias / import.
*
* @param Name $name Original name
* @param string $aliasName Aliased name
* @param int $type One of Stmt\Use_::TYPE_*
* @param Name $name Original name
* @param string $aliasName Aliased name
* @param Stmt\Use_::TYPE_* $type One of Stmt\Use_::TYPE_*
* @param array<string, mixed> $errorAttrs Attributes to use to report an error
*/
public function addAlias(Name $name, string $aliasName, int $type, array $errorAttrs = []): void {
Expand Down Expand Up @@ -93,8 +93,8 @@ public function getNamespace(): ?Name {
/**
* Get resolved name.
*
* @param Name $name Name to resolve
* @param int $type One of Stmt\Use_::TYPE_{FUNCTION|CONSTANT}
* @param Name $name Name to resolve
* @param Stmt\Use_::TYPE_* $type One of Stmt\Use_::TYPE_{FUNCTION|CONSTANT}
*
* @return null|Name Resolved name, or null if static resolution is not possible
*/
Expand Down Expand Up @@ -148,8 +148,8 @@ public function getResolvedClassName(Name $name): Name {
/**
* Get possible ways of writing a fully qualified name (e.g., by making use of aliases).
*
* @param string $name Fully-qualified name (without leading namespace separator)
* @param int $type One of Stmt\Use_::TYPE_*
* @param string $name Fully-qualified name (without leading namespace separator)
* @param Stmt\Use_::TYPE_* $type One of Stmt\Use_::TYPE_*
*
* @return Name[] Possible representations of the name
*/
Expand Down Expand Up @@ -204,8 +204,8 @@ public function getPossibleNames(string $name, int $type): array {
/**
* Get shortest representation of this fully-qualified name.
*
* @param string $name Fully-qualified name (without leading namespace separator)
* @param int $type One of Stmt\Use_::TYPE_*
* @param string $name Fully-qualified name (without leading namespace separator)
* @param Stmt\Use_::TYPE_* $type One of Stmt\Use_::TYPE_*
*
* @return Name Shortest representation
*/
Expand Down
10 changes: 6 additions & 4 deletions lib/PhpParser/Node/Stmt/GroupUse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use PhpParser\Node\UseItem;

class GroupUse extends Stmt {
/** @var int Type of group use */
/**
* @var Use_::TYPE_* Type of group use
*/
public int $type;
/** @var Name Prefix for uses */
public Name $prefix;
Expand All @@ -17,9 +19,9 @@ class GroupUse extends Stmt {
/**
* Constructs a group use node.
*
* @param Name $prefix Prefix for uses
* @param UseItem[] $uses Uses
* @param int $type Type of group use
* @param Name $prefix Prefix for uses
* @param UseItem[] $uses Uses
* @param Use_::TYPE_* $type Type of group use
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct(Name $prefix, array $uses, int $type = Use_::TYPE_NORMAL, array $attributes = []) {
Expand Down
6 changes: 3 additions & 3 deletions lib/PhpParser/Node/Stmt/Use_.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ class Use_ extends Stmt {
/** Constant import */
public const TYPE_CONSTANT = 3;

/** @var int Type of alias */
/** @var self::TYPE_* Type of alias */
public int $type;
/** @var UseItem[] Aliases */
public array $uses;

/**
* Constructs an alias (use) list node.
*
* @param UseItem[] $uses Aliases
* @param int $type Type of alias
* @param UseItem[] $uses Aliases
* @param Stmt\Use_::TYPE_* $type Type of alias
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct(array $uses, int $type = self::TYPE_NORMAL, array $attributes = []) {
Expand Down
8 changes: 5 additions & 3 deletions lib/PhpParser/Node/UseItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use PhpParser\Node\Stmt\Use_;

class UseItem extends Node\Stmt {
/** @var int One of the Stmt\Use_::TYPE_* constants. Will only differ from TYPE_UNKNOWN for mixed group uses */
/**
* @var Use_::TYPE_* One of the Stmt\Use_::TYPE_* constants. Will only differ from TYPE_UNKNOWN for mixed group uses
*/
public int $type;
/** @var Node\Name Namespace, class, function or constant to alias */
public Name $name;
Expand All @@ -18,8 +20,8 @@ class UseItem extends Node\Stmt {
*
* @param Node\Name $name Namespace/Class to alias
* @param null|string|Identifier $alias Alias
* @param int $type Type of the use element (for mixed group use only)
* @param array<string, mixed> $attributes Additional attributes
* @param Use_::TYPE_* $type Type of the use element (for mixed group use only)
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct(Node\Name $name, $alias = null, int $type = Use_::TYPE_UNKNOWN, array $attributes = []) {
$this->attributes = $attributes;
Expand Down
5 changes: 3 additions & 2 deletions lib/PhpParser/NodeVisitor/NameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public function enterNode(Node $node) {
return null;
}

/** @param Stmt\Use_::TYPE_* $type */
private function addAlias(Node\UseItem $use, int $type, ?Name $prefix = null): void {
// Add prefix for group uses
$name = $prefix ? Name::concat($prefix, $use->name) : $use->name;
Expand Down Expand Up @@ -205,8 +206,8 @@ private function resolveType(?Node $node): ?Node {
/**
* Resolve name, according to name resolver options.
*
* @param Name $name Function or constant name to resolve
* @param int $type One of Stmt\Use_::TYPE_*
* @param Name $name Function or constant name to resolve
* @param Stmt\Use_::TYPE_* $type One of Stmt\Use_::TYPE_*
*
* @return Name Resolved name, or original name with attribute
*/
Expand Down

0 comments on commit 263fa80

Please sign in to comment.