Skip to content

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

Closed

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 😁

Activity

  1. added this to the Easy fixes milestone on Feb 11, 2026
  2. added a commit that references this issue on Feb 12, 2026
  3. added a commit that references this issue on Feb 12, 2026
  4. heiglandreas commented on Feb 12, 2026

    @heiglandreas
    Author

    Did I recently mention that you rock, @ondrejmirtes?

  5. github-actions commented on Mar 16, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  6. locked as resolved and limited conversation to collaborators on Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions