diff --git a/src/Dependency/ExportedNode/ExportedAttributeArgumentNode.php b/src/Dependency/ExportedNode/ExportedAttributeArgumentNode.php deleted file mode 100644 index f3ee48130d..0000000000 --- a/src/Dependency/ExportedNode/ExportedAttributeArgumentNode.php +++ /dev/null @@ -1,71 +0,0 @@ -name === $node->name - && $this->value === $node->value - && $this->byRef === $node->byRef; - } - - /** - * @param mixed[] $properties - * @return self - */ - public static function __set_state(array $properties): ExportedNode - { - return new self( - $properties['name'], - $properties['value'], - $properties['byRef'], - ); - } - - /** - * @return mixed - */ - public function jsonSerialize() - { - return [ - 'type' => self::class, - 'data' => [ - 'name' => $this->name, - 'value' => $this->value, - 'byRef' => $this->byRef, - ], - ]; - } - - /** - * @param mixed[] $data - * @return self - */ - public static function decode(array $data): ExportedNode - { - return new self( - $data['name'], - $data['value'], - $data['byRef'], - ); - } - -} diff --git a/src/Dependency/ExportedNode/ExportedAttributeNode.php b/src/Dependency/ExportedNode/ExportedAttributeNode.php index 5f748f8492..02714f2f59 100644 --- a/src/Dependency/ExportedNode/ExportedAttributeNode.php +++ b/src/Dependency/ExportedNode/ExportedAttributeNode.php @@ -4,15 +4,14 @@ use JsonSerializable; use PHPStan\Dependency\ExportedNode; -use PHPStan\ShouldNotHappenException; -use function array_map; +use ReturnTypeWillChange; use function count; class ExportedAttributeNode implements ExportedNode, JsonSerializable { /** - * @param ExportedAttributeArgumentNode[] $args + * @param array $args argument name or index(string|int) => value expression (string) */ public function __construct( private string $name, @@ -27,18 +26,21 @@ public function equals(ExportedNode $node): bool return false; } + if ($this->name !== $node->name) { + return false; + } + if (count($this->args) !== count($node->args)) { return false; } - foreach ($this->args as $i => $ourAttribute) { - $theirAttribute = $node->args[$i]; - if (!$ourAttribute->equals($theirAttribute)) { + foreach ($this->args as $argName => $argValue) { + if (!isset($node->args[$argName]) || $argValue !== $node->args[$argName]) { return false; } } - return $this->name === $node->name; + return true; } /** @@ -56,6 +58,7 @@ public static function __set_state(array $properties): ExportedNode /** * @return mixed */ + #[ReturnTypeWillChange] public function jsonSerialize() { return [ @@ -75,12 +78,7 @@ public static function decode(array $data): ExportedNode { return new self( $data['name'], - array_map(static function (array $parameterData): ExportedAttributeArgumentNode { - if ($parameterData['type'] !== ExportedAttributeArgumentNode::class) { - throw new ShouldNotHappenException(); - } - return ExportedAttributeArgumentNode::decode($parameterData['data']); - }, $data['args']), + $data['args'], ); } diff --git a/src/Dependency/ExportedNode/ExportedClassConstantNode.php b/src/Dependency/ExportedNode/ExportedClassConstantNode.php index 6a59ca1b02..aab53925f1 100644 --- a/src/Dependency/ExportedNode/ExportedClassConstantNode.php +++ b/src/Dependency/ExportedNode/ExportedClassConstantNode.php @@ -33,9 +33,8 @@ public function equals(ExportedNode $node): bool return false; } - foreach ($this->attributes as $i => $ourAttribute) { - $theirAttribute = $node->attributes[$i]; - if (!$ourAttribute->equals($theirAttribute)) { + foreach ($this->attributes as $i => $attribute) { + if (!$attribute->equals($node->attributes[$i])) { return false; } } @@ -66,11 +65,11 @@ public static function decode(array $data): ExportedNode return new self( $data['name'], $data['value'], - array_map(static function (array $parameterData): ExportedAttributeNode { - if ($parameterData['type'] !== ExportedAttributeNode::class) { + array_map(static function (array $attributeData): ExportedAttributeNode { + if ($attributeData['type'] !== ExportedAttributeNode::class) { throw new ShouldNotHappenException(); } - return ExportedAttributeNode::decode($parameterData['data']); + return ExportedAttributeNode::decode($attributeData['data']); }, $data['attributes']), ); } diff --git a/src/Dependency/ExportedNode/ExportedClassNode.php b/src/Dependency/ExportedNode/ExportedClassNode.php index ac55e42a39..937fd9a7df 100644 --- a/src/Dependency/ExportedNode/ExportedClassNode.php +++ b/src/Dependency/ExportedNode/ExportedClassNode.php @@ -161,11 +161,11 @@ public static function decode(array $data): ExportedNode return $nodeType::decode($node['data']); }, $data['statements']), - array_map(static function (array $parameterData): ExportedAttributeNode { - if ($parameterData['type'] !== ExportedAttributeNode::class) { + array_map(static function (array $attributeData): ExportedAttributeNode { + if ($attributeData['type'] !== ExportedAttributeNode::class) { throw new ShouldNotHappenException(); } - return ExportedAttributeNode::decode($parameterData['data']); + return ExportedAttributeNode::decode($attributeData['data']); }, $data['attributes']), ); } diff --git a/src/Dependency/ExportedNode/ExportedEnumNode.php b/src/Dependency/ExportedNode/ExportedEnumNode.php index ad4670721e..f01c1c2c97 100644 --- a/src/Dependency/ExportedNode/ExportedEnumNode.php +++ b/src/Dependency/ExportedNode/ExportedEnumNode.php @@ -63,11 +63,9 @@ public function equals(ExportedNode $node): bool } foreach ($this->attributes as $i => $attribute) { - if ($attribute->equals($node->attributes[$i])) { - continue; + if (!$attribute->equals($node->attributes[$i])) { + return false; } - - return false; } return $this->name === $node->name @@ -126,11 +124,11 @@ public static function decode(array $data): ExportedNode return $nodeType::decode($node['data']); }, $data['statements']), - array_map(static function (array $parameterData): ExportedAttributeNode { - if ($parameterData['type'] !== ExportedAttributeNode::class) { + array_map(static function (array $attributeData): ExportedAttributeNode { + if ($attributeData['type'] !== ExportedAttributeNode::class) { throw new ShouldNotHappenException(); } - return ExportedAttributeNode::decode($parameterData['data']); + return ExportedAttributeNode::decode($attributeData['data']); }, $data['attributes']), ); } diff --git a/src/Dependency/ExportedNode/ExportedFunctionNode.php b/src/Dependency/ExportedNode/ExportedFunctionNode.php index 9ec002b367..795cdb2ed9 100644 --- a/src/Dependency/ExportedNode/ExportedFunctionNode.php +++ b/src/Dependency/ExportedNode/ExportedFunctionNode.php @@ -60,9 +60,8 @@ public function equals(ExportedNode $node): bool return false; } - foreach ($this->attributes as $i => $ourAttribute) { - $theirAttribute = $node->attributes[$i]; - if (!$ourAttribute->equals($theirAttribute)) { + foreach ($this->attributes as $i => $attribute) { + if (!$attribute->equals($node->attributes[$i])) { return false; } } @@ -124,11 +123,11 @@ public static function decode(array $data): ExportedNode } return ExportedParameterNode::decode($parameterData['data']); }, $data['parameters']), - array_map(static function (array $parameterData): ExportedAttributeNode { - if ($parameterData['type'] !== ExportedAttributeNode::class) { + array_map(static function (array $attributeData): ExportedAttributeNode { + if ($attributeData['type'] !== ExportedAttributeNode::class) { throw new ShouldNotHappenException(); } - return ExportedAttributeNode::decode($parameterData['data']); + return ExportedAttributeNode::decode($attributeData['data']); }, $data['attributes']), ); } diff --git a/src/Dependency/ExportedNode/ExportedMethodNode.php b/src/Dependency/ExportedNode/ExportedMethodNode.php index 27e9406881..a60a6d205e 100644 --- a/src/Dependency/ExportedNode/ExportedMethodNode.php +++ b/src/Dependency/ExportedNode/ExportedMethodNode.php @@ -65,9 +65,8 @@ public function equals(ExportedNode $node): bool return false; } - foreach ($this->attributes as $i => $ourAttribute) { - $theirAttribute = $node->attributes[$i]; - if (!$ourAttribute->equals($theirAttribute)) { + foreach ($this->attributes as $i => $attribute) { + if (!$attribute->equals($node->attributes[$i])) { return false; } } @@ -149,11 +148,11 @@ public static function decode(array $data): ExportedNode } return ExportedParameterNode::decode($parameterData['data']); }, $data['parameters']), - array_map(static function (array $parameterData): ExportedAttributeNode { - if ($parameterData['type'] !== ExportedAttributeNode::class) { + array_map(static function (array $attributeData): ExportedAttributeNode { + if ($attributeData['type'] !== ExportedAttributeNode::class) { throw new ShouldNotHappenException(); } - return ExportedAttributeNode::decode($parameterData['data']); + return ExportedAttributeNode::decode($attributeData['data']); }, $data['attributes']), ); } diff --git a/src/Dependency/ExportedNode/ExportedParameterNode.php b/src/Dependency/ExportedNode/ExportedParameterNode.php index ced2ac4c1e..d205afafdb 100644 --- a/src/Dependency/ExportedNode/ExportedParameterNode.php +++ b/src/Dependency/ExportedNode/ExportedParameterNode.php @@ -36,9 +36,8 @@ public function equals(ExportedNode $node): bool return false; } - foreach ($this->attributes as $i => $ourAttribute) { - $theirAttribute = $node->attributes[$i]; - if (!$ourAttribute->equals($theirAttribute)) { + foreach ($this->attributes as $i => $attribute) { + if (!$attribute->equals($node->attributes[$i])) { return false; } } @@ -97,11 +96,11 @@ public static function decode(array $data): ExportedNode $data['byRef'], $data['variadic'], $data['hasDefault'], - array_map(static function (array $parameterData): ExportedAttributeNode { - if ($parameterData['type'] !== ExportedAttributeNode::class) { + array_map(static function (array $attributeData): ExportedAttributeNode { + if ($attributeData['type'] !== ExportedAttributeNode::class) { throw new ShouldNotHappenException(); } - return ExportedAttributeNode::decode($parameterData['data']); + return ExportedAttributeNode::decode($attributeData['data']); }, $data['attributes']), ); } diff --git a/src/Dependency/ExportedNode/ExportedPropertiesNode.php b/src/Dependency/ExportedNode/ExportedPropertiesNode.php index 363b50953e..c801f25642 100644 --- a/src/Dependency/ExportedNode/ExportedPropertiesNode.php +++ b/src/Dependency/ExportedNode/ExportedPropertiesNode.php @@ -61,9 +61,8 @@ public function equals(ExportedNode $node): bool return false; } - foreach ($this->attributes as $i => $ourAttribute) { - $theirAttribute = $node->attributes[$i]; - if (!$ourAttribute->equals($theirAttribute)) { + foreach ($this->attributes as $i => $attribute) { + if (!$attribute->equals($node->attributes[$i])) { return false; } } @@ -107,11 +106,11 @@ public static function decode(array $data): ExportedNode $data['private'], $data['static'], $data['readonly'], - array_map(static function (array $parameterData): ExportedAttributeNode { - if ($parameterData['type'] !== ExportedAttributeNode::class) { + array_map(static function (array $attributeData): ExportedAttributeNode { + if ($attributeData['type'] !== ExportedAttributeNode::class) { throw new ShouldNotHappenException(); } - return ExportedAttributeNode::decode($parameterData['data']); + return ExportedAttributeNode::decode($attributeData['data']); }, $data['attributes']), ); } diff --git a/src/Dependency/ExportedNodeResolver.php b/src/Dependency/ExportedNodeResolver.php index fb64727bc8..a03e883664 100644 --- a/src/Dependency/ExportedNodeResolver.php +++ b/src/Dependency/ExportedNodeResolver.php @@ -7,7 +7,6 @@ use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; -use PHPStan\Dependency\ExportedNode\ExportedAttributeArgumentNode; use PHPStan\Dependency\ExportedNode\ExportedAttributeNode; use PHPStan\Dependency\ExportedNode\ExportedClassConstantNode; use PHPStan\Dependency\ExportedNode\ExportedClassConstantsNode; @@ -411,12 +410,8 @@ private function exportAttributeNodes(array $attributeGroups): array foreach ($attributeGroups as $attributeGroup) { foreach ($attributeGroup->attrs as $attribute) { $args = []; - foreach ($attribute->args as $arg) { - $args[] = new ExportedAttributeArgumentNode( - $arg->name !== null ? $arg->name->name : null, - $this->exprPrinter->printExpr($arg->value), - $arg->byRef, - ); + foreach ($attribute->args as $i => $arg) { + $args[$arg->name->name ?? $i] = $this->exprPrinter->printExpr($arg->value); } $nodes[] = new ExportedAttributeNode(