diff --git a/src/Dependency/ExportedNode/ExportedAttributeArgumentNode.php b/src/Dependency/ExportedNode/ExportedAttributeArgumentNode.php index f3ee48130d1..64bad230cb5 100644 --- a/src/Dependency/ExportedNode/ExportedAttributeArgumentNode.php +++ b/src/Dependency/ExportedNode/ExportedAttributeArgumentNode.php @@ -4,15 +4,12 @@ use JsonSerializable; use PHPStan\Dependency\ExportedNode; +use ReturnTypeWillChange; class ExportedAttributeArgumentNode implements ExportedNode, JsonSerializable { - public function __construct( - private ?string $name, - private string $value, - private bool $byRef, - ) + public function __construct(private string $value) { } @@ -22,9 +19,7 @@ public function equals(ExportedNode $node): bool return false; } - return $this->name === $node->name - && $this->value === $node->value - && $this->byRef === $node->byRef; + return $this->value === $node->value; } /** @@ -34,23 +29,20 @@ public function equals(ExportedNode $node): bool public static function __set_state(array $properties): ExportedNode { return new self( - $properties['name'], $properties['value'], - $properties['byRef'], ); } /** * @return mixed */ + #[ReturnTypeWillChange] public function jsonSerialize() { return [ 'type' => self::class, 'data' => [ - 'name' => $this->name, 'value' => $this->value, - 'byRef' => $this->byRef, ], ]; } @@ -62,9 +54,7 @@ public function jsonSerialize() 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 5f748f8492b..d084a49572a 100644 --- a/src/Dependency/ExportedNode/ExportedAttributeNode.php +++ b/src/Dependency/ExportedNode/ExportedAttributeNode.php @@ -5,14 +5,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 */ public function __construct( private string $name, @@ -27,18 +27,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 => $arg) { + if (!isset($node->args[$argName]) || !$arg->equals($node->args[$argName])) { return false; } } - return $this->name === $node->name; + return true; } /** @@ -56,6 +59,7 @@ public static function __set_state(array $properties): ExportedNode /** * @return mixed */ + #[ReturnTypeWillChange] public function jsonSerialize() { return [ @@ -73,14 +77,19 @@ public function jsonSerialize() */ public static function decode(array $data): ExportedNode { + $args = []; + + foreach ($data['args'] as $argName => $argData) { + if ($argData['type'] !== ExportedAttributeArgumentNode::class) { + throw new ShouldNotHappenException(); + } + + $args[$argName] = ExportedAttributeArgumentNode::decode($argData['data']); + } + 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']), + $args, ); } diff --git a/src/Dependency/ExportedNode/ExportedClassConstantNode.php b/src/Dependency/ExportedNode/ExportedClassConstantNode.php index 6a59ca1b02f..aab53925f16 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 ac55e42a396..937fd9a7df4 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 ad4670721ec..f01c1c2c978 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 9ec002b367d..795cdb2ed96 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 27e9406881b..a60a6d205ee 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 ced2ac4c1ea..d205afafdbc 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 363b50953ec..c801f256428 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 fb64727bc80..ee98d214217 100644 --- a/src/Dependency/ExportedNodeResolver.php +++ b/src/Dependency/ExportedNodeResolver.php @@ -411,11 +411,9 @@ 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, + foreach ($attribute->args as $i => $arg) { + $args[$arg->name->name ?? $i] = new ExportedAttributeArgumentNode( $this->exprPrinter->printExpr($arg->value), - $arg->byRef, ); }