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

Magic property is inferred as mixed after method call #8748

Closed
danog opened this issue Nov 24, 2022 · 1 comment
Closed

Magic property is inferred as mixed after method call #8748

danog opened this issue Nov 24, 2022 · 1 comment
Labels

Comments

@danog
Copy link
Collaborator

danog commented Nov 24, 2022

https://psalm.dev/r/10eec1df24

This really surprised me, because it looks like Psalm is automatically creating some mistaken assertion on the property assigned in the updateErrors method, because if the assignment is removed from updateErrors the error is fixed: https://psalm.dev/r/4e00eed4c5

@psalm-github-bot
Copy link

psalm-github-bot bot commented Nov 24, 2022

I found these snippets:

https://psalm.dev/r/10eec1df24
<?php

/**
 * @property array<string, string> $errors
 *
 * @psalm-seal-properties
 */
final class OrganizationObject {

	public function __get(string $key): mixed
	{
		return [];
	}

	public function __set(string $key, mixed $a): void
	{
	}

	public function updateErrors(): void {
		/** @var array<string, string> */
		$errors = [];
		$this->errors = $errors;
	}
	public function updateStatus(): void {
		$errors = $this->errors;
		/** @psalm-trace $errors */;
		$this->updateErrors();
		$errors = $this->errors;
		/** @psalm-trace $errors */;
	}
}
Psalm output (using commit 8f39de9):

INFO: Trace - 26:30 - $errors: array<string, string>

INFO: MixedAssignment - 28:3 - Unable to determine the type that $errors is being assigned to

INFO: Trace - 29:30 - $errors: mixed

INFO: UnusedVariable - 25:3 - $errors is never referenced or the value is not used

INFO: UnusedVariable - 28:3 - $errors is never referenced or the value is not used
https://psalm.dev/r/4e00eed4c5
<?php

/**
 * @property array<string, string> $errors
 *
 * @psalm-seal-properties
 */
final class OrganizationObject {

	public function __get(string $key): mixed
	{
		return [];
	}

	public function __set(string $key, mixed $a): void
	{
	}

	public function updateErrors(): void {
		/** @var array<string, string> */
		$errors = [];
		//$this->errors = $errors;
	}
	public function updateStatus(): void {
		$errors = $this->errors;
		/** @psalm-trace $errors */;
		$this->updateErrors();
		$errors = $this->errors;
		/** @psalm-trace $errors */;
	}
}
Psalm output (using commit 8f39de9):

INFO: UnusedVariable - 21:3 - $errors is never referenced or the value is not used

INFO: Trace - 26:30 - $errors: array<string, string>

INFO: Trace - 29:30 - $errors: array<string, string>

INFO: UnusedVariable - 25:3 - $errors is never referenced or the value is not used

INFO: UnusedVariable - 28:3 - $errors is never referenced or the value is not used

@danog danog added the bug label Nov 24, 2022
@orklah orklah closed this as completed in 85071b6 Nov 24, 2022
orklah added a commit that referenced this issue Nov 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant