Skip to content

Asymmetric @property types #2294

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

Closed
wants to merge 11 commits into from
Closed

Conversation

jtojnar
Copy link
Contributor

@jtojnar jtojnar commented Mar 20, 2023

@jtojnar jtojnar marked this pull request as draft March 20, 2023 09:47
@jtojnar jtojnar force-pushed the asymmetric-properties branch 6 times, most recently from cc9fe7e to e8425b3 Compare March 20, 2023 16:50
$propertyWriteTags[$propertyName]->getType(),
$classReflection->getActiveTemplateTypeMap(),
);

return new AnnotationPropertyReflection(
$declaringClass,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The limitation that any @property{,-read,write} will shadow all magic properties from parent. But that is probably not needed for most use cases.

@jtojnar jtojnar force-pushed the asymmetric-properties branch 2 times, most recently from 131e032 to 69ec1dd Compare March 20, 2023 17:10
@jtojnar jtojnar marked this pull request as ready for review March 20, 2023 17:19
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@jtojnar jtojnar force-pushed the asymmetric-properties branch from 5c68b74 to 3f40360 Compare March 21, 2023 21:18
}

public function getWritableType(): Type
{
return $this->type;
return $this->writableType ?? new VoidType();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jtojnar

This comment was marked as resolved.

@hrach
Copy link
Contributor

hrach commented Mar 22, 2023

Isn't this fixing phpstan/phpstan#4845 as well? (Probably not :/)

@jtojnar
Copy link
Contributor Author

jtojnar commented Mar 22, 2023

@hrach With this PR, it would be expressible as:

/**
 * @property DateTimeImmutable|null $createdAt
 * @property-write DateTimeImmutable|'now'|null $createdAt
 */
class Entity {}

$e = new Entity();
$e->createdAt = 'now';
$e->createdAt->format();

But you would still need getTypeAfterAssignment() to know 'now' will be converted to non-null value.

@jtojnar jtojnar force-pushed the asymmetric-properties branch from bfd7f40 to 075c3cc Compare March 22, 2023 16:42
@jtojnar jtojnar force-pushed the asymmetric-properties branch from 075c3cc to 4cd7b39 Compare April 2, 2023 18:11
jtojnar added 2 commits April 4, 2023 22:43
Allow having `@property-read` and `@property-write` with the same name but different types.
jtojnar added 9 commits April 4, 2023 22:43
Ideally, `get{Readable,Writable}Type()` would be nullable but that would be a BC break.
We cannot make `PropertyReflection::get{Readable,Writable}Type()` nullable without breaking API compatibility.
Instead, we will use `VoidType` since no values inhabit it, which is a nice fit for a property that does not exist – properties always need to have a value.
…ly properties

Those will be caught by WritingToReadOnlyPropertiesRule and we would just get:

    Property Foo::$bar (void) does not accept string.
    Property Foo::$bar is not writable.
@ondrejmirtes
Copy link
Member

Hello, thank you for your work! I took the liberty to take your tests and implement it in an easier way: #2327

We just needed to add different types to PropertyTag, there was no need to deprecate getPropertyTags() and change ResolvedPhpDocBlock etc.

@jtojnar jtojnar deleted the asymmetric-properties branch April 5, 2023 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Asymmetric @property types
4 participants