psalm
psalm copied to clipboard
@psalm-type does not support parentheses
https://psalm.dev/r/a55241a2df
The error message implies a syntax error, but it seems to be the case that parentheses are just unsupported in @psalm-type
. Perhaps that's by design (last I checked, the docs have very little to say about parentheses). If it is intentional or not worth supporting, perhaps the error message could be a little more straightforward? "Unterminated parentheses" seems incorrect; perhaps "Parentheses are not supported in @psalm-type"?
The parentheses in the example are obviously superfluous. It's less obvious (though probably still superfluous?) if you want to define a large union over multiple lines.
I found these snippets:
https://psalm.dev/r/a55241a2df
<?php
/** @psalm-type arr = (list<string>) */
class Demo {
/** @return arr */
public function f() {
return ["a"];
}
}
Psalm output (using commit 8c33b21):
ERROR: InvalidDocblock - 4:1 - Unterminated parentheses
ERROR: UndefinedDocblockClass - 5:17 - Docblock-defined class, interface or enum named arr does not exist
ERROR: InvalidReturnStatement - 7:16 - The inferred type 'array{"a"}' does not match the declared return type 'arr' for Demo::f
ERROR: InvalidReturnType - 5:17 - The declared return type 'arr' for Demo::f is incorrect, got 'array{"a"}'
Hi Shawn! I ran into the same thing, but I think it's even more odd.
@psalm-type, anything after the >
greater-than symbol is just straight-up ignored by the parser:
https://psalm.dev/r/67abe4ae45
I found these snippets:
https://psalm.dev/r/67abe4ae45
<?php
/**
* @psalm-type maybe0 = (array<string>)
*/
/**
* @psalm-type maybe1 = null|array<string>
* @psalm-type maybe2 = array<string>|null
*/
/** @var maybe0 */
$x = 1;
/** @psalm-trace $x */
isset($x);
/** @var maybe1 */
$x = 1;
/** @psalm-trace $x */
isset($x);
/** @var maybe2 */
$x = 1;
/** @psalm-trace $x */
isset($x);
Psalm output (using commit f6fb715):
ERROR: InvalidDocblock - 12:1 - Unterminated parentheses
ERROR: UndefinedDocblockClass - 12:1 - Docblock-defined class, interface or enum named maybe0 does not exist
INFO: Trace - 15:1 - $x: maybe0
INFO: Trace - 22:1 - $x: array<array-key, string>|null
INFO: Trace - 29:1 - $x: array<array-key, string>