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

Properties passed as arg 1 to array_pop are mutated, but checks for pure methods do not see the property as mutated #9429

Open
DaveLiddament opened this issue Feb 28, 2023 · 2 comments · Fixed by #9434

Comments

@DaveLiddament
Copy link
Contributor

See example: https://psalm.dev/r/b0916c1ed2

I'd expect line 16 to be reported as an error. Something along the lines of:

ImpurePropertyAssignment - 16:9 - Cannot assign to a property from a mutation-free context

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/b0916c1ed2
<?php declare(strict_types = 1);

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

	public function push(string $item): void
	{
		$this->stack[] = $item;
	}

    /** @psalm-mutation-free */
    public function next(): string|null
	{
		return array_pop($this->stack);
	}
}

function takesString(string $string): void {var_dump($string);}


$stack = new Stack();
$stack->push("Jane");
if ($stack->next() !== null) {
  takesString($stack->next());	
}
Psalm output (using commit e212b06):

ERROR: ForbiddenCode - 20:45 - Unsafe var_dump

weirdan added a commit to weirdan/psalm that referenced this issue Feb 28, 2023
@weirdan
Copy link
Collaborator

weirdan commented Feb 28, 2023

Naive approach doesn't work

@weirdan weirdan reopened this Feb 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants