Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UndefinedVariable "$__fake_offset_var__" #7164

Closed
PurpleVsGreen opened this issue Dec 15, 2021 · 4 comments · Fixed by #7167
Closed

UndefinedVariable "$__fake_offset_var__" #7164

PurpleVsGreen opened this issue Dec 15, 2021 · 4 comments · Fixed by #7167
Labels

Comments

@PurpleVsGreen
Copy link

Thank you for making Psalm such an amazing analyzer.
I might have found a bug but I'm not sure.

https://psalm.dev/r/cccdb532d0
https://psalm.dev/r/c114e494ca

The error goes away when I use an anonymous function as the callable for the inner array_map.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/cccdb532d0
<?php
function bar(array $a): array {
	return $a;
}

/**
 * @param array[] $x
 *
 * @return array[]
 */
function foo(array $x): array {
	return array_map(
		"array_merge",
		array_map(
			"bar",
			$x
		),
		$x
	);
}
Psalm output (using commit 646ba98):

ERROR: UndefinedVariable - 18:3 - Cannot find referenced variable $__fake_offset_var__
https://psalm.dev/r/c114e494ca
<?php
class Test {

	/** @param array[] $x */
	public function foo(array $x): array {
		return array_map(
			"array_merge",
			array_map(
				[$this, "bar"],
				$x
			),
			$x
		);
	}

	public function bar(array $a): array {
		return $a;
	}

}
Psalm output (using commit 646ba98):

ERROR: UndefinedVariable - 12:4 - Cannot find referenced variable $__fake_offset_var__

@weirdan weirdan added the bug label Dec 15, 2021
@AndrolGenhald
Copy link
Collaborator

Best I can tell, this is due to the nested array_map. $__fake_offset_var__ is removed from the context at ArrayMapReturnTypeProvider.php:470, and at some point it tries to use $__fake_offset_var__ when only $x and $x['$__fake_offset_var__] are set (I don't really understand why $x['$__fake_offset_var__] is being set in the first place though).

Using a temporary variable is another workaround: https://psalm.dev/r/b582aba92e

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/b582aba92e
<?php
function bar(array $a): array {
	return $a;
}

/**
 * @param array[] $x
 *
 * @return array[]
 */
function foo(array $x): array {
    $tmp = array_map(
        "bar",
        $x
    );
	return array_map(
		"array_merge",
		$tmp,
		$x
	);
}
Psalm output (using commit 38a3efe):

No issues!

@AndrolGenhald
Copy link
Collaborator

Ok, I'm convinced the $x['$__fake_offset_var__] isn't intended to be set and is polluting the context, it should probably be removed somehow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants