From 39a452f30553376cdf613303ad04119a5477d6bf Mon Sep 17 00:00:00 2001 From: Christian Stocker Date: Sat, 1 Dec 2018 13:23:43 +0100 Subject: [PATCH 1/2] internal classes have false in reflection::getFilename() Make sure, those are not added to ClassMetadata->fileResources, since otherwise file_exists in strict_mode=1 trips over in the metadata package at https://github.com/schmittjoh/metadata/blob/master/src/ClassMetadata.php#L65 --- src/Metadata/Driver/AnnotationDriver.php | 5 ++++- src/Metadata/Driver/NullDriver.php | 5 ++++- src/Metadata/Driver/XmlDriver.php | 5 ++++- src/Metadata/Driver/YamlDriver.php | 6 +++++- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Metadata/Driver/AnnotationDriver.php b/src/Metadata/Driver/AnnotationDriver.php index 1a8cb0d07..a04d5591f 100644 --- a/src/Metadata/Driver/AnnotationDriver.php +++ b/src/Metadata/Driver/AnnotationDriver.php @@ -73,7 +73,10 @@ public function __construct(Reader $reader, PropertyNamingStrategyInterface $nam public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadata { $classMetadata = new ClassMetadata($name = $class->name); - $classMetadata->fileResources[] = $class->getFilename(); + $fileResource = $class->getFilename(); + if (false !== $fileResource) { + $classMetadata->fileResources[] = $fileResource; + } $propertiesMetadata = []; $propertiesAnnotations = []; diff --git a/src/Metadata/Driver/NullDriver.php b/src/Metadata/Driver/NullDriver.php index 3d8057abb..fbbf750f0 100644 --- a/src/Metadata/Driver/NullDriver.php +++ b/src/Metadata/Driver/NullDriver.php @@ -13,7 +13,10 @@ class NullDriver implements DriverInterface public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadata { $classMetadata = new ClassMetadata($name = $class->name); - $classMetadata->fileResources[] = $class->getFilename(); + $fileResource = $class->getFilename(); + if (false !== $fileResource) { + $classMetadata->fileResources[] = $fileResource; + } return $classMetadata; } diff --git a/src/Metadata/Driver/XmlDriver.php b/src/Metadata/Driver/XmlDriver.php index b2c0390c7..c894c9ccf 100644 --- a/src/Metadata/Driver/XmlDriver.php +++ b/src/Metadata/Driver/XmlDriver.php @@ -56,7 +56,10 @@ protected function loadMetadataFromFile(\ReflectionClass $class, string $path): $elem = reset($elems); $metadata->fileResources[] = $path; - $metadata->fileResources[] = $class->getFileName(); + $fileResource = $class->getFilename(); + if (false !== $fileResource) { + $metadata->fileResources[] = $fileResource; + } $exclusionPolicy = strtoupper((string) $elem->attributes()->{'exclusion-policy'}) ?: 'NONE'; $exclude = $elem->attributes()->exclude; $excludeAll = null !== $exclude ? 'true' === strtolower((string) $exclude) : false; diff --git a/src/Metadata/Driver/YamlDriver.php b/src/Metadata/Driver/YamlDriver.php index c083370c6..20b2e81a7 100644 --- a/src/Metadata/Driver/YamlDriver.php +++ b/src/Metadata/Driver/YamlDriver.php @@ -48,7 +48,11 @@ protected function loadMetadataFromFile(\ReflectionClass $class, string $file): $config = $config[$name]; $metadata = new ClassMetadata($name); $metadata->fileResources[] = $file; - $metadata->fileResources[] = $class->getFileName(); + $fileResource = $class->getFilename(); + if (false !== $fileResource) { + $metadata->fileResources[] = $fileResource; + } + $exclusionPolicy = isset($config['exclusion_policy']) ? strtoupper($config['exclusion_policy']) : 'NONE'; $excludeAll = isset($config['exclude']) ? (bool) $config['exclude'] : false; $classAccessType = $config['access_type'] ?? PropertyMetadata::ACCESS_TYPE_PROPERTY; From 82d8dd2adefeea0d4b11e7b8102e1fb8a1a6f32c Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Sun, 9 Dec 2018 15:21:10 +0100 Subject: [PATCH 2/2] test that is possible to define metadata for internal classes --- tests/Metadata/Driver/AnnotationDriverTest.php | 5 +++++ tests/Metadata/Driver/BaseDriverTest.php | 11 +++++++++++ tests/Metadata/Driver/XmlDriverTest.php | 1 + tests/Metadata/Driver/YamlDriverTest.php | 1 + tests/Metadata/Driver/xml/_/PDOStatement.xml | 6 ++++++ tests/Metadata/Driver/yml/_/PDOStatement.yml | 5 +++++ 6 files changed, 29 insertions(+) create mode 100644 tests/Metadata/Driver/xml/_/PDOStatement.xml create mode 100644 tests/Metadata/Driver/yml/_/PDOStatement.yml diff --git a/tests/Metadata/Driver/AnnotationDriverTest.php b/tests/Metadata/Driver/AnnotationDriverTest.php index b9be6866f..8bba7acf7 100644 --- a/tests/Metadata/Driver/AnnotationDriverTest.php +++ b/tests/Metadata/Driver/AnnotationDriverTest.php @@ -14,4 +14,9 @@ protected function getDriver() { return new AnnotationDriver(new AnnotationReader(), new IdenticalPropertyNamingStrategy()); } + + public function testCanDefineMetadataForInternalClass() + { + $this->markTestSkipped('Can not define annotation metadata for internal classes'); + } } diff --git a/tests/Metadata/Driver/BaseDriverTest.php b/tests/Metadata/Driver/BaseDriverTest.php index bf686a6ea..1516fae86 100644 --- a/tests/Metadata/Driver/BaseDriverTest.php +++ b/tests/Metadata/Driver/BaseDriverTest.php @@ -271,6 +271,17 @@ public function testLoadXmlDiscriminatorWithNamespaces() self::assertFalse($m->xmlDiscriminatorAttribute); } + public function testCanDefineMetadataForInternalClass() + { + /** @var ClassMetadata $m */ + $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass(\PDOStatement::class)); + + self::assertNotNull($m); + self::assertSame('int', $m->propertyMetadata['queryString']->type['name']); + + self::assertCount(1, $m->fileResources); + } + public function testLoadXmlDiscriminatorWithAttributeNamespaces() { /** @var ClassMetadata $m */ diff --git a/tests/Metadata/Driver/XmlDriverTest.php b/tests/Metadata/Driver/XmlDriverTest.php index b50193690..7a2802253 100644 --- a/tests/Metadata/Driver/XmlDriverTest.php +++ b/tests/Metadata/Driver/XmlDriverTest.php @@ -96,6 +96,7 @@ protected function getDriver() return new XmlDriver(new FileLocator([ 'JMS\Serializer\Tests\Fixtures' => __DIR__ . '/xml' . $append, + '' => __DIR__ . '/xml/_' . $append, ]), new IdenticalPropertyNamingStrategy()); } } diff --git a/tests/Metadata/Driver/YamlDriverTest.php b/tests/Metadata/Driver/YamlDriverTest.php index 614eeb4a8..a2a48264d 100644 --- a/tests/Metadata/Driver/YamlDriverTest.php +++ b/tests/Metadata/Driver/YamlDriverTest.php @@ -76,6 +76,7 @@ private function getDriverForSubDir($subDir = null) { return new YamlDriver(new FileLocator([ 'JMS\Serializer\Tests\Fixtures' => __DIR__ . '/yml' . ($subDir ? '/' . $subDir : ''), + '' => __DIR__ . '/yml/_' . ($subDir ? '/' . $subDir : ''), ]), new IdenticalPropertyNamingStrategy()); } diff --git a/tests/Metadata/Driver/xml/_/PDOStatement.xml b/tests/Metadata/Driver/xml/_/PDOStatement.xml new file mode 100644 index 000000000..63c13efb1 --- /dev/null +++ b/tests/Metadata/Driver/xml/_/PDOStatement.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/tests/Metadata/Driver/yml/_/PDOStatement.yml b/tests/Metadata/Driver/yml/_/PDOStatement.yml new file mode 100644 index 000000000..9f2a91e14 --- /dev/null +++ b/tests/Metadata/Driver/yml/_/PDOStatement.yml @@ -0,0 +1,5 @@ +PDOStatement: + properties: + queryString: + type: int +