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

MismatchingDocblockReturnType array template string #10511

Open
Nikolay-Ivanchenko opened this issue Dec 21, 2023 · 1 comment
Open

MismatchingDocblockReturnType array template string #10511

Nikolay-Ivanchenko opened this issue Dec 21, 2023 · 1 comment

Comments

@Nikolay-Ivanchenko
Copy link

Nikolay-Ivanchenko commented Dec 21, 2023

<?php

/**
 * @template Values of non-empty-array<lowercase-string, string>
 * @template DefaultKey of key-of<Values>
 */
final class SortableListType
{
	/**
	 * @param Values $values
	 * @param DefaultKey $defaultKey
	 */
	public function __construct(
		private readonly array $values,
		private readonly string $defaultKey
	) {
	}

	/** @return DefaultKey */
	public function getDefaultKey(): string
	{
		return $this->defaultKey;
	}

	/** @return Values[DefaultKey] */
	public function getDefaultValue(): string
	{
		return $this->values[$this->defaultKey];
	}
}

//type tests
$a = new SortableListType(["a"=> "1", "b" => "2"], "not-exist-key");
$b = new SortableListType(["a"=> "1", "b" => "2"], "a");
$c = new SortableListType(["a"=> "1", "b" => "2"], "b");

//return type tests
$bKey = $b->getDefaultKey();
$bValue = $b->getDefaultValue();

$cKey = $c->getDefaultKey();
$cValue = $c->getDefaultValue();

https://psalm.dev/r/9277d2b643
https://psalm.dev/r/9578e396b2

Copy link

I found these snippets:

https://psalm.dev/r/9277d2b643
<?php

/**
 * @template Values of non-empty-array<lowercase-string, string>
 * @template DefaultKey of key-of<Values>
 */
final class SortableListType
{
	/**
	 * @param Values $values
	 * @param DefaultKey $defaultKey
	 */
	public function __construct(
		private readonly array $values,
		private readonly string $defaultKey
	) {
	}

	/** @return DefaultKey */
	public function getDefaultKey(): string
	{
		return $this->defaultKey;
	}

	/** @return Values[DefaultKey] */
	public function getDefaultValue(): string
	{
		return $this->values[$this->defaultKey];
	}
}

//type tests
$a = new SortableListType(["a"=> "1", "b" => "2"], "not-exist-key");
$b = new SortableListType(["a"=> "1", "b" => "2"], "a");
$c = new SortableListType(["a"=> "1", "b" => "2"], "b");

//return type tests
$bKey = $b->getDefaultKey();
$bValue = $b->getDefaultValue();

$cKey = $c->getDefaultKey();
$cValue = $c->getDefaultValue();
Psalm output (using commit a75d26a):

ERROR: InvalidArgument - 33:52 - Argument 2 of SortableListType::__construct expects 'a'|'b', but 'not-exist-key' provided

INFO: UnusedVariable - 33:1 - $a is never referenced or the value is not used

INFO: UnusedVariable - 38:1 - $bKey is never referenced or the value is not used

INFO: UnusedVariable - 39:1 - $bValue is never referenced or the value is not used

INFO: UnusedVariable - 41:1 - $cKey is never referenced or the value is not used

INFO: UnusedVariable - 42:1 - $cValue is never referenced or the value is not used

ERROR: MismatchingDocblockReturnType - 25:14 - Docblock has incorrect return type 'Values[DefaultKey]', should be 'string'

@Nikolay-Ivanchenko Nikolay-Ivanchenko changed the title MismatchingDocblockReturnType array template MismatchingDocblockReturnType array template string Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant