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

Narrow type on setting offsets of properties #3699

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from

Conversation

herndlm
Copy link
Contributor

@herndlm herndlm commented Dec 1, 2024

Removes OriginalPropertyTypeExpr which now leads to the expression in the scope being used for property fetches when determining the result of an array offset set.

I believe this makes things simpler and more consistent and expected. But it also showed that some small restructuring of tests for TypesAssignedToPropertiesRule was needed to avoid dependencies and weirdness with types that were changed. I was hoping that integration tests will show us how "annoying" this is.

Fixes phpstan/phpstan#6398
Fixes phpstan/phpstan#6571
Fixes phpstan/phpstan#7880

@herndlm herndlm force-pushed the type-narrowing-property-fetch-set-offset-value branch from fee1250 to 9564df9 Compare December 1, 2024 21:33
{
$this->integers[] = 4;
$this->integers['foo'] = 5;
$this->integers[] = 'foo';
Copy link
Contributor

@staabm staabm Dec 2, 2024

Choose a reason for hiding this comment

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

if you don't remove lines in test-files but leave them empty, you don't need to adjust all the line numbers in the expectations (and get a smaller, easier to review PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry, didn't get that fully. can you explain what you mean? because it highly annoys me that I had to do things there yeah.. I would have not touched these even, but the problem here is that invalid assignments would widen the array type and influence other errors, so I had to separate them a bit more.

Copy link
Contributor

@staabm staabm Dec 2, 2024

Choose a reason for hiding this comment

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

if you add 3 empty lines, line 18,19,20 (so the ones you deleted) all following line-numbers in the file will not be reduced by 3

(so you don't delete lines in existing tests, but replace them with a empty lines)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah simple like that, ok, good idea I guess. let me clean this up as much as I can then

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm I don't think this helps much here tbh. I can make like 3 of them go away maybe, but the rest of the line numbers increased, so I can't do anything about them.

Copy link
Contributor

@staabm staabm Dec 2, 2024

Choose a reason for hiding this comment

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

one idea, if you want to invest a few more time:

don't change the test (or only add comments after existing lines) instead of moving lines arround

Copy link
Contributor Author

@herndlm herndlm Dec 2, 2024

Choose a reason for hiding this comment

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

yeah, it's a reasonable approach I guess. I'll revert changes, adapt the test classes to show changed behaviour and add new tests where the existing ones did not pick up issues any more as before. that should also help show the behaviour changes better 👍

UPDATE: think I'll wait for some feedback from the boss before spending more time on formatting these files :) e.g. I hate things like $this->intArray[new \DateTimeImmutable()] = 1; would set the property type in that scope to non-empty-array<*ERROR*, mixed> which allows all kind of keys. I believe the tests need to be restructured, otherwise they look very weird. my main argument for this behaviour change (except that it fixes the linked issues) is that it works like that for other properties too, see https://phpstan.org/r/e8909d22-857a-4dea-8d88-4cb13704b63e

@herndlm herndlm changed the base branch from 1.12.x to 2.1.x December 24, 2024 18:13
@herndlm herndlm force-pushed the type-narrowing-property-fetch-set-offset-value branch from 9564df9 to 8bf4588 Compare December 24, 2024 18:14
@herndlm herndlm marked this pull request as ready for review December 24, 2024 18:19
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants