Skip to content

Passing reference return value of function into pass-by-reference parameter is broken #13711

@heiglandreas

Description

@heiglandreas

Bug report

When I have a function that returns a reference and I pass the return-value of that function to a parameter that expects a reference I get a false-positive error

Parameter #1 $array of function reset is passed by reference, so it expects variables only.

The smallest example I came up with is (working example at https://3v4l.org/nmiog)

class Foo 
{
	/** @var array<string> */
	private array $array = [];

	/** @return array<string> */
	public function &getList(): array
	{
		return $this->array; 
	}

	public function rewind(): void
	{
		reset($this->getList());
	}
}

getList returns a pointer to the array $array. But passing that pointer to reset works in PHP (see 3v4l.org example) but is considered broken by PHPStan.

Is there some additional magical annotation I need to pass to make this work?

Code snippet that reproduces the problem

https://phpstan.org/r/3e15e1f7-fcfd-478a-a263-7ba5dd00e428

Expected output

My expected output would be a green "OK"

Did PHPStan help you today? Did it make you happy in any way?

Every single day - Unless I have to fight it 😁

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions