-
-
Notifications
You must be signed in to change notification settings - Fork 43
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
martingold
wants to merge
1
commit into
spatie:main
Choose a base branch
from
martingold:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
tests/TypeReflectors/PromotedPropertyTypeReflectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 changegetDocblock
anddocblockRegex
. In v3 this problem should already be fixed since we're using PHPstan annotations over there.