Skip to content

Promoted properties support #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/TypeReflectors/PromotedPropertyTypeReflector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Spatie\TypeScriptTransformer\TypeReflectors;

use ReflectionProperty;
use ReflectionType;

class PromotedPropertyTypeReflector extends TypeReflector
{
public static function create(ReflectionProperty $reflection): self
{
return new self($reflection);
}

public function __construct(ReflectionProperty $reflection)
{
parent::__construct($reflection);
}

protected function getDocblock(): string
{
return $this->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();
}
}
Comment on lines +8 to +41
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just use the PropertyTypeReflector here? Since a promoted property is technically a property. We could in the constructor check if the reflection property is a promoted property and then change getDocblock and docblockRegex. In v3 this problem should already be fixed since we're using PHPstan annotations over there.

3 changes: 3 additions & 0 deletions src/TypeReflectors/TypeReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
106 changes: 106 additions & 0 deletions tests/TypeReflectors/PromotedPropertyTypeReflectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

use Spatie\TypeScriptTransformer\TypeReflectors\PromotedPropertyTypeReflector;
use function PHPUnit\Framework\assertEquals;
use Spatie\TypeScriptTransformer\Attributes\LiteralTypeScriptType;

it('can reflect from reflection', function () {
$class = new class(0, 0, 0, 0, 0) {
public function __construct(
public int $p1,
public ?int $p2,
public int | float $p3,
public int | float | null $p4,
public $p5,
) {}
};

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(),
);
});

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<array-key, string> $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<array-key,string>',
(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(),
);
});
Loading