From ab25ea15b7ad275ded209f37b468b9f59c748c59 Mon Sep 17 00:00:00 2001 From: Bas Peeters Date: Wed, 19 Dec 2018 15:08:45 +0700 Subject: [PATCH] Use superseding props over which they supersede Properties that are superseded or superseded by a property were both all created, causing duplicate properties in classes. For example, property `$award` supersedes `$award` in `CreativeWork` (https://schema.org/award) but both of these properties were generated in the class: ``` /** * @var string|null an award won by or for this item * * @ORM\Column(type="text", nullable=true) * @ApiProperty(iri="http://schema.org/award") */ private $award; /** * @var string|null awards won by or for this item * * @ORM\Column(type="text", nullable=true) */ protected $award; ``` This change uses the property which has been declared as superseding. --- src/TypesGenerator.php | 12 +++++++- tests/Command/GenerateTypesCommandTest.php | 34 ++++++++++++++++++++++ tests/config/superseded-properties.yaml | 3 ++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/config/superseded-properties.yaml diff --git a/src/TypesGenerator.php b/src/TypesGenerator.php index 99897159..0f0e70a5 100644 --- a/src/TypesGenerator.php +++ b/src/TypesGenerator.php @@ -51,6 +51,11 @@ class TypesGenerator */ private const SCHEMA_ORG_RANGE = 'schema:rangeIncludes'; + /** + * @var string + */ + private const SCHEMA_ORG_SUPERSEDED_BY = 'schema:supersededBy'; + /** * @var \Twig_Environment */ @@ -232,7 +237,12 @@ public function generate(array $config): void } else { // All properties foreach ($propertiesMap[$type->getUri()] as $property) { - $class = $this->generateField($config, $class, $type, $typeName, $property->localName(), $property); + if ($property->hasProperty(self::SCHEMA_ORG_SUPERSEDED_BY)) { + $supersededBy = $property->get('schema:supersededBy'); + $this->logger->warning(sprintf('The property "%s" is superseded by "%s". Using the superseding property.', $property->localName(), $supersededBy->localName())); + } else { + $class = $this->generateField($config, $class, $type, $typeName, $property->localName(), $property); + } } } diff --git a/tests/Command/GenerateTypesCommandTest.php b/tests/Command/GenerateTypesCommandTest.php index 111b1bea..79ff8ee3 100644 --- a/tests/Command/GenerateTypesCommandTest.php +++ b/tests/Command/GenerateTypesCommandTest.php @@ -368,4 +368,38 @@ public function testGeneratedEnum() $this->assertNotContains('function setId(', $gender); } + + public function testSupersededProperties() + { + $outputDir = __DIR__.'/../../build/superseded-properties'; + $config = __DIR__.'/../config/superseded-properties.yaml'; + + $this->fs->mkdir($outputDir); + + $commandTester = new CommandTester(new GenerateTypesCommand()); + $this->assertEquals(0, $commandTester->execute(['output' => $outputDir, 'config' => $config])); + + $creativeWork = file_get_contents("$outputDir/AppBundle/Entity/CreativeWork.php"); + + $this->assertContains(<<<'PHP' + /** + * @var string|null an award won by or for this item + * + * @ORM\Column(type="text", nullable=true) + * @ApiProperty(iri="http://schema.org/award") + */ + private $award; +PHP + , $creativeWork); + + $this->assertNotContains(<<<'PHP' + /** + * @var string|null awards won by or for this item + * + * @ORM\Column(type="text", nullable=true) + */ + protected $award; +PHP + , $creativeWork); + } } diff --git a/tests/config/superseded-properties.yaml b/tests/config/superseded-properties.yaml new file mode 100644 index 00000000..66a48f0e --- /dev/null +++ b/tests/config/superseded-properties.yaml @@ -0,0 +1,3 @@ +types: + CreativeWork: + allProperties: true