Skip to content

Conversation

@jtojnar
Copy link
Contributor

@jtojnar jtojnar commented Mar 21, 2023

In preparation for phpstan/phpstan-src#2294

When the property is not writeable, the writeable type will be void.

When the property is not writeable, the writeable type will be void.
$property = $class->getProperty($fieldName, $scope);
$propertyType = $property->getWritableType();

if (!$property->isWritable()) {
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem right - if case the property is not writable, we need to resolve the writeable error first, instead it will result in the wrong error -> readable type doesn't match the type being assigned/written.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure what you mean.

  • Without Asymmetric @property types phpstan/phpstan-src#2294, $property->getWritableType() === $property->getReadableType() so this would be a no-op.
  • And with it, this is necessary since $property->getWritableType() will be a bottom type void when the property is not writeable. And it will always be readable or writeable when it exists.

What does not seem right to me is writing to read-only properties but you have created setReadOnlyValue() so 🤷‍♀️

Copy link
Member

Choose a reason for hiding this comment

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

Yes, and since readable type will be different, this PHPStan will improperly throw an error that readable type don't accept the value.

Copy link
Contributor Author

@jtojnar jtojnar Mar 22, 2023

Choose a reason for hiding this comment

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

I do not think it will. PHPStan is not aware that setReadOnlyValue() or setValue() modifies the property. Only this rule ensures it.

And if you try

/**  * @property-read string $name  */ final class Tag extends Entity { public function setName(string $name): void	{ $this->name = $name;	} }

You will rightly get Property Tag::$name is not writable before the type mismatch.

@jtojnar jtojnar mentioned this pull request Apr 3, 2023
@hrach
Copy link
Member

hrach commented Apr 4, 2023

fixed here: f95afa3
thx for reporting

@hrach hrach closed this Apr 4, 2023
@jtojnar jtojnar deleted the patch-1 branch April 4, 2023 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants