From f30108d9b8050be9df3804aa7286bf4db9de419b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Mon, 4 Dec 2023 20:57:25 +0100 Subject: [PATCH 1/5] qa: add test to verify metadata parser can handle missing attributes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- tests/Fixtures/MissingAttributeObject.php | 21 +++++++++++++++++++ ...aseAnnotationOrAttributeDriverTestCase.php | 14 +++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 tests/Fixtures/MissingAttributeObject.php diff --git a/tests/Fixtures/MissingAttributeObject.php b/tests/Fixtures/MissingAttributeObject.php new file mode 100644 index 000000000..508485f4e --- /dev/null +++ b/tests/Fixtures/MissingAttributeObject.php @@ -0,0 +1,21 @@ +markTestSkipped('Short expose syntax not supported on annotations or attribute'); } + + public function testCanHandleMissingAttributes(): void + { + $metadata = $this->getDriver()->loadMetadataForClass(new ReflectionClass(MissingAttributeObject::class)); + self::assertArrayHasKey('property', $metadata->propertyMetadata); + + if (PHP_VERSION_ID >= 80000) { + self::assertArrayHasKey('propertyFromMethod', $metadata->propertyMetadata); + } + } } From 545f455ff28586f3fbbe2ebc0584658812cf04be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Mon, 4 Dec 2023 20:57:40 +0100 Subject: [PATCH 2/5] bugfix: handle missing attributes when parsing metadata MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- .../Driver/AnnotationOrAttributeDriver.php | 23 ++++++++++++++++--- src/Metadata/Driver/AttributeDriver.php | 23 ++++++++++++++++--- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/Metadata/Driver/AnnotationOrAttributeDriver.php b/src/Metadata/Driver/AnnotationOrAttributeDriver.php index 9f6f514c5..a3d14f035 100644 --- a/src/Metadata/Driver/AnnotationOrAttributeDriver.php +++ b/src/Metadata/Driver/AnnotationOrAttributeDriver.php @@ -48,6 +48,8 @@ use Metadata\Driver\DriverInterface; use Metadata\MethodMetadata; +use function array_filter; + class AnnotationOrAttributeDriver implements DriverInterface { use ExpressionMetadataTrait; @@ -309,7 +311,12 @@ protected function getClassAnnotations(\ReflectionClass $class): array static function (\ReflectionAttribute $attribute): object { return $attribute->newInstance(); }, - $class->getAttributes() + array_filter( + $class->getAttributes(), + static function (\ReflectionAttribute $attribute): bool { + return class_exists($attribute->getName()); + } + ) ); } @@ -332,7 +339,12 @@ protected function getMethodAnnotations(\ReflectionMethod $method): array static function (\ReflectionAttribute $attribute): object { return $attribute->newInstance(); }, - $method->getAttributes() + array_filter( + $method->getAttributes(), + static function (\ReflectionAttribute $attribute): bool { + return class_exists($attribute->getName()); + } + ) ); } @@ -355,7 +367,12 @@ protected function getPropertyAnnotations(\ReflectionProperty $property): array static function (\ReflectionAttribute $attribute): object { return $attribute->newInstance(); }, - $property->getAttributes() + array_filter( + $property->getAttributes(), + static function (\ReflectionAttribute $attribute): bool { + return class_exists($attribute->getName()); + } + ) ); } diff --git a/src/Metadata/Driver/AttributeDriver.php b/src/Metadata/Driver/AttributeDriver.php index 8d794c64b..8c2437613 100644 --- a/src/Metadata/Driver/AttributeDriver.php +++ b/src/Metadata/Driver/AttributeDriver.php @@ -4,6 +4,8 @@ namespace JMS\Serializer\Metadata\Driver; +use function array_filter; + class AttributeDriver extends AnnotationOrAttributeDriver { /** @@ -15,7 +17,12 @@ protected function getClassAnnotations(\ReflectionClass $class): array static function (\ReflectionAttribute $attribute): object { return $attribute->newInstance(); }, - $class->getAttributes() + array_filter( + $class->getAttributes(), + static function (\ReflectionAttribute $attribute): bool { + return class_exists($attribute->getName()); + } + ) ); } @@ -28,7 +35,12 @@ protected function getMethodAnnotations(\ReflectionMethod $method): array static function (\ReflectionAttribute $attribute): object { return $attribute->newInstance(); }, - $method->getAttributes() + array_filter( + $method->getAttributes(), + static function (\ReflectionAttribute $attribute): bool { + return class_exists($attribute->getName()); + } + ) ); } @@ -41,7 +53,12 @@ protected function getPropertyAnnotations(\ReflectionProperty $property): array static function (\ReflectionAttribute $attribute): object { return $attribute->newInstance(); }, - $property->getAttributes() + array_filter( + $property->getAttributes(), + static function (\ReflectionAttribute $attribute): bool { + return class_exists($attribute->getName()); + } + ) ); } } From 64da5ffebb807ff3a16909abccc30de4715ff8e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Tue, 5 Dec 2023 10:54:01 +0100 Subject: [PATCH 3/5] qa: ignore missing attribute in static analysis MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The attribute is missing on purpose and therefore, ignoring that error should be legit. Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- phpstan/ignore-by-php-version.neon.php | 3 +++ phpstan/ignore-missing-attribute.neon | 3 +++ 2 files changed, 6 insertions(+) create mode 100644 phpstan/ignore-missing-attribute.neon diff --git a/phpstan/ignore-by-php-version.neon.php b/phpstan/ignore-by-php-version.neon.php index a7156b63c..a569893de 100644 --- a/phpstan/ignore-by-php-version.neon.php +++ b/phpstan/ignore-by-php-version.neon.php @@ -9,6 +9,9 @@ if (PHP_VERSION_ID < 80100) { $includes[] = __DIR__ . '/no-enum.neon'; } +if (PHP_VERSION_ID >= 80000) { + $includes[] = __DIR__ . '/ignore-missing-attributes.neon'; +} if (PHP_VERSION_ID >= 80100 && PHP_VERSION_ID < 80200) { $includes[] = __DIR__ . '/php-81.neon'; } diff --git a/phpstan/ignore-missing-attribute.neon b/phpstan/ignore-missing-attribute.neon new file mode 100644 index 000000000..22b569d97 --- /dev/null +++ b/phpstan/ignore-missing-attribute.neon @@ -0,0 +1,3 @@ +parameters: + ignoreErrors: + - '#^Attribute class JMS\\Serializer\\Tests\\Fixtures\\MissingAttribute does not exist\.$#' From e0fd33c73259dc85d5e0e72630848e9c718ecc94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Thu, 7 Dec 2023 16:13:02 +0100 Subject: [PATCH 4/5] qa: add annotation so that test is passing for both metadata drivers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- tests/Fixtures/MissingAttributeObject.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Fixtures/MissingAttributeObject.php b/tests/Fixtures/MissingAttributeObject.php index 508485f4e..15f09af75 100644 --- a/tests/Fixtures/MissingAttributeObject.php +++ b/tests/Fixtures/MissingAttributeObject.php @@ -12,6 +12,9 @@ final class MissingAttributeObject #[MissingAttribute] public $property; + /** + * @VirtualProperty(name="propertyFromMethod") + */ #[MissingAttribute] #[VirtualProperty(name: 'propertyFromMethod')] public function propertyMethod() From b69fe07004ae2dc5602ce3007f11377c2479eccf Mon Sep 17 00:00:00 2001 From: Marcin Czarnecki Date: Sat, 9 Dec 2023 12:50:37 +0100 Subject: [PATCH 5/5] Update phpstan/ignore-by-php-version.neon.php --- phpstan/ignore-by-php-version.neon.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpstan/ignore-by-php-version.neon.php b/phpstan/ignore-by-php-version.neon.php index a569893de..d5d5a0520 100644 --- a/phpstan/ignore-by-php-version.neon.php +++ b/phpstan/ignore-by-php-version.neon.php @@ -10,7 +10,7 @@ $includes[] = __DIR__ . '/no-enum.neon'; } if (PHP_VERSION_ID >= 80000) { - $includes[] = __DIR__ . '/ignore-missing-attributes.neon'; + $includes[] = __DIR__ . '/ignore-missing-attribute.neon'; } if (PHP_VERSION_ID >= 80100 && PHP_VERSION_ID < 80200) { $includes[] = __DIR__ . '/php-81.neon';