diff --git a/src/Generator/Php.php b/src/Generator/Php.php index ced3b20a..9631bc00 100644 --- a/src/Generator/Php.php +++ b/src/Generator/Php.php @@ -158,6 +158,8 @@ protected function writeStruct(Code\Name $name, array $properties, ?string $exte $prop->addAttribute($attribute); } + $prop->setDefault(null); + $class->addStmt($prop); $setter = $this->factory->method($property->getName()->getMethod(prefix: ['set'])); diff --git a/tests/Generator/resource/php/php.php b/tests/Generator/resource/php/php.php index d7cb8216..c69cb41e 100644 --- a/tests/Generator/resource/php/php.php +++ b/tests/Generator/resource/php/php.php @@ -3,8 +3,8 @@ #[Description('Location of the person')] class Location implements \JsonSerializable, \PSX\Record\RecordableInterface { - protected ?float $lat; - protected ?float $long; + protected ?float $lat = null; + protected ?float $long = null; public function setLat(?float $lat) : void { $this->lat = $lat; @@ -41,20 +41,20 @@ public function jsonSerialize() : object #[Description('An simple author element with some description')] class Author implements \JsonSerializable, \PSX\Record\RecordableInterface { - protected ?string $title; + protected ?string $title = null; #[Description('We will send no spam to this address')] #[Nullable(true)] - protected ?string $email; + protected ?string $email = null; /** * @var array|null */ - protected ?array $categories; + protected ?array $categories = null; /** * @var array|null */ #[Description('Array of locations')] - protected ?array $locations; - protected ?Location $origin; + protected ?array $locations = null; + protected ?Location $origin = null; public function setTitle(?string $title) : void { $this->title = $title; @@ -138,47 +138,47 @@ class Meta extends \ArrayObject #[Description('An general news entry')] class News implements \JsonSerializable, \PSX\Record\RecordableInterface { - protected ?Meta $config; + protected ?Meta $config = null; /** * @var \PSX\Record\Record|null */ - protected ?\PSX\Record\Record $inlineConfig; + protected ?\PSX\Record\Record $inlineConfig = null; /** * @var \PSX\Record\Record|null */ - protected ?\PSX\Record\Record $mapTags; + protected ?\PSX\Record\Record $mapTags = null; /** * @var \PSX\Record\Record|null */ - protected ?\PSX\Record\Record $mapReceiver; + protected ?\PSX\Record\Record $mapReceiver = null; /** * @var array|null */ - protected ?array $tags; + protected ?array $tags = null; /** * @var array|null */ - protected ?array $receiver; - protected ?bool $read; - protected ?Author $author; - protected ?Meta $meta; + protected ?array $receiver = null; + protected ?bool $read = null; + protected ?Author $author = null; + protected ?Meta $meta = null; #[Format('date')] - protected ?\PSX\DateTime\LocalDate $sendDate; + protected ?\PSX\DateTime\LocalDate $sendDate = null; #[Format('date-time')] - protected ?\PSX\DateTime\LocalDateTime $readDate; - protected ?float $price; - protected ?int $rating; + protected ?\PSX\DateTime\LocalDateTime $readDate = null; + protected ?float $price = null; + protected ?int $rating = null; #[Description('Contains the main content of the news entry')] - protected ?string $content; - protected ?string $question; - protected ?string $version; + protected ?string $content = null; + protected ?string $question = null; + protected ?string $version = null; #[Format('time')] - protected ?\PSX\DateTime\LocalTime $coffeeTime; + protected ?\PSX\DateTime\LocalTime $coffeeTime = null; #[Key('g-recaptcha-response')] - protected ?string $captcha; + protected ?string $captcha = null; #[Key('media.fields')] - protected ?string $mediaFields; - protected mixed $payload; + protected ?string $mediaFields = null; + protected mixed $payload = null; public function setConfig(?Meta $config) : void { $this->config = $config; diff --git a/tests/Generator/resource/php/php_complex.php b/tests/Generator/resource/php/php_complex.php index 0fcb6583..d277093b 100644 --- a/tests/Generator/resource/php/php_complex.php +++ b/tests/Generator/resource/php/php_complex.php @@ -10,11 +10,11 @@ abstract class DefinitionType implements \JsonSerializable, \PSX\Record\RecordableInterface { #[Description('')] - protected ?string $description; + protected ?string $description = null; #[Description('')] - protected ?bool $deprecated; + protected ?bool $deprecated = null; #[Description('')] - protected ?string $type; + protected ?string $type = null; public function setDescription(?string $description) : void { $this->description = $description; @@ -60,28 +60,28 @@ public function jsonSerialize() : object class StructDefinitionType extends DefinitionType implements \JsonSerializable, \PSX\Record\RecordableInterface { #[Description('')] - protected ?string $type; + protected ?string $type = null; #[Description('The parent type of this struct. The struct inherits all properties from the parent type')] - protected ?string $parent; + protected ?string $parent = null; #[Description('Indicates that this struct is a base type, this means it is an abstract type which is used by different types as parent')] - protected ?bool $base; + protected ?bool $base = null; /** * @var \PSX\Record\Record|null */ #[Description('')] - protected ?\PSX\Record\Record $properties; + protected ?\PSX\Record\Record $properties = null; #[Description('In case this is a base type it is possible to specify a discriminator property')] - protected ?string $discriminator; + protected ?string $discriminator = null; /** * @var \PSX\Record\Record|null */ #[Description('In case a discriminator property was set it is possible to specify a mapping. The key is the type name and the value the concrete value which is mapped to the type')] - protected ?\PSX\Record\Record $mapping; + protected ?\PSX\Record\Record $mapping = null; /** * @var \PSX\Record\Record|null */ #[Description('In case the parent type contains generics it is possible to set a concrete type for each generic type')] - protected ?\PSX\Record\Record $template; + protected ?\PSX\Record\Record $template = null; public function setType(?string $type) : void { $this->type = $type; @@ -168,9 +168,9 @@ public function jsonSerialize() : object abstract class CollectionDefinitionType extends DefinitionType implements \JsonSerializable, \PSX\Record\RecordableInterface { #[Description('')] - protected ?string $type; + protected ?string $type = null; #[Description('')] - protected ?PropertyType $schema; + protected ?PropertyType $schema = null; public function setType(?string $type) : void { $this->type = $type; @@ -207,7 +207,7 @@ public function jsonSerialize() : object class MapDefinitionType extends CollectionDefinitionType implements \JsonSerializable, \PSX\Record\RecordableInterface { #[Description('')] - protected ?string $type; + protected ?string $type = null; public function setType(?string $type) : void { $this->type = $type; @@ -235,7 +235,7 @@ public function jsonSerialize() : object class ArrayDefinitionType extends CollectionDefinitionType implements \JsonSerializable, \PSX\Record\RecordableInterface { #[Description('')] - protected ?string $type; + protected ?string $type = null; public function setType(?string $type) : void { $this->type = $type; @@ -275,13 +275,13 @@ public function jsonSerialize() : object abstract class PropertyType implements \JsonSerializable, \PSX\Record\RecordableInterface { #[Description('')] - protected ?string $description; + protected ?string $description = null; #[Description('')] - protected ?bool $deprecated; + protected ?bool $deprecated = null; #[Description('')] - protected ?string $type; + protected ?string $type = null; #[Description('')] - protected ?bool $nullable; + protected ?bool $nullable = null; public function setDescription(?string $description) : void { $this->description = $description; @@ -343,7 +343,7 @@ public function jsonSerialize() : object abstract class ScalarPropertyType extends PropertyType implements \JsonSerializable, \PSX\Record\RecordableInterface { #[Description('')] - protected ?string $type; + protected ?string $type = null; public function setType(?string $type) : void { $this->type = $type; @@ -371,7 +371,7 @@ public function jsonSerialize() : object class IntegerPropertyType extends ScalarPropertyType implements \JsonSerializable, \PSX\Record\RecordableInterface { #[Description('')] - protected ?string $type; + protected ?string $type = null; public function setType(?string $type) : void { $this->type = $type; @@ -399,7 +399,7 @@ public function jsonSerialize() : object class NumberPropertyType extends ScalarPropertyType implements \JsonSerializable, \PSX\Record\RecordableInterface { #[Description('')] - protected ?string $type; + protected ?string $type = null; public function setType(?string $type) : void { $this->type = $type; @@ -427,9 +427,9 @@ public function jsonSerialize() : object class StringPropertyType extends ScalarPropertyType implements \JsonSerializable, \PSX\Record\RecordableInterface { #[Description('')] - protected ?string $type; + protected ?string $type = null; #[Description('')] - protected ?string $format; + protected ?string $format = null; public function setType(?string $type) : void { $this->type = $type; @@ -466,7 +466,7 @@ public function jsonSerialize() : object class BooleanPropertyType extends ScalarPropertyType implements \JsonSerializable, \PSX\Record\RecordableInterface { #[Description('')] - protected ?string $type; + protected ?string $type = null; public function setType(?string $type) : void { $this->type = $type; @@ -499,9 +499,9 @@ public function jsonSerialize() : object abstract class CollectionPropertyType extends PropertyType implements \JsonSerializable, \PSX\Record\RecordableInterface { #[Description('')] - protected ?string $type; + protected ?string $type = null; #[Description('')] - protected ?PropertyType $schema; + protected ?PropertyType $schema = null; public function setType(?string $type) : void { $this->type = $type; @@ -538,7 +538,7 @@ public function jsonSerialize() : object class MapPropertyType extends CollectionPropertyType implements \JsonSerializable, \PSX\Record\RecordableInterface { #[Description('')] - protected ?string $type; + protected ?string $type = null; public function setType(?string $type) : void { $this->type = $type; @@ -566,7 +566,7 @@ public function jsonSerialize() : object class ArrayPropertyType extends CollectionPropertyType implements \JsonSerializable, \PSX\Record\RecordableInterface { #[Description('')] - protected ?string $type; + protected ?string $type = null; public function setType(?string $type) : void { $this->type = $type; @@ -594,7 +594,7 @@ public function jsonSerialize() : object class AnyPropertyType extends PropertyType implements \JsonSerializable, \PSX\Record\RecordableInterface { #[Description('')] - protected ?string $type; + protected ?string $type = null; public function setType(?string $type) : void { $this->type = $type; @@ -622,9 +622,9 @@ public function jsonSerialize() : object class GenericPropertyType extends PropertyType implements \JsonSerializable, \PSX\Record\RecordableInterface { #[Description('')] - protected ?string $type; + protected ?string $type = null; #[Description('The generic name, it is recommended to use typical generics names like T or TValue')] - protected ?string $name; + protected ?string $name = null; public function setType(?string $type) : void { $this->type = $type; @@ -661,9 +661,9 @@ public function jsonSerialize() : object class ReferencePropertyType extends PropertyType implements \JsonSerializable, \PSX\Record\RecordableInterface { #[Description('')] - protected ?string $type; + protected ?string $type = null; #[Description('Name of the target reference, must a key from the definitions map')] - protected ?string $target; + protected ?string $target = null; public function setType(?string $type) : void { $this->type = $type; @@ -703,14 +703,14 @@ class Specification implements \JsonSerializable, \PSX\Record\RecordableInterfac * @var \PSX\Record\Record|null */ #[Description('')] - protected ?\PSX\Record\Record $import; + protected ?\PSX\Record\Record $import = null; /** * @var \PSX\Record\Record|null */ #[Description('')] - protected ?\PSX\Record\Record $definitions; + protected ?\PSX\Record\Record $definitions = null; #[Description('')] - protected ?string $root; + protected ?string $root = null; public function setImport(?\PSX\Record\Record $import) : void { $this->import = $import; diff --git a/tests/Generator/resource/php/php_import.php b/tests/Generator/resource/php/php_import.php index 13088fcc..0c6abf16 100644 --- a/tests/Generator/resource/php/php_import.php +++ b/tests/Generator/resource/php/php_import.php @@ -1,7 +1,7 @@ class Import implements \JsonSerializable, \PSX\Record\RecordableInterface { - protected ?StudentMap $students; - protected ?Student $student; + protected ?StudentMap $students = null; + protected ?Student $student = null; public function setStudents(?StudentMap $students) : void { $this->students = $students; diff --git a/tests/Generator/resource/php/php_import_ns.php b/tests/Generator/resource/php/php_import_ns.php index 71136534..1647e499 100644 --- a/tests/Generator/resource/php/php_import_ns.php +++ b/tests/Generator/resource/php/php_import_ns.php @@ -3,8 +3,8 @@ class Import implements \JsonSerializable, \PSX\Record\RecordableInterface { - protected ?\My\Import\StudentMap $students; - protected ?\My\Import\Student $student; + protected ?\My\Import\StudentMap $students = null; + protected ?\My\Import\Student $student = null; public function setStudents(?\My\Import\StudentMap $students) : void { $this->students = $students; diff --git a/tests/Generator/resource/php/php_oop.php b/tests/Generator/resource/php/php_oop.php index 8612a696..ffc6fb7e 100644 --- a/tests/Generator/resource/php/php_oop.php +++ b/tests/Generator/resource/php/php_oop.php @@ -1,7 +1,7 @@ class Human implements \JsonSerializable, \PSX\Record\RecordableInterface { - protected ?string $firstName; - protected ?Human $parent; + protected ?string $firstName = null; + protected ?Human $parent = null; public function setFirstName(?string $firstName) : void { $this->firstName = $firstName; @@ -34,7 +34,7 @@ public function jsonSerialize() : object class Student extends Human implements \JsonSerializable, \PSX\Record\RecordableInterface { - protected ?string $matricleNumber; + protected ?string $matricleNumber = null; public function setMatricleNumber(?string $matricleNumber) : void { $this->matricleNumber = $matricleNumber; @@ -61,11 +61,11 @@ public function jsonSerialize() : object */ class Map implements \JsonSerializable, \PSX\Record\RecordableInterface { - protected ?int $totalResults; + protected ?int $totalResults = null; /** * @var array|null */ - protected ?array $entries; + protected ?array $entries = null; public function setTotalResults(?int $totalResults) : void { $this->totalResults = $totalResults; @@ -111,7 +111,7 @@ class StudentMap extends Map implements \JsonSerializable, \PSX\Record\Recordabl class RootSchema implements \JsonSerializable, \PSX\Record\RecordableInterface { - protected ?StudentMap $students; + protected ?StudentMap $students = null; public function setStudents(?StudentMap $students) : void { $this->students = $students;