Skip to content

Simplify by replacing chained $var === const with in_array() #4098

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

Open
wants to merge 2 commits into
base: 2.1.x
Choose a base branch
from

Conversation

zonuexe
Copy link
Contributor

@zonuexe zonuexe commented Jul 14, 2025

This is the same change as the previous one at #2618, but it will be automatically refactored by rules 🤖.

Currently, return $var === 'foo' || $var === 'bar'; and $isFooOrBar = $var === 'foo' || $var === 'bar'; are not covered. It also does not support formulas replaced by De Morgan's laws.

@zonuexe zonuexe changed the title Simplify by replacing chained $var === const with in_array()Refactor/or idetical chain to in array() Simplify by replacing chained $var === const with in_array() Jul 15, 2025
Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

Great idea! I'll merge it after this fix.


public function __construct(
#[AutowiredParameter]
private PhpPrinter $printer,
Copy link
Member

Choose a reason for hiding this comment

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

Do not make this a DI service. Ask for ExprPrinter instead which is already a DI service.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I replaced it with ExprPrinter.

@zonuexe zonuexe force-pushed the refactor/or-idetical-chain-to-in_array branch from 6044c89 to 1b0b7d4 Compare July 17, 2025 08:01
@zonuexe zonuexe marked this pull request as draft July 17, 2025 08:08
@zonuexe
Copy link
Contributor Author

zonuexe commented Jul 17, 2025

I see, the error that occurred after rebasing seems to be independent of the changes in this PR.

Error: Catching internal class PHPUnit\Framework\ExpectationFailedException.
Error: Catching internal class PHPUnit\Framework\ExpectationFailedException.
Error: Call to method getComparisonFailure() of internal class PHPUnit\Framework\ExpectationFailedException from outside its root namespace PHPUnit.
Error: Call to method getComparisonFailure() of internal class PHPUnit\Framework\ExpectationFailedException from outside its root namespace PHPUnit.

@zonuexe zonuexe marked this pull request as ready for review July 17, 2025 08:11
@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
Development

Successfully merging this pull request may close these issues.

3 participants