RemoveAlwaysTrueIfConditionRector with array_push is buggy
Bug Report
| Subject | Details |
|---|---|
| Rector version | 1.2.5 |
RemoveAlwaysTrueIfConditionRector removes a correct if statement. Code is quite ugly. It seems the problem is with:
- array_push .... with "$outerList[$id][] = [];" insetad of "array_push($outerList[$id], []);" it works
- an array with at least 2 foreach iterations are needed => with "$idList = [1];" it works
private function demo(): void
{
$outerList = [];
$idList = [1, 2];
foreach ($idList as $id) {
$outerList[$id] = [];
array_push($outerList[$id], []);
}
$resultSet = [];
foreach ($outerList as $key => $outerElement) {
$result = false;
foreach ($outerElement as $innerElement) {
$result = true;
}
if (!$result) {
array_push($resultSet, $key);
}
}
}
rector remove the if statement:
- if (!$result) {
- array_push($resultSet, $key);
- }
+ array_push($resultSet, $key);
Expected Behaviour
Rule should not be executed/change code
We use phpstan to detect type, on phpstan, it is detected as falsy, I think it should be reported there on phpstan side, see https://phpstan.org/r/73565769-6b97-48b7-8b52-e3b8883a5961
I am closing it as it is phpstan bug, and the workaround solution already provided there https://github.com/phpstan/phpstan/issues/11846#issuecomment-2413030997 ;)
Thank you for understanding.
This issue has been automatically locked because it has been closed for 150 days. Please open a new issue if you have a similar problem.