Skip to content

Incorrect behavior of NullToStrictStringFuncCallArgRector with loosely-typed code #7680

@acoulton

Description

@acoulton

Bug Report

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

Minimal PHP Code Causing Issue

See https://getrector.org/demo/c70acf73-1342-46d9-bd91-3b165fed0939

<?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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions