diff --git a/src/TypeReflectors/PromotedPropertyTypeReflector.php b/src/TypeReflectors/PromotedPropertyTypeReflector.php new file mode 100644 index 00000000..4e961deb --- /dev/null +++ b/src/TypeReflectors/PromotedPropertyTypeReflector.php @@ -0,0 +1,41 @@ +reflection->getDeclaringClass()->getMethod('__construct')->getDocComment(); + } + + protected function docblockRegex(): string + { + $parameterName = $this->reflection->getName(); + return '/@param\s+([^\s<]+(?:<[^>]*>)?)\s+\$' . preg_quote($parameterName, '/') . '\b/m'; + + } + + protected function getReflectionType(): ?ReflectionType + { + return $this->reflection->getType(); + } + + protected function getAttributes(): array + { + return $this->reflection->getAttributes(); + } +} diff --git a/src/TypeReflectors/TypeReflector.php b/src/TypeReflectors/TypeReflector.php index 7f16e80a..d887a278 100644 --- a/src/TypeReflectors/TypeReflector.php +++ b/src/TypeReflectors/TypeReflector.php @@ -36,6 +36,9 @@ public function __construct(protected ReflectionMethod|ReflectionProperty|Reflec public static function new(ReflectionMethod|ReflectionProperty|ReflectionParameter $reflection): static { if ($reflection instanceof ReflectionProperty) { + if ($reflection->isPromoted()) { + return new PromotedPropertyTypeReflector($reflection); + } return new PropertyTypeReflector($reflection); } diff --git a/tests/TypeReflectors/PromotedPropertyTypeReflectorTest.php b/tests/TypeReflectors/PromotedPropertyTypeReflectorTest.php new file mode 100644 index 00000000..62d9e4c8 --- /dev/null +++ b/tests/TypeReflectors/PromotedPropertyTypeReflectorTest.php @@ -0,0 +1,106 @@ +reflect(), + ); + + assertEquals( + '?int', + (string) PromotedPropertyTypeReflector::create(new ReflectionProperty($class, 'p2'))->reflect(), + ); + + assertEquals( + 'int|float', + (string) PromotedPropertyTypeReflector::create(new ReflectionProperty($class, 'p3'))->reflect(), + ); + + assertEquals( + 'int|float|null', + (string) PromotedPropertyTypeReflector::create(new ReflectionProperty($class, 'p4'))->reflect(), + ); + + assertEquals( + 'any', + (string) PromotedPropertyTypeReflector::create(new ReflectionProperty($class, 'p5'))->reflect(), + ); +}); + +it('can reflect from docblock', function () { + $class = new class(0, 0, 0, 0, 0, []) { + /** + * @param int $p1 + * @param ?int $p2 + * @param int|float $p3 + * @param int|float|null $p4 + * @param array $p6 + */ + public function __construct( + public int $p1, + public ?int $p2, + public int | float $p3, + public int | float | null $p4, + public $p5, + public array $p6, + ) {} + }; + + assertEquals( + 'int', + (string) PromotedPropertyTypeReflector::create(new ReflectionProperty($class, 'p1'))->reflect(), + ); + + assertEquals( + '?int', + (string) PromotedPropertyTypeReflector::create(new ReflectionProperty($class, 'p2'))->reflect(), + ); + + assertEquals( + 'int|float', + (string) PromotedPropertyTypeReflector::create(new ReflectionProperty($class, 'p3'))->reflect(), + ); + + assertEquals( + 'int|float|null', + (string) PromotedPropertyTypeReflector::create(new ReflectionProperty($class, 'p4'))->reflect(), + ); + + assertEquals( + 'any', + (string) PromotedPropertyTypeReflector::create(new ReflectionProperty($class, 'p5'))->reflect(), + ); + + assertEquals( + 'array', + (string) PromotedPropertyTypeReflector::create(new ReflectionProperty($class, 'p6'))->reflect(), + ); +}); + +it('can reflect from attribute', function () { + $class = new class(0){ + public function __construct( + #[LiteralTypeScriptType('Integer')] + public $p1, + ) {} + }; + + assertEquals( + 'Integer', + (string) PromotedPropertyTypeReflector::create(new ReflectionProperty($class, 'p1'))->reflect(), + ); +});