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

Incorrect behavior of SimplifyForeachToArrayFilterRector #7895

Closed
raoz opened this issue Apr 21, 2023 · 1 comment · Fixed by rectorphp/rector-src#3738
Closed

Incorrect behavior of SimplifyForeachToArrayFilterRector #7895

raoz opened this issue Apr 21, 2023 · 1 comment · Fixed by rectorphp/rector-src#3738
Assignees
Labels

Comments

@raoz
Copy link

raoz commented Apr 21, 2023

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/0c85f61d-5dab-46fb-ae9e-be5c32d5a3f3

<?php
class Test {
    public function __construct(
    public int $a,
    public ?int $b) {       
    }
}

final class DemoFile
{
    public function run()
    {
        $a = new Test(3, null);
        
        $arr = [];
        foreach($a as $k => $v) {
			if($v !== null) {
				$arr[$k] = $v;
            }
        }
        echo json_encode($arr);
    }
}

Responsible rules

  • SimplifyForeachToArrayFilterRector

Expected Behavior

Either leave unchanged or possibly something like

$arr = array_filter(iterator_to_array(new \ArrayIterator($a)), fn($v) => $v !== null);

The problem is that array_filter only supports arrays, so the changed code does not run.

@raoz raoz added the bug label Apr 21, 2023
@samsonasik
Copy link
Member

I think it can just be skipped, could you try provide a patch? Verify that foreach iterated data is only ArrayType

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

Successfully merging a pull request may close this issue.

2 participants