From 2179c337e2dc21de38dab7ece1f4703d287eab0c Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 22 May 2025 07:52:35 +0700 Subject: [PATCH 1/3] PHPUnit 10/11 Only set attribute when target attribute class exists on CoversAnnotationWithValueToAttributeRector --- ...CoversAnnotationWithValueToAttributeRector.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php b/rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php index f80fb912..5577daec 100644 --- a/rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php +++ b/rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php @@ -145,13 +145,17 @@ public function refactor(Node $node): ?Node return $node; } - private function createAttributeGroup(string $annotationValue): AttributeGroup + private function createAttributeGroup(string $annotationValue): ?AttributeGroup { if (str_starts_with($annotationValue, '::')) { $attributeClass = self::COVERS_FUNCTION_ATTRIBUTE; $attributeValue = [trim($annotationValue, ':()')]; } elseif (str_contains($annotationValue, '::')) { $attributeClass = self::COVERS_METHOD_ATTRIBUTE; + if (! $this->reflectionProvider->hasClass($attributeClass)) { + return null; + } + $attributeValue = [$this->getClass($annotationValue) . '::class', $this->getMethod($annotationValue)]; } else { $attributeClass = self::COVERTS_CLASS_ATTRIBUTE; @@ -161,6 +165,9 @@ private function createAttributeGroup(string $annotationValue): AttributeGroup if ($classReflection->isTrait()) { $attributeClass = self::COVERTS_TRAIT_ATTRIBUTE; + if (! $this->reflectionProvider->hasClass($attributeClass)) { + return null; + } } } @@ -206,6 +213,12 @@ private function handleCoversDefaultClass(PhpDocInfo $phpDocInfo): array continue; } + $attributeGroup = $this->createAttributeGroup($desiredTagValueNode->value->value); + // phpunit 10 may not fully support attribute + if (! $attributeGroup instanceof AttributeGroup) { + continue; + } + $attributeGroups[] = $this->createAttributeGroup($desiredTagValueNode->value->value); $this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode); } From af44a0c40aa31f71d199a7cf65daa9429b6a7b3f Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 22 May 2025 00:53:44 +0000 Subject: [PATCH 2/3] [ci-review] Rector Rectify --- .../Class_/CoversAnnotationWithValueToAttributeRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php b/rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php index 5577daec..99563ac4 100644 --- a/rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php +++ b/rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php @@ -166,7 +166,7 @@ private function createAttributeGroup(string $annotationValue): ?AttributeGroup if ($classReflection->isTrait()) { $attributeClass = self::COVERTS_TRAIT_ATTRIBUTE; if (! $this->reflectionProvider->hasClass($attributeClass)) { - return null; + return null; } } } From b4f48c4dce2730e0520f53e640714b002ce6d7ee Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 22 May 2025 08:00:57 +0700 Subject: [PATCH 3/3] fix phpstan --- ...rsAnnotationWithValueToAttributeRector.php | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php b/rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php index 99563ac4..1569cc15 100644 --- a/rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php +++ b/rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php @@ -219,7 +219,7 @@ private function handleCoversDefaultClass(PhpDocInfo $phpDocInfo): array continue; } - $attributeGroups[] = $this->createAttributeGroup($desiredTagValueNode->value->value); + $attributeGroups[] = $attributeGroup; $this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode); } @@ -239,10 +239,15 @@ private function handleCovers(PhpDocInfo $phpDocInfo, bool $hasCoversDefault): a } $covers = $desiredTagValueNode->value->value; - if (str_starts_with($covers, '\\')) { - $attributeGroups[$covers] = $this->createAttributeGroup($covers); - } elseif (! $hasCoversDefault && str_starts_with($covers, '::')) { - $attributeGroups[$covers] = $this->createAttributeGroup($covers); + if (str_starts_with($covers, '\\') || (! $hasCoversDefault && str_starts_with($covers, '::'))) { + $attributeGroup = $this->createAttributeGroup($covers); + + // phpunit 10 may not fully support attribute + if (! $attributeGroup instanceof AttributeGroup) { + continue; + } + + $attributeGroups[$covers] = $attributeGroup; } $this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode); @@ -269,10 +274,15 @@ private function resolveMethodAttributes(ClassMethod $classMethod, bool $hasCove } $covers = $desiredTagValueNode->value->value; - if (str_starts_with($covers, '\\')) { - $attributeGroups[$covers] = $this->createAttributeGroup($covers); - } elseif (! $hasCoversDefault && str_starts_with($covers, '::')) { - $attributeGroups[$covers] = $this->createAttributeGroup($covers); + if (str_starts_with($covers, '\\') || (! $hasCoversDefault && str_starts_with($covers, '::'))) { + $attributeGroup = $this->createAttributeGroup($covers); + + // phpunit 10 may not fully support attribute + if (! $attributeGroup instanceof AttributeGroup) { + continue; + } + + $attributeGroups[$covers] = $attributeGroup; } }