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

[DeadCode] Add early return check to RemoveUnusedNonEmptyArrayBeforeForeachRector #3611

Merged
merged 21 commits into from
Apr 14, 2023

Conversation

TomasVotruba
Copy link
Member

No description provided.

@TomasVotruba TomasVotruba force-pushed the tv-extend-empty-return branch 2 times, most recently from a83b74a to 07f5070 Compare April 13, 2023 10:58
@samsonasik
Copy link
Member

All checks have passed 🎉 @TomasVotruba I think it is ready.

@samsonasik
Copy link
Member

Rebased, I will add more fixture for skip multi ifs and fixtures.

@samsonasik
Copy link
Member

I added fixture for skip multi ifs and foreach empty check 833fd62

@samsonasik
Copy link
Member

Fixed 🎉 by return early when there is next stmt after foreach.

@samsonasik
Copy link
Member

All checks have passed 🎉 @TomasVotruba I think it is ready.

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.

@TomasVotruba I am thinking that the following code should be skipped as well:

if (empty($items)) {
    return;
}

foreach ($items as $item) {
    return 1;
}

above, when empty, it doesn't return anything, when not empty, it return 1, which make it sometime return sometime not.

@samsonasik
Copy link
Member

I added failing fixture for skip has return with value inside foreach af6f370

@samsonasik
Copy link
Member

Oh, I see, when empty, it pass to after foreach, which no other stmt, so that seems ok 👍

@samsonasik
Copy link
Member

I updated the fixture 0db3625

@samsonasik
Copy link
Member

All checks have passed 🎉 @TomasVotruba I think it is ready.


private function refactorStmtsAware(StmtsAwareInterface $stmtsAware): ?StmtsAwareInterface
{
foreach ((array) $stmtsAware->stmts as $key => $stmt) {
Copy link
Member

@samsonasik samsonasik Apr 14, 2023

Choose a reason for hiding this comment

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

It seems it can be optimized by only get $stmtsAware->stms[$lastKey - 1] and $stmtsAware->stms[$lastKey], that make loop is not needed

Copy link
Member

Choose a reason for hiding this comment

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

optimized, also allow multi if -> foreach which actually ok, with only remove last if bd8b9f9

Copy link
Member

Choose a reason for hiding this comment

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

New phpunit https://github.com/sebastianbergmann/phpunit/releases/tag/10.1.0 seems cause error on paratest, the handling is still on progress paratestphp/paratest#752

I will set phpunit to 10.0.19 temporary.

Copy link
Member

Choose a reason for hiding this comment

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

I temporary set phpunit to 10.0.19 e23e2e3 while wait for paratest handling for it.

Copy link
Member

Choose a reason for hiding this comment

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

Comment on lines +37 to +47
if (empty($items)) {
return;
}

foreach ($items as $item) {
echo $item;
}

foreach ($items2 as $item2) {
echo $item2;
}
Copy link
Member

@samsonasik samsonasik Apr 14, 2023

Choose a reason for hiding this comment

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

this only last if before last foreach that removed 👍

@samsonasik
Copy link
Member

All checks have passed 🎉 @TomasVotruba I think it is ready.

@TomasVotruba TomasVotruba merged commit 195bb62 into main Apr 14, 2023
@TomasVotruba TomasVotruba deleted the tv-extend-empty-return branch April 14, 2023 08:07
@TomasVotruba
Copy link
Member Author

Thank you, this looks very good 😊

samsonasik added a commit that referenced this pull request May 8, 2023
…oreachRector (#3611)

* [DeadCode] Add early return check to RemoveUnusedNonEmptyArrayBeforeForeachRector

* skip edge case

* Add fixture multi if -> foreach

* Add the patch by loop jump to key recursive

* [ci-review] Rector Rectify

* clean up skip config

* Revert clean up skip config

This reverts commit 22c3df8.

* Revert [ci-review] Rector Rectify

This reverts commit 10a8bfa.

* Revert Add the patch by loop jump to key recursive

This reverts commit 9375b38.

* Revert Add fixture multi if -> foreach

This reverts commit ceb6b33.

* add failing fixture throw inside empty

* Add patch ensure if only return with no expr

* add fixture for multi ifs and fixtures

* Fixed 🎉

* rename fixture

* skip has return with value inside foreach

* rename fixture

* optimize, allow multi if check, but last only

* temporary pin phpunit to 10.0.19 due to make error on paratest

* Fix phpstan

* use paratest 7.1.3

---------

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
Co-authored-by: GitHub Action <actions@github.com>
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.

3 participants