Skip to content
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

Add ReflectionProperty::getType() and hasType() #5584

Merged
merged 1 commit into from
Jul 10, 2021
Merged
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
1 change: 1 addition & 0 deletions dictionaries/CallMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11564,6 +11564,7 @@
'ReflectionProperty::getName' => ['string'],
'ReflectionProperty::getType' => ['?ReflectionType'],
'ReflectionProperty::getValue' => ['mixed', 'object='=>'object'],
'ReflectionProperty::hasType' => ['bool'],
'ReflectionProperty::isDefault' => ['bool'],
'ReflectionProperty::isPrivate' => ['bool'],
'ReflectionProperty::isProtected' => ['bool'],
Expand Down
12 changes: 12 additions & 0 deletions stubs/Reflection.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ class ReflectionProperty implements Reflector
* @return array<ReflectionAttribute<TClass>>
*/
public function getAttributes(?string $name = null, int $flags = 0): array {}

/**
* @since 7.4
* @psalm-assert-if-true ReflectionType $this->getType()
*/
public function hasType() : bool {}

/**
* @since 7.4
* @psalm-mutation-free
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@caugner I think it works with the @psalm-mutation-free you said 👍 (apart from adding the 7.4 check)

Copy link
Contributor

Choose a reason for hiding this comment

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

Just to check: Did you need to add mutation-free to make the test pass? So marking the test with 7.4 was not enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, glad to hear it works! This reminds me that I should use that annotation in my own projects as well. 🙂

*/
public function getType() : ?ReflectionType {}
franmomu marked this conversation as resolved.
Show resolved Hide resolved
}

class ReflectionMethod implements Reflector
Expand Down
13 changes: 13 additions & 0 deletions tests/AssertAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,19 @@ function foo(array $foos) : array {
*/
function allIsInstanceOf($value, $class): void {}'
],
'implicitReflectionPropertyAssertion' => [
'<?php
$class = new ReflectionClass(stdClass::class);
$properties = $class->getProperties();
foreach ($properties as $property) {
if ($property->hasType()) {
$property->getType()->allowsNull();
}
}',
[],
[],
'7.4'
],
];
}

Expand Down