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

DocblockTypeContradiction when comparing @property, using prefix type, through magic getter with actual class constant #3272

Closed
joakimfors opened this issue Apr 30, 2020 · 3 comments · Fixed by #6133
Labels

Comments

@joakimfors
Copy link

https://psalm.dev/r/86b57553b8

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/86b57553b8
<?php

/**
 * @property FooBar::TYPE_* $type
 */
class FooBar {
	public const TYPE_FOO = 'foo';
	public const TYPE_BAR = 'bar';

	/** @var FooBar::TYPE_* */
	private $_type;

	public function __construct()
	{
		$this->_type = FooBar::TYPE_FOO;
	}

	/**
	 * @param string $name
	 * @return mixed
	 */
	public function __get(string $name)
	{
		$getter = 'get' . $name;
		return $this->$getter();
	}

	/**
	 * @return FooBar::TYPE_*
	 */
	public function getType()
	{
		return $this->_type;
	}
}

$foobar = new FooBar();
$test = $foobar->type === FooBar::TYPE_BAR;
print($test);
$test = $foobar->getType() === FooBar::TYPE_BAR;
print($test);
Psalm output (using commit 21f4dee):

ERROR: DocblockTypeContradiction - 38:9 - string(bar) does not contain scalar-class-constant(FooBar::TYPE_*)

@weirdan weirdan added the bug label Apr 30, 2020
@joakimfors
Copy link
Author

Same thing with assignment through magic setter, only InvalidPropertyAssignmentValue instead:

https://psalm.dev/r/b7f029181f

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/b7f029181f
<?php

/**
 * @property FooBar::TYPE_* $type
 */
class FooBar {
	public const TYPE_FOO = 'foo';
	public const TYPE_BAR = 'bar';

	/** @var FooBar::TYPE_* */
	private $_type;

	public function __construct()
	{
		$this->_type = FooBar::TYPE_FOO;
	}

	/**
	 * @param string $name
	 * @return mixed
	 */
	public function __get(string $name)
	{
		$getter = 'get' . $name;
		return $this->$getter();
	}

	/**
	 * @param string $name
	 * @param mixed $value
	 * @return void
	 */
	public function __set(string $name, mixed $value)
	{
		$setter = 'set' . $name;
		$this->$setter($value);
	}

	/**
	 * @return FooBar::TYPE_*
	 */
	public function getType()
	{
		return $this->_type;
	}

	/**
	 * @param FooBar::TYPE_* $value
	 * @return void
	 */
	public function setType(string $value)
	{
		$this->_type = $value;
	}
}

$foobar = new FooBar();

$test = $foobar->type === FooBar::TYPE_BAR;
print($test);
$test = $foobar->getType() === FooBar::TYPE_BAR; // OK
print($test);

$foobar->type = FooBar::TYPE_BAR;
$foobar->setType(FooBar::TYPE_BAR); // OK
Psalm output (using commit 21f4dee):

ERROR: DocblockTypeContradiction - 59:9 - string(bar) does not contain scalar-class-constant(FooBar::TYPE_*)

ERROR: InvalidPropertyAssignmentValue - 64:17 - $foobar->type with declared type 'scalar-class-constant(FooBar::TYPE_*)' cannot be assigned type 'string(bar)'

weirdan added a commit to weirdan/psalm that referenced this issue Jul 18, 2021
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.

2 participants