diff --git a/src/Generator/ParameterGenerator.php b/src/Generator/ParameterGenerator.php index 48434ab4..2787fc98 100644 --- a/src/Generator/ParameterGenerator.php +++ b/src/Generator/ParameterGenerator.php @@ -167,6 +167,14 @@ public function getType() : null; } + /** + * @return ?TypeGenerator + */ + public function getTypeObject() + { + return $this->type; + } + /** * @param string $name * @return ParameterGenerator diff --git a/src/Generator/PromotedParameterGenerator.php b/src/Generator/PromotedParameterGenerator.php index 56cd02cf..4aa89dc0 100644 --- a/src/Generator/PromotedParameterGenerator.php +++ b/src/Generator/PromotedParameterGenerator.php @@ -87,6 +87,13 @@ public static function fromParameterGeneratorWithVisibility(ParameterGenerator $ ); } + if (null !== $type + && ($typeObject = $generator->getTypeObject()) + && $typeObject->getNullable() + ) { + $type = '?' . $type; + } + return new self( $name, $type, diff --git a/src/Generator/TypeGenerator.php b/src/Generator/TypeGenerator.php index 0d7fc532..819d436d 100644 --- a/src/Generator/TypeGenerator.php +++ b/src/Generator/TypeGenerator.php @@ -140,6 +140,14 @@ public function __toString(): string return $this->type->toString(); } + /** + * @return bool Nullable flag + */ + public function getNullable() + { + return $this->nullable; + } + /** * @return bool[]|string[] ordered tuple, first key represents whether the type is nullable, second is the * trimmed string diff --git a/test/Generator/PromotedParameterGeneratorTest.php b/test/Generator/PromotedParameterGeneratorTest.php new file mode 100644 index 00000000..ff7bbbc1 --- /dev/null +++ b/test/Generator/PromotedParameterGeneratorTest.php @@ -0,0 +1,25 @@ +assertSame('protected ?int $nullable', $generator->generate()); + } +} diff --git a/test/TestAsset/ClassWithPromotedProperties.php b/test/TestAsset/ClassWithPromotedProperties.php new file mode 100644 index 00000000..4d932d3c --- /dev/null +++ b/test/TestAsset/ClassWithPromotedProperties.php @@ -0,0 +1,18 @@ +