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

RemoveUnusedNonEmptyArrayBeforeForeachRector: ignore phpdoc types #5169

Merged
merged 4 commits into from
Oct 14, 2023

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Oct 14, 2023

No description provided.

{
$this->items = $items;
}
private array $items = [];

public function run()
{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMO this test was wrong because we can't know the type of $this->items (it is mixed in this case)
-> I simplified the test-case and made sure it works on a native array

Copy link
Member

@samsonasik samsonasik Oct 14, 2023

Choose a reason for hiding this comment

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

no, it is clearly set :

  • default array on property level
  • assigned with always array

the type will always be array.

Changing to typed property will reduce the functionality of upgrading to better code.

We don't need to ensure the outside object fill usage as that out of scope.

Copy link
Contributor Author

@staabm staabm Oct 14, 2023

Choose a reason for hiding this comment

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

We can't know whether the class has some other method which overrides the property with any non-array type - at least the rector rule does not do the necessary checks to verify it , or does it?

see e.g. https://getrector.com/demo/a7d43fbd-c848-47e4-9ba2-bd9e6438746c


assignment of a default value via construct does not verify the type will never change (at least not for a non-typed property)

Copy link
Member

@samsonasik samsonasik Oct 14, 2023

Choose a reason for hiding this comment

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

That seems code smell, and the app already crash from the beginning if not filled with proper data for that truthy check then foreach it, like other PR, on runtime perspective.

The issue with more and more skipping things like that will reduce the usability of upgrading to better code itself.

Copy link
Member

@samsonasik samsonasik left a comment

Choose a reason for hiding this comment

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

Ok, combine with TypedPropertyFromAssignsRector seems can be solution to keep up the empty removal, as after it typed, the empty will be removable if always array filled.

Just the non-array type empty check then foreach is a bit strange use case, which I can't found in real life yet which fatal error is "the correct one" since checked on runtime.

@TomasVotruba I am going to merge this because of the rule named RemoveUnusedNonEmptyArrayBeforeForeachRector for arrays.

@samsonasik samsonasik merged commit c639ef8 into rectorphp:main Oct 14, 2023
39 checks passed
@samsonasik
Copy link
Member

Thank you @staabm

@staabm staabm deleted the no-phpdoc branch October 14, 2023 13:09
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.

2 participants