diff --git a/CHANGELOG.md b/CHANGELOG.md index c889df39..171cf45a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # CHANGELOG +## 4.1.13 - 2024-06-21 +- issue #312, pr #313 - Invalid getter and annotation for nullable property + ## 4.1.12 - 2024-04-13 - issue #308, pr #309 - Symfony 7 / added support for symfony 7 diff --git a/src/File/AbstractModelFile.php b/src/File/AbstractModelFile.php index 635e8c53..a83b5299 100644 --- a/src/File/AbstractModelFile.php +++ b/src/File/AbstractModelFile.php @@ -400,7 +400,12 @@ protected function getStructAttributeTypeGetAnnotation(StructAttributeModel $att return '\\DOMDocument|string|null'; } - return sprintf('%s%s%s', $this->getStructAttributeTypeAsPhpType($attribute, false), $this->useBrackets($attribute, $returnArrayType) ? '[]' : '', !$nullableItemType && ($attribute->isRequired() || $attribute->isArray() || $attribute->isList()) ? '' : '|null'); + return sprintf( + '%s%s%s', + $this->getStructAttributeTypeAsPhpType($attribute, false), + $this->useBrackets($attribute, $returnArrayType) ? '[]' : '', + !$nullableItemType && !$attribute->isNullable() && ($attribute->isRequired() || $attribute->isArray() || $attribute->isList()) ? '' : '|null' + ); } protected function getStructAttributeTypeSetAnnotation(StructAttributeModel $attribute, bool $returnArrayType = true, bool $itemType = false): string @@ -413,7 +418,11 @@ protected function getStructAttributeTypeSetAnnotation(StructAttributeModel $att return 'array|string'; } - return sprintf('%s%s', $this->getStructAttributeTypeAsPhpType($attribute, $returnArrayType), $this->useBrackets($attribute, !$itemType) ? '[]' : ''); + return sprintf( + '%s%s', + $this->getStructAttributeTypeAsPhpType($attribute, $returnArrayType), + $this->useBrackets($attribute, !$itemType) ? '[]' : '' + ); } protected function useBrackets(StructAttributeModel $attribute, bool $returnArrayType = true): bool diff --git a/src/File/Struct.php b/src/File/Struct.php index 226c1d32..a035ca6c 100644 --- a/src/File/Struct.php +++ b/src/File/Struct.php @@ -347,17 +347,16 @@ protected function addStructMethodGetBodyReturn(PhpMethod $method, StructAttribu protected function addStructMethodGet(StructAttributeModel $attribute): self { - switch (true) { - // it can either be a string, a DOMDocument or null... - case $attribute->isXml(): - $returnType = ''; - - break; - - default: - $returnType = (!$attribute->getRemovableFromRequest() && !$attribute->isAChoice() && $attribute->isRequired() ? '' : '?').$this->getStructAttributeTypeAsPhpType($attribute); - - break; + // it can either be a string, a DOMDocument or null... + if ($attribute->isXml()) { + $returnType = ''; + } else { + $returnType = ( + !$attribute->getRemovableFromRequest() + && !$attribute->isAChoice() + && $attribute->isRequired() + && !$attribute->isNullable() ? '' : '?' + ).$this->getStructAttributeTypeAsPhpType($attribute); } $method = new PhpMethod( diff --git a/src/File/StructArray.php b/src/File/StructArray.php index 9893cc37..a867db33 100644 --- a/src/File/StructArray.php +++ b/src/File/StructArray.php @@ -40,11 +40,6 @@ public function addStructMethodsSetAndGet(): self return $this; } - protected function addClassElement(): AbstractModelFile - { - return AbstractModelFile::addClassElement(); - } - public function setModel(AbstractModel $model): self { if ($model instanceof StructModel && !$model->isArray()) { @@ -54,6 +49,11 @@ public function setModel(AbstractModel $model): self return parent::setModel($model); } + protected function addClassElement(): AbstractModelFile + { + return AbstractModelFile::addClassElement(); + } + /** * Disable this feature within StructArray class. */ diff --git a/tests/resources/generated/ValidBannerInfo.php b/tests/resources/generated/ValidBannerInfo.php index 1d6c7b09..c8b79821 100644 --- a/tests/resources/generated/ValidBannerInfo.php +++ b/tests/resources/generated/ValidBannerInfo.php @@ -79,7 +79,7 @@ class ApiBannerInfo extends AbstractStructBase * - base: soapenc:Array * - nillable: true * - ref: soapenc:arrayType - * @var \StructType\ApiBannerPhraseInfo[] + * @var \StructType\ApiBannerPhraseInfo[]|null */ protected ?array $Phrases = null; /** @@ -89,7 +89,7 @@ class ApiBannerInfo extends AbstractStructBase * - base: soapenc:Array * - nillable: true * - ref: soapenc:arrayType - * @var string[] + * @var string[]|null */ protected ?array $MinusKeywords = null; /** @@ -155,7 +155,7 @@ class ApiBannerInfo extends AbstractStructBase * - base: soapenc:Array * - nillable: true * - ref: soapenc:arrayType - * @var \StructType\ApiSitelink[] + * @var \StructType\ApiSitelink[]|null */ protected ?array $Sitelinks = null; /** @@ -165,7 +165,7 @@ class ApiBannerInfo extends AbstractStructBase * - base: soapenc:Array * - nillable: true * - ref: soapenc:arrayType - * @var string[] + * @var string[]|null */ protected ?array $AdWarnings = null; /** @@ -182,7 +182,7 @@ class ApiBannerInfo extends AbstractStructBase * - base: soapenc:Array * - nillable: true * - ref: soapenc:arrayType - * @var \StructType\ApiRejectReason[] + * @var \StructType\ApiRejectReason[]|null */ protected ?array $ModerateRejectionReasons = null; /** @@ -520,7 +520,7 @@ public function setGeo(?string $geo = null): self } /** * Get Phrases value - * @return \StructType\ApiBannerPhraseInfo[] + * @return \StructType\ApiBannerPhraseInfo[]|null */ public function getPhrases(): ?array { @@ -587,7 +587,7 @@ public function addToPhrases(\StructType\ApiBannerPhraseInfo $item): self } /** * Get MinusKeywords value - * @return string[] + * @return string[]|null */ public function getMinusKeywords(): ?array { @@ -838,7 +838,7 @@ public function setStatusSitelinksModerate(?string $statusSitelinksModerate = nu } /** * Get Sitelinks value - * @return \StructType\ApiSitelink[] + * @return \StructType\ApiSitelink[]|null */ public function getSitelinks(): ?array { @@ -905,7 +905,7 @@ public function addToSitelinks(\StructType\ApiSitelink $item): self } /** * Get AdWarnings value - * @return string[] + * @return string[]|null */ public function getAdWarnings(): ?array { @@ -995,7 +995,7 @@ public function setFixedOnModeration(?string $fixedOnModeration = null): self } /** * Get ModerateRejectionReasons value - * @return \StructType\ApiRejectReason[] + * @return \StructType\ApiRejectReason[]|null */ public function getModerateRejectionReasons(): ?array { diff --git a/tests/resources/generated/ValidHouseProfileData.php b/tests/resources/generated/ValidHouseProfileData.php index 303769a3..edb0268e 100644 --- a/tests/resources/generated/ValidHouseProfileData.php +++ b/tests/resources/generated/ValidHouseProfileData.php @@ -372,7 +372,7 @@ class ApiHouseProfileData extends AbstractStructBase * - base: soap-enc:Array * - nillable: true * - ref: soap-enc:arrayType - * @var \StructType\ApiLift[] + * @var \StructType\ApiLift[]|null */ protected ?array $lifts = null; /** @@ -1691,7 +1691,7 @@ public function setGas_system(?\StructType\ApiGasSystem $gas_system = null): sel } /** * Get lifts value - * @return \StructType\ApiLift[] + * @return \StructType\ApiLift[]|null */ public function getLifts(): ?array {