Sitelet https://github.com/phpstan/phpstan/issues/9011
Skip to content

False positive when return array_combine() is used  #9011

Description

@bancer

Bug report

"Method x() should return array but returns false." error is reported when return array_combine($a, $b); although the call to array_combine never returns false.

According to the docs https://www.php.net/manual/en/function.array-combine.php:

As of PHP 8.0.0, a ValueError is thrown if the number of elements in keys and values does not match. Prior to PHP 8.0.0, a E_WARNING was emitted instead.

Executing this in PHP generates a warning but does not return false - https://onlinephp.io/c/4f929.

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

class HelloWorld
{
	/**
	 * @return string[]
	 */
	public function getX(): array
	{
		$a = array('green', 'red', 'yellow', 'y');
		$b = array('avocado', 'apple', 'banana');
		return array_combine($a, $b);
	}
}

https://phpstan.org/r/4e381b5b-88e6-4e6a-8e68-9232819c74d1

Expected output

No error should be reported.

Activity

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