From 20639f4a0da910de5fe56b8970225e426cd21d02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Grundko=CC=88tter?= Date: Thu, 9 Nov 2023 08:36:06 +0100 Subject: [PATCH 01/11] some general cleanup --- generate-api.php | 2 +- src/Generator/TypeUtil.php | 4 -- tests/Generator/CodeGenUtilTest.php | 6 +- tests/Generator/JmsXmlDeSerializerTest.php | 56 ++++++++++++++----- tests/Generator/JmsXmlSerializerTest.php | 23 ++++---- tests/Generator/SymfonyJsonSerializerTest.php | 20 +++---- tests/Generator/TypeUtilTest.php | 10 ++-- 7 files changed, 72 insertions(+), 49 deletions(-) diff --git a/generate-api.php b/generate-api.php index fbf59c8..01c85e4 100644 --- a/generate-api.php +++ b/generate-api.php @@ -3,4 +3,4 @@ $xsdFile = './example/openimmo_127b.xsd'; $apiGenerator = new \Ujamii\OpenImmo\Generator\ApiGenerator(); -$apiGenerator->generateApiClasses($xsdFile); \ No newline at end of file +$apiGenerator->generateApiClasses($xsdFile); diff --git a/src/Generator/TypeUtil.php b/src/Generator/TypeUtil.php index 8328d5b..8630ff2 100644 --- a/src/Generator/TypeUtil.php +++ b/src/Generator/TypeUtil.php @@ -21,7 +21,6 @@ public static function getTypeForSerializer(string $type): string $isPlural = substr($type, -2) === '[]'; $singular = str_replace('[]', '', $type); switch ($singular) { - case 'string': case 'float': case 'int': @@ -60,7 +59,6 @@ public static function getTypeForSerializer(string $type): string $ns = self::OPENIMMO_NAMESPACE; $type = $ns . $singular; break; - } if ($isPlural) { @@ -83,7 +81,6 @@ public static function getValidPhpType(string $propertyType): string } switch ($propertyType) { - case 'decimal': case 'float': $propertyType = 'float'; @@ -133,7 +130,6 @@ public static function getValidPhpType(string $propertyType): string public static function getDefaultValueForType(string $propertyType, bool $nullable) { switch ($propertyType) { - case 'float': $defaultValue = 0.0; break; diff --git a/tests/Generator/CodeGenUtilTest.php b/tests/Generator/CodeGenUtilTest.php index 9306514..8e6e225 100644 --- a/tests/Generator/CodeGenUtilTest.php +++ b/tests/Generator/CodeGenUtilTest.php @@ -8,7 +8,7 @@ class CodeGenUtilTest extends TestCase { - public function testAddDescriptionPart() + public function testAddDescriptionPart(): void { $property = new Property('foo'); $property->setComment('foobar makes the world go round'); @@ -17,7 +17,7 @@ public function testAddDescriptionPart() $this->assertEquals('foobar makes the world go round' . CodeGenUtil::DESCRIPTION_PART_DELIMTER . 'really!', $property->getComment()); } - public function testEmptyExistingDescription() + public function testEmptyExistingDescription(): void { $property = new Property('foo'); $property->setComment(' '); @@ -26,7 +26,7 @@ public function testEmptyExistingDescription() $this->assertEquals('foobar', $property->getComment()); } - public function testAddEmptyDescription() + public function testAddEmptyDescription(): void { $property = new Property('foo'); $property->setComment('foo bar'); diff --git a/tests/Generator/JmsXmlDeSerializerTest.php b/tests/Generator/JmsXmlDeSerializerTest.php index 33c9e00..1ae56ce 100644 --- a/tests/Generator/JmsXmlDeSerializerTest.php +++ b/tests/Generator/JmsXmlDeSerializerTest.php @@ -2,7 +2,6 @@ namespace Ujamii\OpenImmo\Tests\Generator; -use Doctrine\Common\Annotations\AnnotationRegistry; use JMS\Serializer\Handler\HandlerRegistryInterface; use JMS\Serializer\SerializerInterface; use PHPUnit\Framework\TestCase; @@ -11,6 +10,7 @@ use Ujamii\OpenImmo\API\Bewertung; use Ujamii\OpenImmo\API\EmailSonstige; use Ujamii\OpenImmo\API\Foto; +use Ujamii\OpenImmo\API\Geo; use Ujamii\OpenImmo\API\Objektkategorie; use Ujamii\OpenImmo\API\Openimmo; use Ujamii\OpenImmo\API\Uebertragung; @@ -36,14 +36,12 @@ public function setUp(): void $registry->registerSubscribingHandler(new DateTimeHandler()); }); $this->serializer = $builder->build(); - // @see https://stackoverflow.com/questions/14629137/jmsserializer-stand-alone-annotation-does-not-exist-or-cannot-be-auto-loaded - AnnotationRegistry::registerLoader('class_exists'); } - public function testReadXml() + public function testReadXml(): void { $file = './example/openimmo-data_127.xml'; - if (! is_file($file) || ! is_readable($file)) { + if (!is_file($file) || !is_readable($file)) { $this->markTestSkipped($file . ' is not part of the distribution package due to license restrictions. Please download yourself from http://www.openimmo.de/go.php/p/24/download.htm (it\'s free)'); } $xmlString = file_get_contents($file); @@ -99,10 +97,10 @@ public function testReadXml() $this->assertEquals('en', $immobilie->getFreitexte()->getObjektText()->getLang()); } - public function testReadRealDataXml() + public function testReadRealDataXml(): void { $file = './example/1548246253_0.xml'; - if (! is_file($file) || ! is_readable($file)) { + if (!is_file($file) || !is_readable($file)) { $this->markTestSkipped('I am not allowed to include real world examples into this distribution package due to license restrictions.'); } $xmlString = file_get_contents($file); @@ -114,7 +112,7 @@ public function testReadRealDataXml() $this->assertCount(17, $openImmo->getAnbieter()[0]->getImmobilie()); } - public function testReadAnhangXml() + public function testReadAnhangXml(): void { $xmlString = ' @@ -136,7 +134,7 @@ public function testReadAnhangXml() * Note the difference in microsecond precision! As the default `precision` (in PHP.ini) is 14, the microsecond * part will only have 6 digits, while other tools may generate longer values! */ - public function testDateTimeWithMicroseconds() + public function testDateTimeWithMicroseconds(): void { $xmlString = ''; /* @var Uebertragung $uebertragung */ @@ -150,7 +148,7 @@ public function testDateTimeWithMicroseconds() $this->assertEquals('2020-08-07T11:56:39.124297+02:00', $uebertragung->getTimestamp()->format('Y-m-d\TH:i:s.uP')); } - public function testReadAussenCourtageXml() + public function testReadAussenCourtageXml(): void { $xmlString = '12354,12 €'; @@ -161,7 +159,7 @@ public function testReadAussenCourtageXml() $this->assertEquals('12354,12 €', $aussenCourtage->getValue()); } - public function testReadObjektKategorieXml() + public function testReadObjektKategorieXml(): void { $xmlString = ' @@ -185,7 +183,7 @@ public function testReadObjektKategorieXml() $this->assertEquals('Dachgeschoss', $category->getObjektart()->getObjektartZusatz()[0]); } - public function testReadComplexType() + public function testReadComplexType(): void { $xmlString = ' @@ -209,7 +207,7 @@ public function testReadComplexType() $this->assertEquals(100, $feld->getWert()); } - public function testReadComplexTypeWithArrays() + public function testReadComplexTypeWithArrays(): void { $xmlString = ' @@ -236,4 +234,36 @@ public function testReadComplexTypeWithArrays() $this->assertEquals('abc', $feld->getName()); $this->assertEquals(100, $feld->getWert()); } + + public function testReadGeoWithUserDefinedSimplefields(): void + { + $xmlString = ' + 01809 + Heidenau + Dr.-Otto-Nuschke-Straße + 2 + + 0 + 8.1.1 + Mügeln + Mügeln + + '; + + /* @var Geo $subject */ + $subject = $this->serializer->deserialize($xmlString, Geo::class, 'xml'); + + $this->assertSame('01809', $subject->getPlz()); + $this->assertSame('Heidenau', $subject->getOrt()); + $this->assertSame('Dr.-Otto-Nuschke-Straße', $subject->getStrasse()); + $this->assertSame('2', $subject->getHausnummer()); + $this->assertSame('DEU', $subject->getLand()->getIsoLand()); + $this->assertSame(0, $subject->getEtage()); + $this->assertSame('8.1.1', $subject->getWohnungsnr()); + $this->assertSame('Mügeln', $subject->getRegionalerZusatz()); + $this->assertCount(2, $subject->getUserDefinedSimplefield()); + $this->assertSame('Mügeln', $subject->getUserDefinedSimplefield()[0]->getValue()); + $this->assertSame('ZONE', $subject->getUserDefinedSimplefield()[0]->getFeldname()); + $this->assertSame('regionaler_zusatz_gruppe', $subject->getUserDefinedSimplefield()[1]->getFeldname()); + } } diff --git a/tests/Generator/JmsXmlSerializerTest.php b/tests/Generator/JmsXmlSerializerTest.php index e8dc3d9..77fda75 100644 --- a/tests/Generator/JmsXmlSerializerTest.php +++ b/tests/Generator/JmsXmlSerializerTest.php @@ -2,7 +2,6 @@ namespace Ujamii\OpenImmo\Tests\Generator; -use Doctrine\Common\Annotations\AnnotationRegistry; use JMS\Serializer\Handler\HandlerRegistryInterface; use JMS\Serializer\SerializerInterface; use PHPUnit\Framework\TestCase; @@ -43,11 +42,9 @@ public function setUp(): void $registry->registerSubscribingHandler(new DateTimeHandler()); }); $this->serializer = $builder->build(); - // @see https://stackoverflow.com/questions/14629137/jmsserializer-stand-alone-annotation-does-not-exist-or-cannot-be-auto-loaded - AnnotationRegistry::registerLoader('class_exists'); } - public function testWriteImmobilieXml() + public function testWriteImmobilieXml(): void { $data = new Immobilie(); $data->setKontaktperson((new Kontaktperson())->setAnrede('Herr')); @@ -57,7 +54,7 @@ public function testWriteImmobilieXml() $this->assertXmlStringEqualsXmlString('Herr', $xmlContent); } - public function testWriteUebertragungXml() + public function testWriteUebertragungXml(): void { $xmlString = ''; @@ -76,7 +73,7 @@ public function testWriteUebertragungXml() $this->assertXmlStringEqualsXmlString($xmlString, $this->serializer->serialize($uebertragung, 'xml')); } - public function testWriteUebertragungXmlRealWorld() + public function testWriteUebertragungXmlRealWorld(): void { $xmlString = ''; @@ -94,7 +91,7 @@ public function testWriteUebertragungXmlRealWorld() $this->assertXmlStringEqualsXmlString($xmlString, $this->serializer->serialize($uebertragung, 'xml')); } - public function testWriteNutzungsartXmlAsUsedInReadme() + public function testWriteNutzungsartXmlAsUsedInReadme(): void { $xmlString = ''; $nutzungsart = new Nutzungsart(); @@ -107,7 +104,7 @@ public function testWriteNutzungsartXmlAsUsedInReadme() $this->assertXmlStringEqualsXmlString($xmlString, $this->serializer->serialize($nutzungsart, 'xml')); } - public function testWriteDistanzenZuSportXml() + public function testWriteDistanzenZuSportXml(): void { $xmlString = '15.0'; $phpObj = new DistanzenSport(DistanzenSport::DISTANZ_ZU_SPORT_SEE, 15); @@ -115,7 +112,7 @@ public function testWriteDistanzenZuSportXml() $this->assertXmlStringEqualsXmlString($xmlString, $this->serializer->serialize($phpObj, 'xml')); } - public function testWriteInfrastrukturXmlAsUsedInReadme() + public function testWriteInfrastrukturXmlAsUsedInReadme(): void { $xmlString = ' false @@ -137,7 +134,7 @@ public function testWriteInfrastrukturXmlAsUsedInReadme() $this->assertXmlStringEqualsXmlString($xmlString, $this->serializer->serialize($infrastrktur, 'xml')); } - public function testWriteAnbieterXml() + public function testWriteAnbieterXml(): void { // as soon as https://github.com/schmittjoh/serializer/pull/883 is merged, the can be removed $xmlString = ' @@ -155,7 +152,7 @@ public function testWriteAnbieterXml() $this->assertXmlStringEqualsXmlString($xmlString, $this->serializer->serialize($openImmo, 'xml')); } - public function testWriteObjektKategorieXml() + public function testWriteObjektKategorieXml(): void { $xmlString = ' @@ -178,7 +175,7 @@ public function testWriteObjektKategorieXml() $this->assertXmlStringEqualsXmlString($xmlString, $this->serializer->serialize($category, 'xml')); } - public function testWriteComplexTypeMixed() + public function testWriteComplexTypeMixed(): void { $xmlString = 'k.A.'; $subject = new AussenCourtage(false, 'k.A.'); @@ -186,7 +183,7 @@ public function testWriteComplexTypeMixed() $this->assertXmlStringEqualsXmlString($xmlString, $this->serializer->serialize($subject, 'xml')); } - public function testWriteComplexType() + public function testWriteComplexType(): void { $xmlString = ' diff --git a/tests/Generator/SymfonyJsonSerializerTest.php b/tests/Generator/SymfonyJsonSerializerTest.php index c269ceb..b31846d 100644 --- a/tests/Generator/SymfonyJsonSerializerTest.php +++ b/tests/Generator/SymfonyJsonSerializerTest.php @@ -57,7 +57,7 @@ public function setUp(): void $this->serializer = new Serializer($normalizers, $encoders); } - public function testWriteImmobilieJson() + public function testWriteImmobilieJson(): void { $jsonString = '{ "geo": { @@ -141,7 +141,7 @@ public function testWriteImmobilieJson() $this->assertJsonStringEqualsJsonString($jsonString, $jsonContent); } - public function testWriteUebertragungJson() + public function testWriteUebertragungJson(): void { $jsonString = '{ "art": "ONLINE", @@ -171,7 +171,7 @@ public function testWriteUebertragungJson() $this->assertJsonStringEqualsJsonString($jsonString, $jsonContent); } - public function testWriteUebertragungJsonRealWorld() + public function testWriteUebertragungJsonRealWorld(): void { $jsonString = '{ "art": "OFFLINE", @@ -199,7 +199,7 @@ public function testWriteUebertragungJsonRealWorld() $this->assertJsonStringEqualsJsonString($jsonString, $jsonContent); } - public function testWriteNutzungsartJsonAsUsedInReadme() + public function testWriteNutzungsartJsonAsUsedInReadme(): void { $jsonString = '{ "anlage": false, @@ -218,7 +218,7 @@ public function testWriteNutzungsartJsonAsUsedInReadme() $this->assertJsonStringEqualsJsonString($jsonString, $jsonContent); } - public function testWriteDistanzenZuSportJson() + public function testWriteDistanzenZuSportJson(): void { $jsonString = '{"distanzZuSport": "SEE", "value": 15}'; $phpObj = new DistanzenSport(DistanzenSport::DISTANZ_ZU_SPORT_SEE, 15); @@ -227,7 +227,7 @@ public function testWriteDistanzenZuSportJson() $this->assertJsonStringEqualsJsonString($jsonString, $jsonContent); } - public function testWriteInfrastrukturJsonAsUsedInReadme() + public function testWriteInfrastrukturJsonAsUsedInReadme(): void { $jsonString = '{ "ausblick": { @@ -265,7 +265,7 @@ public function testWriteInfrastrukturJsonAsUsedInReadme() $this->assertJsonStringEqualsJsonString($jsonString, $jsonContent); } - public function testWriteAnbieterJson() + public function testWriteAnbieterJson(): void { $jsonString = '{ "anbieter": [ @@ -317,7 +317,7 @@ public function testWriteAnbieterJson() $this->assertJsonStringEqualsJsonString($jsonString, $jsonContent); } - public function testWriteObjektKategorieJson() + public function testWriteObjektKategorieJson(): void { $jsonString = '{ "nutzungsart": { @@ -368,7 +368,7 @@ public function testWriteObjektKategorieJson() $this->assertJsonStringEqualsJsonString($jsonString, $jsonContent); } - public function testWriteComplexTypeMixed() + public function testWriteComplexTypeMixed(): void { $jsonString = '{"mitMwst": false, "value": "k.A."}'; $subject = new AussenCourtage(false, 'k.A.'); @@ -377,7 +377,7 @@ public function testWriteComplexTypeMixed() $this->assertJsonStringEqualsJsonString($jsonString, $jsonContent); } - public function testWriteComplexType() + public function testWriteComplexType(): void { $jsonString = '{ "feld": [ diff --git a/tests/Generator/TypeUtilTest.php b/tests/Generator/TypeUtilTest.php index d673a12..cd74a5d 100644 --- a/tests/Generator/TypeUtilTest.php +++ b/tests/Generator/TypeUtilTest.php @@ -24,7 +24,7 @@ public function testCamelize(string $nameInXsd, string $expectedPhpName, ?bool $ $this->assertSame('FooBar', TypeUtil::camelize('foo_bar')); } - public function camelizeDataProvider(): array + public static function camelizeDataProvider(): array { return [ ['bebaubar_nach', 'BebaubarNach'], @@ -48,7 +48,7 @@ public function testGetTypeForSerializer(string $xsdType, string $serializerType $this->assertSame($serializerType, $generatedType); } - public function getTypeForSerializerDataProvider(): array + public static function getTypeForSerializerDataProvider(): array { return [ ['string', 'string'], @@ -84,7 +84,7 @@ public function testGetValidPhpType(string $xsdType, string $phpType): void $this->assertSame($phpType, $generatedType); } - public function getValidPhpTypeDataProvider(): array + public static function getValidPhpTypeDataProvider(): array { return [ ['decimal', 'float'], @@ -123,7 +123,7 @@ public function testGetDefaultValueForType(string $propertyType, bool $nullable, $this->assertSame($defaultValue, $generatedValue); } - public function getDefaultValueForTypeDataProvider(): array + public static function getDefaultValueForTypeDataProvider(): array { return [ ['float', false, 0.0], @@ -157,7 +157,7 @@ public function testIsConstantsBasedProperty(Property $property, bool $expectedR $this->assertSame($expectedResult, TypeUtil::isConstantsBasedProperty($property)); } - public function getConstantsBasedPropertyDataProvider(): \Generator + public static function getConstantsBasedPropertyDataProvider(): \Generator { $property = new Property('foobar'); $property->setComment('Hallo Welt!'); From fe95b44ccac7eb48e20fb138be1fd5b7a4089858 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Grundko=CC=88tter?= Date: Thu, 9 Nov 2023 09:25:13 +0100 Subject: [PATCH 02/11] fixed changed ChoiceType handling in xsd parser --- src/Generator/ApiGenerator.php | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/Generator/ApiGenerator.php b/src/Generator/ApiGenerator.php index 67704d3..9385faa 100644 --- a/src/Generator/ApiGenerator.php +++ b/src/Generator/ApiGenerator.php @@ -3,6 +3,7 @@ namespace Ujamii\OpenImmo\Generator; use GoetasWebservices\XML\XSDReader\Schema\Attribute\Attribute; +use GoetasWebservices\XML\XSDReader\Schema\Element\Choice; use GoetasWebservices\XML\XSDReader\Schema\Element\Element; use GoetasWebservices\XML\XSDReader\Schema\Element\ElementDef; use GoetasWebservices\XML\XSDReader\Schema\Element\ElementItem; @@ -74,7 +75,7 @@ public function generateApiClasses(string $xsdFile, bool $wipeTargetFolder = tru $this->referencedInlineElements = []; foreach ($schema->getElements() as $element) { - if (! ($element->getType() instanceof SimpleType)) { + if (!($element->getType() instanceof SimpleType)) { $this->parseElementDef($element); } } @@ -112,7 +113,14 @@ private function parseElementDef($element): void } else { /* @var ComplexType $complexType */ foreach ($element->getType()->getElements() as $property) { - $this->parseProperty($property, $class, $namespace); + if ($property instanceof Choice) { + /** @var ElementRef $choiceProperty */ + foreach ($property->getElements() as $choiceProperty) { + $this->parseProperty($choiceProperty, $class, $namespace); + } + } else { + $this->parseProperty($property, $class, $namespace); + } } } @@ -122,7 +130,7 @@ private function parseElementDef($element): void $classPropertyCount = count($class->getProperties()); $hasConstructor = $class->hasMethod('__construct'); - if (! $hasConstructor && $classPropertyCount > 0 && $classPropertyCount <= self::MAX_PROPERTIES_IN_CONSTRUCTOR) { + if (!$hasConstructor && $classPropertyCount > 0 && $classPropertyCount <= self::MAX_PROPERTIES_IN_CONSTRUCTOR) { $this->generateConstructor($class); } @@ -155,7 +163,7 @@ private function addSimpleValue(?Extension $extension, ClassType $class, PhpName $namespace ->addUse(Type::class) ->addUse(Inline::class); - CodeGenUtil::generateGetterAndSetter($classProperty, $class, true, ! TypeUtil::isConstantsBasedProperty($classProperty)); + CodeGenUtil::generateGetterAndSetter($classProperty, $class, true, !TypeUtil::isConstantsBasedProperty($classProperty)); } /** @@ -211,14 +219,13 @@ private function parseProperty(ElementItem $property, ClassType $class, PhpNames $namespace->addUse(Type::class); $isArray = 'array' === $phpType; - $nullable = ! $isArray && $property->getMin() === 0; + $nullable = !$isArray && $property->getMin() === 0; // if the property type is an object, it should be nullable - if (strpos($serializerType, TypeUtil::OPENIMMO_NAMESPACE) === 0 || '\DateTime' === $phpType) { + if ('\DateTime' === $phpType || str_starts_with($serializerType, TypeUtil::OPENIMMO_NAMESPACE)) { $nullable = true; } - $classProperty->setType($phpType) ->setNullable($nullable); @@ -265,7 +272,7 @@ private function getPhpPropertyTypeFromXsdElement($property): string } } - if (! ($property instanceof Attribute) && $property->getMax() == -1) { + if (!($property instanceof Attribute) && $property->getMax() == -1) { $propertyType .= '[]'; } @@ -329,12 +336,11 @@ private function parseRestriction(Restriction $restriction, string $nameInXsd, C { foreach ($restriction->getChecks() as $type => $options) { switch ($type) { - case 'enumeration': $constantPrefix = strtoupper($nameInXsd . '_'); foreach ($options as $possibleValue) { $constantName = strtoupper($constantPrefix . str_replace([' ', '-'], '_', $possibleValue['value'])); - if (! array_key_exists($constantName, $class->getConstants())) { + if (!array_key_exists($constantName, $class->getConstants())) { $class->addConstant($constantName, $possibleValue['value']); } } @@ -365,7 +371,6 @@ private function parseRestriction(Restriction $restriction, string $nameInXsd, C default: throw new \InvalidArgumentException(vsprintf('Type "%s" is not handled in %s->parseAttribute', [$type, __CLASS__])); - } } } @@ -408,8 +413,8 @@ public function getTargetFolder(): string */ public function setTargetFolder(?string $targetFolder): void { - if (! is_null($targetFolder)) { - if (! (is_dir($targetFolder) && is_writable($targetFolder))) { + if (!is_null($targetFolder)) { + if (!(is_dir($targetFolder) && is_writable($targetFolder))) { throw new \Exception("Directory {$targetFolder} does not exist or is not writeable!"); } $this->targetFolder = $targetFolder; From e77e060446dfc60753d008367356b7e814680773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Grundko=CC=88tter?= Date: Thu, 9 Nov 2023 09:27:29 +0100 Subject: [PATCH 03/11] updated tests to match new php generation --- .../Generator/ApiGenerator/DefaultClassTest.php | 8 ++++---- .../ApiGenerator/FileGeneratingTest.php | 16 +++++++--------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/tests/Generator/ApiGenerator/DefaultClassTest.php b/tests/Generator/ApiGenerator/DefaultClassTest.php index 29e11ab..575ab4f 100644 --- a/tests/Generator/ApiGenerator/DefaultClassTest.php +++ b/tests/Generator/ApiGenerator/DefaultClassTest.php @@ -12,10 +12,10 @@ public function testGenerateApiClassDefault(): void ); $this->assertClassHasConstants($generatedClass, [ - 'BLICK_BERGE' => "'BERGE'", - 'BLICK_IN_DIE_FERNE' => "'IN DIE FERNE'", - 'BLICK_MEER' => "'MEER'", - 'BLICK_SEE' => "'SEE'", + 'BLICK_BERGE' => 'BERGE', + 'BLICK_IN_DIE_FERNE' => 'IN DIE FERNE', + 'BLICK_MEER' => 'MEER', + 'BLICK_SEE' => 'SEE', ]); $properties = [ diff --git a/tests/Generator/ApiGenerator/FileGeneratingTest.php b/tests/Generator/ApiGenerator/FileGeneratingTest.php index 8800961..678b7fd 100644 --- a/tests/Generator/ApiGenerator/FileGeneratingTest.php +++ b/tests/Generator/ApiGenerator/FileGeneratingTest.php @@ -88,7 +88,7 @@ public function assertClassHasConstants(ClassType $generatedClass, array $consta { foreach ($constants as $constantName => $constantValue) { $this->assertArrayHasKey($constantName, $generatedClass->getConstants(), "Constant {$constantName} does not exist"); - $this->assertEquals($constantValue, $generatedClass->getConstants()[$constantName]->getValue()->__toString()); + $this->assertEquals($constantValue, $generatedClass->getConstants()[$constantName]->getValue(), "Constant {$constantName} in class {$generatedClass->getName()} has wrong value"); } $this->assertCount(count($constants), $generatedClass->getConstants()); } @@ -137,7 +137,6 @@ public function assertClassHasProperty( $getter = $class->getMethod('get' . ucfirst($propertyName)); $this->assertEquals('public', $getter->getVisibility()); $this->assertEquals($phpType, $getter->getReturnType(), "Return type of {$getter->getName()}"); - //$this->assertTrue($getter->isReturnNullable()); $setter = $class->getMethod('set' . ucfirst($propertyName)); $this->assertEquals('public', $setter->getVisibility()); @@ -160,25 +159,24 @@ public function assertClassHasConstructor(ClassType $class, array $properties): $this->assertEquals($type, $constructorParam->getType()); if ($constructorParam->isNullable()) { - $this->assertSame('null', (string) $constructorParam->getDefaultValue()); + $this->assertNull($constructorParam->getDefaultValue(), 'Default value for nullable constructor param ' . $constructorParam->getName() . ' in class ' . $class->getName()); } else { if ($constructorParam->getType() === 'array') { - $this->assertSame('[]', (string) $constructorParam->getDefaultValue()); + $this->assertSame([], $constructorParam->getDefaultValue()); } if ($constructorParam->getType() === 'bool') { - $this->assertSame('false', (string) $constructorParam->getDefaultValue()); + $this->assertFalse($constructorParam->getDefaultValue()); } if ($constructorParam->getType() === 'float') { - $this->assertSame('0.0', (string) $constructorParam->getDefaultValue()); + $this->assertSame(0.0, $constructorParam->getDefaultValue()); } if ($constructorParam->getType() === 'int') { - $this->assertSame('0', (string) $constructorParam->getDefaultValue()); + $this->assertSame(0, $constructorParam->getDefaultValue()); } if ($constructorParam->getType() === 'string') { - $this->assertSame("''", (string) $constructorParam->getDefaultValue()); + $this->assertSame('', $constructorParam->getDefaultValue()); } } -// $this->assertFalse($constructor->getParameter($propertyName)->getNullable()); } } From 3ae1b1b9364c59282dcf81121dbb13f7566fffcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Grundko=CC=88tter?= Date: Thu, 9 Nov 2023 09:27:59 +0100 Subject: [PATCH 04/11] auto formatted code for >=8.1 looks a little different --- src/API/Anhaenge.php | 2 +- src/API/Anhang.php | 2 +- src/API/Ausbaustufe.php | 2 +- src/API/Bad.php | 2 +- src/API/Heizungsart.php | 2 +- src/API/Objektkategorie.php | 2 +- src/API/Openimmo.php | 2 +- src/API/Serviceleistungen.php | 2 +- src/API/Stellplatzart.php | 2 +- src/API/StpCarport.php | 7 +++++-- src/API/StpDuplex.php | 7 +++++-- src/API/StpFreiplatz.php | 7 +++++-- src/API/StpGarage.php | 7 +++++-- src/API/StpParkhaus.php | 7 +++++-- src/API/StpTiefgarage.php | 7 +++++-- src/API/Vermarktungsart.php | 2 +- src/API/Versteigerung.php | 2 +- 17 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/API/Anhaenge.php b/src/API/Anhaenge.php index bbfab6c..cf41db9 100644 --- a/src/API/Anhaenge.php +++ b/src/API/Anhaenge.php @@ -102,7 +102,7 @@ public function __construct( array $anhang = [], array $userDefinedSimplefield = [], array $userDefinedAnyfield = [], - array $userDefinedExtend = [] + array $userDefinedExtend = [], ) { $this->anhang = $anhang; $this->userDefinedSimplefield = $userDefinedSimplefield; diff --git a/src/API/Anhang.php b/src/API/Anhang.php index df65bb7..0bf16af 100644 --- a/src/API/Anhang.php +++ b/src/API/Anhang.php @@ -136,7 +136,7 @@ public function __construct( ?string $anhangtitel = null, string $format = '', ?Check $check = null, - ?Daten $daten = null + ?Daten $daten = null, ) { $this->location = $location; $this->gruppe = $gruppe; diff --git a/src/API/Ausbaustufe.php b/src/API/Ausbaustufe.php index 0ea9fdd..f2a4d4a 100644 --- a/src/API/Ausbaustufe.php +++ b/src/API/Ausbaustufe.php @@ -114,7 +114,7 @@ public function __construct( ?bool $ausbauhaus = null, ?bool $schluesselfertigmitkeller = null, ?bool $schluesselfertigohnebodenplatte = null, - ?bool $schluesselfertigmitbodenplatte = null + ?bool $schluesselfertigmitbodenplatte = null, ) { $this->bausatzhaus = $bausatzhaus; $this->ausbauhaus = $ausbauhaus; diff --git a/src/API/Bad.php b/src/API/Bad.php index 56b2255..34ffabe 100644 --- a/src/API/Bad.php +++ b/src/API/Bad.php @@ -114,7 +114,7 @@ public function __construct( ?bool $wanne = null, ?bool $fenster = null, ?bool $bidet = null, - ?bool $pissoir = null + ?bool $pissoir = null, ) { $this->dusche = $dusche; $this->wanne = $wanne; diff --git a/src/API/Heizungsart.php b/src/API/Heizungsart.php index e8f9908..c5524ed 100644 --- a/src/API/Heizungsart.php +++ b/src/API/Heizungsart.php @@ -114,7 +114,7 @@ public function __construct( ?bool $etage = null, ?bool $zentral = null, ?bool $fern = null, - ?bool $fussboden = null + ?bool $fussboden = null, ) { $this->ofen = $ofen; $this->etage = $etage; diff --git a/src/API/Objektkategorie.php b/src/API/Objektkategorie.php index 619ad68..70ac39b 100644 --- a/src/API/Objektkategorie.php +++ b/src/API/Objektkategorie.php @@ -125,7 +125,7 @@ public function __construct( ?Objektart $objektart = null, array $userDefinedSimplefield = [], array $userDefinedAnyfield = [], - array $userDefinedExtend = [] + array $userDefinedExtend = [], ) { $this->nutzungsart = $nutzungsart; $this->vermarktungsart = $vermarktungsart; diff --git a/src/API/Openimmo.php b/src/API/Openimmo.php index b65cc3e..bc769f2 100644 --- a/src/API/Openimmo.php +++ b/src/API/Openimmo.php @@ -96,7 +96,7 @@ public function __construct( ?Uebertragung $uebertragung = null, array $anbieter = [], array $userDefinedSimplefield = [], - array $userDefinedAnyfield = [] + array $userDefinedAnyfield = [], ) { $this->uebertragung = $uebertragung; $this->anbieter = $anbieter; diff --git a/src/API/Serviceleistungen.php b/src/API/Serviceleistungen.php index 389f6b4..8563e9f 100644 --- a/src/API/Serviceleistungen.php +++ b/src/API/Serviceleistungen.php @@ -114,7 +114,7 @@ public function __construct( ?bool $catering = null, ?bool $reinigung = null, ?bool $einkauf = null, - ?bool $wachdienst = null + ?bool $wachdienst = null, ) { $this->betreutesWohnen = $betreutesWohnen; $this->catering = $catering; diff --git a/src/API/Stellplatzart.php b/src/API/Stellplatzart.php index 88bee47..33fbff2 100644 --- a/src/API/Stellplatzart.php +++ b/src/API/Stellplatzart.php @@ -134,7 +134,7 @@ public function __construct( ?bool $carport = null, ?bool $freiplatz = null, ?bool $parkhaus = null, - ?bool $duplex = null + ?bool $duplex = null, ) { $this->garage = $garage; $this->tiefgarage = $tiefgarage; diff --git a/src/API/StpCarport.php b/src/API/StpCarport.php index beed7e4..cba0644 100644 --- a/src/API/StpCarport.php +++ b/src/API/StpCarport.php @@ -69,8 +69,11 @@ public function setAnzahl(?int $anzahl): StpCarport return $this; } - public function __construct(?float $stellplatzmiete = null, ?float $stellplatzkaufpreis = null, ?int $anzahl = null) - { + public function __construct( + ?float $stellplatzmiete = null, + ?float $stellplatzkaufpreis = null, + ?int $anzahl = null, + ) { $this->stellplatzmiete = $stellplatzmiete; $this->stellplatzkaufpreis = $stellplatzkaufpreis; $this->anzahl = $anzahl; diff --git a/src/API/StpDuplex.php b/src/API/StpDuplex.php index 44c9ab7..3d2284f 100644 --- a/src/API/StpDuplex.php +++ b/src/API/StpDuplex.php @@ -69,8 +69,11 @@ public function setAnzahl(?int $anzahl): StpDuplex return $this; } - public function __construct(?float $stellplatzmiete = null, ?float $stellplatzkaufpreis = null, ?int $anzahl = null) - { + public function __construct( + ?float $stellplatzmiete = null, + ?float $stellplatzkaufpreis = null, + ?int $anzahl = null, + ) { $this->stellplatzmiete = $stellplatzmiete; $this->stellplatzkaufpreis = $stellplatzkaufpreis; $this->anzahl = $anzahl; diff --git a/src/API/StpFreiplatz.php b/src/API/StpFreiplatz.php index 015cb7e..188d92a 100644 --- a/src/API/StpFreiplatz.php +++ b/src/API/StpFreiplatz.php @@ -69,8 +69,11 @@ public function setAnzahl(?int $anzahl): StpFreiplatz return $this; } - public function __construct(?float $stellplatzmiete = null, ?float $stellplatzkaufpreis = null, ?int $anzahl = null) - { + public function __construct( + ?float $stellplatzmiete = null, + ?float $stellplatzkaufpreis = null, + ?int $anzahl = null, + ) { $this->stellplatzmiete = $stellplatzmiete; $this->stellplatzkaufpreis = $stellplatzkaufpreis; $this->anzahl = $anzahl; diff --git a/src/API/StpGarage.php b/src/API/StpGarage.php index ea95f0d..b1cd5e3 100644 --- a/src/API/StpGarage.php +++ b/src/API/StpGarage.php @@ -69,8 +69,11 @@ public function setAnzahl(?int $anzahl): StpGarage return $this; } - public function __construct(?float $stellplatzmiete = null, ?float $stellplatzkaufpreis = null, ?int $anzahl = null) - { + public function __construct( + ?float $stellplatzmiete = null, + ?float $stellplatzkaufpreis = null, + ?int $anzahl = null, + ) { $this->stellplatzmiete = $stellplatzmiete; $this->stellplatzkaufpreis = $stellplatzkaufpreis; $this->anzahl = $anzahl; diff --git a/src/API/StpParkhaus.php b/src/API/StpParkhaus.php index d2ae907..44856fe 100644 --- a/src/API/StpParkhaus.php +++ b/src/API/StpParkhaus.php @@ -69,8 +69,11 @@ public function setAnzahl(?int $anzahl): StpParkhaus return $this; } - public function __construct(?float $stellplatzmiete = null, ?float $stellplatzkaufpreis = null, ?int $anzahl = null) - { + public function __construct( + ?float $stellplatzmiete = null, + ?float $stellplatzkaufpreis = null, + ?int $anzahl = null, + ) { $this->stellplatzmiete = $stellplatzmiete; $this->stellplatzkaufpreis = $stellplatzkaufpreis; $this->anzahl = $anzahl; diff --git a/src/API/StpTiefgarage.php b/src/API/StpTiefgarage.php index 49cf59b..f558227 100644 --- a/src/API/StpTiefgarage.php +++ b/src/API/StpTiefgarage.php @@ -69,8 +69,11 @@ public function setAnzahl(?int $anzahl): StpTiefgarage return $this; } - public function __construct(?float $stellplatzmiete = null, ?float $stellplatzkaufpreis = null, ?int $anzahl = null) - { + public function __construct( + ?float $stellplatzmiete = null, + ?float $stellplatzkaufpreis = null, + ?int $anzahl = null, + ) { $this->stellplatzmiete = $stellplatzmiete; $this->stellplatzkaufpreis = $stellplatzkaufpreis; $this->anzahl = $anzahl; diff --git a/src/API/Vermarktungsart.php b/src/API/Vermarktungsart.php index 8a233fb..48c3345 100644 --- a/src/API/Vermarktungsart.php +++ b/src/API/Vermarktungsart.php @@ -94,7 +94,7 @@ public function __construct( bool $kauf = false, bool $mietePacht = false, ?bool $erbpacht = null, - ?bool $leasing = null + ?bool $leasing = null, ) { $this->kauf = $kauf; $this->mietePacht = $mietePacht; diff --git a/src/API/Versteigerung.php b/src/API/Versteigerung.php index ac01e7f..b198b4f 100644 --- a/src/API/Versteigerung.php +++ b/src/API/Versteigerung.php @@ -102,7 +102,7 @@ public function __construct( ?\DateTime $zvtermin = null, ?\DateTime $zusatztermin = null, ?string $amtsgericht = null, - ?float $verkehrswert = null + ?float $verkehrswert = null, ) { $this->zwangsversteigerung = $zwangsversteigerung; $this->aktenzeichen = $aktenzeichen; From 39aa9ec848cd7a663419895e4a9b7533055165d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Grundko=CC=88tter?= Date: Thu, 9 Nov 2023 09:29:14 +0100 Subject: [PATCH 05/11] [TASK] dropped support for php 7.4 and 8.0 - updated minimum library versions - excluded phpunit 10 as there are some breaking changes for data providers --- composer.json | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index 96cee25..7f168f2 100644 --- a/composer.json +++ b/composer.json @@ -12,39 +12,32 @@ } ], "require": { - "php": ">=7.4", + "php": ">=8.1", "ext-dom": "*", "ext-simplexml": "*", "jms/serializer": "^2.1 || ^3.5", "cweagans/composer-patches": "^1.7 || ^2.0" }, "extra": { + "platform": { + "php": "8.1" + }, "patches": { "goetas-webservices/xsd-reader": { "Fix https://github.com/goetas-webservices/xsd-reader/issues/50": "patches/SchemaReader-ComplexTypeMixed-Issue-3.patch" } } }, - "repositories": [ - { - "type": "git", - "url": "https://github.com/bnf/php-code-generator.git" - }, - { - "type": "git", - "url": "https://github.com/AlexAzartsev/php-code-formatter.git" - } - ], "require-dev": { "ext-json": "*", - "goetas-webservices/xsd-reader": "^0.3", - "phpunit/phpunit": ">=7.5", + "goetas-webservices/xsd-reader": "^0.4", + "phpunit/phpunit": ">=7.5 <10", "phpstan/phpstan": "^1.0", "infection/infection": ">=0.16", - "friendsofphp/php-cs-fixer": "^2.18 || ^3.0", + "friendsofphp/php-cs-fixer": "^3.0", "rector/rector": ">=0.9", "symfony/serializer": "^5.3", - "nette/php-generator": "^3.6 || ^4.0" + "nette/php-generator": "^4.0" }, "minimum-stability": "dev", "prefer-stable": true, From f4de612253b10fc29adf5724054c76139722dc9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Grundko=CC=88tter?= Date: Thu, 9 Nov 2023 09:31:19 +0100 Subject: [PATCH 06/11] [TASK] dropped support for php 7.4 and 8.0, added 8.2 --- .github/workflows/php.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 4795dda..d3641df 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -17,7 +17,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@2.19.1 with: - php-version: '7.4' + php-version: '8.1' tools: composer extensions: dom, simplexml @@ -42,7 +42,7 @@ jobs: strategy: fail-fast: false matrix: - php: ['7.4', '8.0', '8.1'] + php: ['8.1', '8.2'] steps: - uses: actions/checkout@v3 @@ -91,7 +91,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@2.19.1 with: - php-version: '7.4' + php-version: '8.1' tools: composer coverage: none From 6023412c1fbe024acdedc1cabc0d6d318331dea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Grundko=CC=88tter?= Date: Thu, 9 Nov 2023 09:42:17 +0100 Subject: [PATCH 07/11] updated rector and executed it on the project files --- build/config/rector.php | 24 +++++---- src/Generator/ApiGenerator.php | 46 +++-------------- src/Generator/CodeGenUtil.php | 23 --------- src/Generator/TypeUtil.php | 49 +++++-------------- src/Handler/DateTimeHandler.php | 6 --- tests/API/ApiClassTest.php | 6 --- .../ApiGenerator/FileGeneratingTest.php | 10 ---- tests/Generator/TypeUtilTest.php | 8 --- 8 files changed, 29 insertions(+), 143 deletions(-) diff --git a/build/config/rector.php b/build/config/rector.php index 76a5218..606434d 100644 --- a/build/config/rector.php +++ b/build/config/rector.php @@ -2,20 +2,18 @@ declare(strict_types=1); -use Rector\Core\Configuration\Option; -use Rector\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector; -use Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector; -use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector; +use Rector\Config\RectorConfig; use Rector\Set\ValueObject\SetList; -use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; +use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector; -return static function (ContainerConfigurator $containerConfigurator): void { - $containerConfigurator->import(SetList::DEAD_CODE); +return static function (RectorConfig $rectorConfig): void { + // register single rule + $rectorConfig->rule(TypedPropertyFromStrictConstructorRector::class); - $parameters = $containerConfigurator->parameters(); - $parameters->set(Option::SKIP, [ - RemoveUnusedPrivateClassConstantRector::class, - RemoveUselessParamTagRector::class, - RemoveUselessReturnTagRector::class + // here we can define, what sets of rules will be applied + // tip: use "SetList" class to autocomplete sets with your IDE + $rectorConfig->sets([ + SetList::CODE_QUALITY, + SetList::DEAD_CODE ]); -}; +}; \ No newline at end of file diff --git a/src/Generator/ApiGenerator.php b/src/Generator/ApiGenerator.php index 9385faa..789a9a2 100644 --- a/src/Generator/ApiGenerator.php +++ b/src/Generator/ApiGenerator.php @@ -41,14 +41,10 @@ class ApiGenerator */ public const MAX_PROPERTIES_IN_CONSTRUCTOR = 6; - /** - * @var string - */ protected string $targetFolder = './src/API/'; /** * Additional elements may be referenced inside of MixedComplexTypes. - * @var array */ protected array $referencedInlineElements = []; @@ -56,8 +52,6 @@ class ApiGenerator * Generates the API classes. * * @param string $xsdFile file path - * @param bool $wipeTargetFolder - * @param ?string $targetFolder * * @throws \GoetasWebservices\XML\XSDReader\Exception\IOException * @throws \Exception @@ -139,8 +133,6 @@ private function parseElementDef($element): void /** * @param Extension|null $extension - * @param ClassType $class - * @param PhpNamespace $namespace */ private function addSimpleValue(?Extension $extension, ClassType $class, PhpNamespace $namespace): void { @@ -166,11 +158,7 @@ private function addSimpleValue(?Extension $extension, ClassType $class, PhpName CodeGenUtil::generateGetterAndSetter($classProperty, $class, true, !TypeUtil::isConstantsBasedProperty($classProperty)); } - /** - * @param ClassType $class - * - * @return void - */ + private function generateConstructor(ClassType $class): void { $constructor = $class->addMethod('__construct'); @@ -193,8 +181,6 @@ private function generateConstructor(ClassType $class): void /** * @param Element|ElementRef|ElementDef $property - * @param ClassType $class - * @param PhpNamespace $namespace */ private function parseProperty(ElementItem $property, ClassType $class, PhpNamespace $namespace): void { @@ -238,7 +224,7 @@ private function parseProperty(ElementItem $property, ClassType $class, PhpNames $namespace->addUse(SkipWhenEmpty::class); } - if ($property->getType()->getRestriction()) { + if ($property->getType()->getRestriction() !== null) { $this->parseRestriction( $property->getType()->getRestriction(), $property->getName(), @@ -252,8 +238,6 @@ private function parseProperty(ElementItem $property, ClassType $class, PhpNames /** * @param Item|Element|ElementRef|ElementDef|ElementItem $property - * - * @return string */ private function getPhpPropertyTypeFromXsdElement($property): string { @@ -263,13 +247,11 @@ private function getPhpPropertyTypeFromXsdElement($property): string } else { $propertyType = TypeUtil::camelize($property->getReferencedElement()->getName()); } + } elseif ($property->getType() instanceof ComplexType) { + $this->referencedInlineElements[] = $property; + $propertyType = TypeUtil::extractTypeForPhp($property->getType(), TypeUtil::camelize($property->getName(), true)); } else { - if ($property->getType() instanceof ComplexType) { - $this->referencedInlineElements[] = $property; - $propertyType = TypeUtil::extractTypeForPhp($property->getType(), TypeUtil::camelize($property->getName(), true)); - } else { - $propertyType = TypeUtil::extractTypeForPhp($property->getType()); - } + $propertyType = TypeUtil::extractTypeForPhp($property->getType()); } if (!($property instanceof Attribute) && $property->getMax() == -1) { @@ -279,11 +261,6 @@ private function getPhpPropertyTypeFromXsdElement($property): string return $propertyType; } - /** - * @param Attribute $attribute - * @param ClassType $class - * @param PhpNamespace $namespace - */ private function parseAttribute(Attribute $attribute, ClassType $class, PhpNamespace $namespace): void { $propertyName = TypeUtil::camelize(strtolower($attribute->getName()), true); @@ -326,12 +303,6 @@ private function parseAttribute(Attribute $attribute, ClassType $class, PhpNames CodeGenUtil::generateGetterAndSetter($classProperty, $class, true, $nullable); } - /** - * @param Restriction $restriction - * @param string $nameInXsd - * @param ClassType $class - * @param Property $classProperty - */ private function parseRestriction(Restriction $restriction, string $nameInXsd, ClassType $class, Property $classProperty): void { foreach ($restriction->getChecks() as $type => $options) { @@ -384,8 +355,6 @@ private function wipeTargetFolder(): void } /** - * @param PhpNamespace $namespace - * @param ClassType $class * * @return bool|int */ @@ -398,9 +367,6 @@ private function createPhpFile(PhpNamespace $namespace, ClassType $class) return file_put_contents($this->getTargetFolder() . $class->getName() . '.php', $code); } - /** - * @return string - */ public function getTargetFolder(): string { return $this->targetFolder; diff --git a/src/Generator/CodeGenUtil.php b/src/Generator/CodeGenUtil.php index 300e997..bc866a7 100644 --- a/src/Generator/CodeGenUtil.php +++ b/src/Generator/CodeGenUtil.php @@ -11,12 +11,6 @@ class CodeGenUtil /** * Adds a new description part to the given class property. - * - * @param Property $classProperty - * @param string $descriptionPart - * @param string $separator - * - * @return void */ public static function addDescriptionPart(Property $classProperty, string $descriptionPart, string $separator = self::DESCRIPTION_PART_DELIMTER): void { @@ -33,23 +27,12 @@ public static function addDescriptionPart(Property $classProperty, string $descr $classProperty->setComment(implode($separator, $currentDescriptionParts)); } - /** - * @param Property $property - * @param ClassType $class - * @param bool $fluentApi - * @param bool $nullable - */ public static function generateGetterAndSetter(Property $property, ClassType $class, bool $fluentApi = true, bool $nullable = false): void { self::generateGetter($property, $class, $nullable); self::generateSetter($property, $class, $fluentApi, $nullable); } - /** - * @param Property $property - * @param ClassType $class - * @param bool $nullable - */ public static function generateGetter(Property $property, ClassType $class, bool $nullable): void { $propertyType = $property->getType(); @@ -69,12 +52,6 @@ public static function generateGetter(Property $property, ClassType $class, bool } } - /** - * @param Property $property - * @param ClassType $class - * @param bool $fluentApi - * @param bool $nullable - */ public static function generateSetter(Property $property, ClassType $class, bool $fluentApi, bool $nullable): void { $setter = $class->addMethod('set' . ucfirst($property->getName())); diff --git a/src/Generator/TypeUtil.php b/src/Generator/TypeUtil.php index 8630ff2..07b919f 100644 --- a/src/Generator/TypeUtil.php +++ b/src/Generator/TypeUtil.php @@ -11,14 +11,10 @@ class TypeUtil { public const OPENIMMO_NAMESPACE = 'Ujamii\\OpenImmo\\API\\'; - /** - * @param string $type - * - * @return string - */ + public static function getTypeForSerializer(string $type): string { - $isPlural = substr($type, -2) === '[]'; + $isPlural = str_ends_with($type, '[]'); $singular = str_replace('[]', '', $type); switch ($singular) { case 'string': @@ -68,14 +64,10 @@ public static function getTypeForSerializer(string $type): string return $type; } - /** - * @param string $propertyType - * - * @return string - */ + public static function getValidPhpType(string $propertyType): string { - $isPlural = substr($propertyType, -2) === '[]'; + $isPlural = str_ends_with($propertyType, '[]'); if ($isPlural) { return 'array'; } @@ -122,8 +114,6 @@ public static function getValidPhpType(string $propertyType): string } /** - * @param string $propertyType - * @param bool $nullable * * @return false|float|int|string|null */ @@ -151,21 +141,15 @@ public static function getDefaultValueForType(string $propertyType, bool $nullab break; default: - if ('[]' === substr($propertyType, -2)) { - $defaultValue = []; - } else { - $defaultValue = null; - } + $defaultValue = str_ends_with($propertyType, '[]') ? [] : null; } return $nullable ? null : $defaultValue; } /** - * @param Type $typeFromXsd * @param string|null $propertyName * - * @return string|null */ public static function extractTypeForPhp(Type $typeFromXsd, ?string $propertyName = null): ?string { @@ -173,31 +157,22 @@ public static function extractTypeForPhp(Type $typeFromXsd, ?string $propertyNam if ($typeFromXsd->getName() != '') { $type = $typeFromXsd->getName(); - } else { - if ($typeFromXsd instanceof ComplexType) { - if (null !== $propertyName) { - return ucfirst($propertyName); - } - } else { - if ($typeFromXsd instanceof ComplexTypeSimpleContent) { - // is default string - } else { - if ($typeFromXsd->getRestriction()->getBase() != '') { - $type = $typeFromXsd->getRestriction()->getBase()->getName(); - } - } + } elseif ($typeFromXsd instanceof ComplexType) { + if (null !== $propertyName) { + return ucfirst($propertyName); } + } elseif ($typeFromXsd instanceof ComplexTypeSimpleContent) { + // is default string + } elseif ($typeFromXsd->getRestriction()->getBase() != '') { + $type = $typeFromXsd->getRestriction()->getBase()->getName(); } return $type; } /** - * @param string $input - * @param bool $lcFirst * @param array $separators * - * @return string */ public static function camelize(string $input, bool $lcFirst = false, array $separators = ['-', '_']): string { diff --git a/src/Handler/DateTimeHandler.php b/src/Handler/DateTimeHandler.php index 19c5a2d..4af4047 100644 --- a/src/Handler/DateTimeHandler.php +++ b/src/Handler/DateTimeHandler.php @@ -35,11 +35,8 @@ public static function getSubscribingMethods() } /** - * @param XmlSerializationVisitor $visitor - * @param \DateTime $date * @param array $type * - * @return \DOMText */ public function serializeDateTimeToXml(XmlSerializationVisitor $visitor, \DateTime $date, array $type): \DOMText { @@ -47,10 +44,7 @@ public function serializeDateTimeToXml(XmlSerializationVisitor $visitor, \DateTi } /** - * @param XmlDeserializationVisitor $visitor - * @param \SimpleXMLElement $dateAsString * - * @return \DateTime * @throws \Exception */ public function deserializeDateTimeToXml(XmlDeserializationVisitor $visitor, \SimpleXMLElement $dateAsString): \DateTime diff --git a/tests/API/ApiClassTest.php b/tests/API/ApiClassTest.php index f6cc0d9..d45443a 100644 --- a/tests/API/ApiClassTest.php +++ b/tests/API/ApiClassTest.php @@ -32,10 +32,6 @@ public function testClassProperty() /** * This automation creates a new instance of the given class and tests the * setter and getter for the given property. - * - * @param string $className - * @param string $propertyName - * @param string $type */ protected function automateTestClassProperties(string $className, string $propertyName, string $type) { @@ -48,8 +44,6 @@ protected function automateTestClassProperties(string $className, string $proper } /** - * @param string $type - * * @return array|bool|bool[]|\DateTime|\DateTime[]|float|float[]|int|int[]|object|object[]|string|string[] */ protected function getExampleData(string $type) diff --git a/tests/Generator/ApiGenerator/FileGeneratingTest.php b/tests/Generator/ApiGenerator/FileGeneratingTest.php index 678b7fd..017f791 100644 --- a/tests/Generator/ApiGenerator/FileGeneratingTest.php +++ b/tests/Generator/ApiGenerator/FileGeneratingTest.php @@ -36,10 +36,7 @@ public function tearDown(): void } /** - * @param string $nameInXsd - * @param string $docBlockComment * - * @return ClassType * @throws IOException */ public function getGeneratedClassFromFile(string $nameInXsd, string $docBlockComment = ''): ClassType @@ -66,9 +63,6 @@ public function getGeneratedClassFromFile(string $nameInXsd, string $docBlockCom } /** - * @param ClassType $generatedClass - * - * @return \ReflectionClass * @throws \ReflectionException */ public function getReflectionClassFromGeneratedClass(ClassType $generatedClass): \ReflectionClass @@ -80,10 +74,6 @@ public function getReflectionClassFromGeneratedClass(ClassType $generatedClass): return new \ReflectionClass(new $subjectClassName()); } - /** - * @param ClassType $generatedClass - * @param array $constants - */ public function assertClassHasConstants(ClassType $generatedClass, array $constants): void { foreach ($constants as $constantName => $constantValue) { diff --git a/tests/Generator/TypeUtilTest.php b/tests/Generator/TypeUtilTest.php index cd74a5d..27e0483 100644 --- a/tests/Generator/TypeUtilTest.php +++ b/tests/Generator/TypeUtilTest.php @@ -9,8 +9,6 @@ class TypeUtilTest extends TestCase { /** - * @param string $nameInXsd - * @param string $expectedPhpName * @param bool|null $lcFirst * * @dataProvider camelizeDataProvider @@ -37,8 +35,6 @@ public static function camelizeDataProvider(): array } /** - * @param string $xsdType - * @param string $serializerType * * @dataProvider getTypeForSerializerDataProvider */ @@ -73,8 +69,6 @@ public static function getTypeForSerializerDataProvider(): array } /** - * @param string $xsdType - * @param string $phpType * * @dataProvider getValidPhpTypeDataProvider */ @@ -111,8 +105,6 @@ public static function getValidPhpTypeDataProvider(): array } /** - * @param string $propertyType - * @param bool $nullable * @param mixed $defaultValue * * @dataProvider getDefaultValueForTypeDataProvider From 080ae0721b9bfc0e80785806302613fb3146520f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Grundko=CC=88tter?= Date: Thu, 9 Nov 2023 09:42:56 +0100 Subject: [PATCH 08/11] updated package versions --- .github/workflows/php.yml | 14 +++++++------- composer.json | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index d3641df..b620f76 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -12,10 +12,10 @@ jobs: fail-fast: false steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup PHP - uses: shivammathur/setup-php@2.19.1 + uses: shivammathur/setup-php@2.27.1 with: php-version: '8.1' tools: composer @@ -45,10 +45,10 @@ jobs: php: ['8.1', '8.2'] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup PHP - uses: shivammathur/setup-php@2.19.1 + uses: shivammathur/setup-php@2.27.1 with: php-version: ${{ matrix.php }} tools: composer @@ -70,7 +70,7 @@ jobs: - name: Run test suite run: composer run phpunit - - uses: codecov/codecov-action@v3.1.1 + - uses: codecov/codecov-action@v3.1.4 with: token: ${{ secrets.CODECOV_TOKEN }} files: ./build/coverage/clover.xml @@ -86,10 +86,10 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Setup PHP - uses: shivammathur/setup-php@2.19.1 + uses: shivammathur/setup-php@2.27.1 with: php-version: '8.1' tools: composer diff --git a/composer.json b/composer.json index 7f168f2..ec804bd 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,7 @@ "phpstan/phpstan": "^1.0", "infection/infection": ">=0.16", "friendsofphp/php-cs-fixer": "^3.0", - "rector/rector": ">=0.9", + "rector/rector": ">=0.18", "symfony/serializer": "^5.3", "nette/php-generator": "^4.0" }, From 3ddb4c78464bb308eb516b0098d8fec488614bf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Grundko=CC=88tter?= Date: Thu, 9 Nov 2023 09:49:06 +0100 Subject: [PATCH 09/11] added php 8.3 to matrix test config --- .github/workflows/php.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index b620f76..5de7ae4 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -42,7 +42,7 @@ jobs: strategy: fail-fast: false matrix: - php: ['8.1', '8.2'] + php: ['8.1', '8.2', '8.3'] steps: - uses: actions/checkout@v4 From 38660a0944f9f90a9f991cc210eecb952d215c63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Grundko=CC=88tter?= Date: Thu, 9 Nov 2023 10:24:43 +0100 Subject: [PATCH 10/11] [DOC] adjusted php version info --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f504f01..0bfff6a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # OpenImmo PHP library [![Packagist](https://img.shields.io/packagist/v/ujamii/openimmo.svg?colorB=green&style=flat)](https://packagist.org/packages/ujamii/openimmo) -[![Minimum PHP Version](https://img.shields.io/badge/php-7.4%2B-8892BF.svg?style=flat)](https://php.net/) +[![Minimum PHP Version](https://img.shields.io/badge/php-8.1%2B-8892BF.svg?style=flat)](https://php.net/) [![Continuous Integration](https://github.com/ujamii/openimmo/actions/workflows/php.yml/badge.svg)](https://github.com/ujamii/openimmo/actions) [![codecov](https://codecov.io/gh/ujamii/openimmo/branch/master/graph/badge.svg?token=97D799UX1B)](https://codecov.io/gh/ujamii/openimmo) [![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fujamii%2Fopenimmo%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/ujamii/openimmo/master) @@ -9,7 +9,8 @@ OpenImmo and the OpenImmo logo are registered trademarks of the [OpenImmo e.V.](http://www.openimmo.de) Neither is this package an official distribution nor am I associated with this organisation! -This library just wraps the OpenImmo XML format with some PHP7/8 classes. +This library just wraps the OpenImmo XML format with some PHP8 classes. +If you need support for PHP <= 8.1, see version 1.x of this package. Version 2 is for PHP >=8.2 only. There is an official library available at http://www.openimmo.de/go.php/p/22/support20.htm which costs 95 EUR excl. VAT and is PHP5 only. To completely convince you, you will only be allowed to see the code **after** you have paid and they have a no-refund policy. @@ -118,7 +119,7 @@ foreach ($openImmo->getAnbieter() as $anbieter) { Although the OpenImmo standard just describes an XML version, there may be cases when you want to generate JSON from the given data. Sadly, there is [an issue](https://github.com/schmittjoh/serializer/issues/1251) with custom types, scalar values and JSON serialization in the JMS serializer. -Nevertheless it is still possible to write JSON format with the [Symfony serializer component](https://symfony.com/doc/current/components/serializer.html). +However, it is still possible to write JSON format with the [Symfony serializer component](https://symfony.com/doc/current/components/serializer.html). ```shell composer require symfony/serializer From 4aba49a3fbc6200400e1ef21c9670beda8d07309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Grundko=CC=88tter?= Date: Thu, 9 Nov 2023 10:36:28 +0100 Subject: [PATCH 11/11] - code style fixes - fixed log path of infection logs - raised memory limit for phpstan --- build/config/infection.json.dist | 4 ++-- composer.json | 2 +- src/Generator/ApiGenerator.php | 3 +-- src/Generator/TypeUtil.php | 3 --- src/Handler/DateTimeHandler.php | 2 -- tests/Generator/ApiGenerator/FileGeneratingTest.php | 1 - tests/Generator/TypeUtilTest.php | 2 -- 7 files changed, 4 insertions(+), 13 deletions(-) diff --git a/build/config/infection.json.dist b/build/config/infection.json.dist index ee30071..78bccc1 100644 --- a/build/config/infection.json.dist +++ b/build/config/infection.json.dist @@ -8,8 +8,8 @@ "configDir": ".\/" }, "logs": { - "text": "./build/coverage/infection-full.log", - "summary": "./build/coverage/infection-summary.log", + "text": "../coverage/infection-full.log", + "summary": "../coverage/infection-summary.log", "github": true, "stryker": { "badge": "master" diff --git a/composer.json b/composer.json index ec804bd..1183e6e 100644 --- a/composer.json +++ b/composer.json @@ -57,7 +57,7 @@ "@php-cs-fixer" ], "phpunit": "vendor/bin/phpunit -c build/config/phpunit.xml.dist", - "phpstan": "vendor/bin/phpstan analyse -c build/config/phpstan.neon", + "phpstan": "vendor/bin/phpstan --memory-limit=1G analyse -c build/config/phpstan.neon", "rector": "vendor/bin/rector process src tests -c build/config/rector.php --xdebug", "php-cs-fixer": "vendor/bin/php-cs-fixer fix --config build/config/cs.php", "infection": "vendor/bin/infection --only-covered --configuration=./build/config/infection.json.dist --threads=8 --min-msi=99", diff --git a/src/Generator/ApiGenerator.php b/src/Generator/ApiGenerator.php index 789a9a2..6f2079e 100644 --- a/src/Generator/ApiGenerator.php +++ b/src/Generator/ApiGenerator.php @@ -158,7 +158,7 @@ private function addSimpleValue(?Extension $extension, ClassType $class, PhpName CodeGenUtil::generateGetterAndSetter($classProperty, $class, true, !TypeUtil::isConstantsBasedProperty($classProperty)); } - + private function generateConstructor(ClassType $class): void { $constructor = $class->addMethod('__construct'); @@ -355,7 +355,6 @@ private function wipeTargetFolder(): void } /** - * * @return bool|int */ private function createPhpFile(PhpNamespace $namespace, ClassType $class) diff --git a/src/Generator/TypeUtil.php b/src/Generator/TypeUtil.php index 07b919f..0dbb43c 100644 --- a/src/Generator/TypeUtil.php +++ b/src/Generator/TypeUtil.php @@ -114,7 +114,6 @@ public static function getValidPhpType(string $propertyType): string } /** - * * @return false|float|int|string|null */ public static function getDefaultValueForType(string $propertyType, bool $nullable) @@ -149,7 +148,6 @@ public static function getDefaultValueForType(string $propertyType, bool $nullab /** * @param string|null $propertyName - * */ public static function extractTypeForPhp(Type $typeFromXsd, ?string $propertyName = null): ?string { @@ -172,7 +170,6 @@ public static function extractTypeForPhp(Type $typeFromXsd, ?string $propertyNam /** * @param array $separators - * */ public static function camelize(string $input, bool $lcFirst = false, array $separators = ['-', '_']): string { diff --git a/src/Handler/DateTimeHandler.php b/src/Handler/DateTimeHandler.php index 4af4047..cc8c747 100644 --- a/src/Handler/DateTimeHandler.php +++ b/src/Handler/DateTimeHandler.php @@ -36,7 +36,6 @@ public static function getSubscribingMethods() /** * @param array $type - * */ public function serializeDateTimeToXml(XmlSerializationVisitor $visitor, \DateTime $date, array $type): \DOMText { @@ -44,7 +43,6 @@ public function serializeDateTimeToXml(XmlSerializationVisitor $visitor, \DateTi } /** - * * @throws \Exception */ public function deserializeDateTimeToXml(XmlDeserializationVisitor $visitor, \SimpleXMLElement $dateAsString): \DateTime diff --git a/tests/Generator/ApiGenerator/FileGeneratingTest.php b/tests/Generator/ApiGenerator/FileGeneratingTest.php index 017f791..5337449 100644 --- a/tests/Generator/ApiGenerator/FileGeneratingTest.php +++ b/tests/Generator/ApiGenerator/FileGeneratingTest.php @@ -36,7 +36,6 @@ public function tearDown(): void } /** - * * @throws IOException */ public function getGeneratedClassFromFile(string $nameInXsd, string $docBlockComment = ''): ClassType diff --git a/tests/Generator/TypeUtilTest.php b/tests/Generator/TypeUtilTest.php index 27e0483..aed3503 100644 --- a/tests/Generator/TypeUtilTest.php +++ b/tests/Generator/TypeUtilTest.php @@ -35,7 +35,6 @@ public static function camelizeDataProvider(): array } /** - * * @dataProvider getTypeForSerializerDataProvider */ public function testGetTypeForSerializer(string $xsdType, string $serializerType): void @@ -69,7 +68,6 @@ public static function getTypeForSerializerDataProvider(): array } /** - * * @dataProvider getValidPhpTypeDataProvider */ public function testGetValidPhpType(string $xsdType, string $phpType): void