-
-
Notifications
You must be signed in to change notification settings - Fork 738
Incorrect behavior of NullToStrictStringFuncCallArgRector with loosely-typed code #7680
Copy link
Copy link
Closed
Labels
Description
Bug Report
| Subject | Details |
|---|---|
| Rector version | last dev-main |
| Installed as | composer dependency |
Minimal PHP Code Causing Issue
<?php
final class DemoFile
{
protected function listThings() {
$things=[];
$things[] = 'f';
return $things;
}
public function run()
{
$replaces = $this->listThings();
$preg = preg_replace('/^/', 'prefix.', $replaces);
$str = str_replace('foo', 'bar', $replaces);
return [...$preg, ...$str];
}
}Responsible rules
NullToStrictStringFuncCallArgRector
Expected Behavior
Rector should not make any changes to the code. Array arguments to preg_replace / str_replace etc are valid and attempting to cast them to a string causes a runtime Array to string conversion error.
I'm very new to Rector, but from tracing through, it looks like this is happening because $replaces is detected as a PhpParser\Node\Expr\Variable and the node type resolver identifies that it is of PHPStan\Type\MixedType.
Therefore NullToStrictStringFuncCallArgRector assumes it is nullable and adds the (string) typecast. However, in this case it isn't actually nullable, it looks like phpstan / rector is using MixedType to indicate unknown / undefined?
Reactions are currently unavailable