-
Notifications
You must be signed in to change notification settings - Fork 659
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
Bug when using "@template" #10901
Labels
Comments
I found these snippets: https://psalm.dev/r/23d995c8fa<?php
interface TestInterface
{
public function test(): void;
}
function f1(?string $p): string
{
if ($p === null) {
throw new Exception('Error');
}
return $p;
}
function f2(?string $p): string
{
return $p ?? throw new Exception('Error');
}
function f3(?TestInterface $p): TestInterface
{
return $p ?? throw new Exception('Error');
}
$r1 = f1(null);
$r2 = f2(null);
$r3 = f3(null);
/** @psalm-trace $r1, $r2, $r3 */
https://psalm.dev/r/449c3a0338<?php
interface TestInterface
{
public function test(): void;
}
class Test
{
public function __construct(
public TestInterface|null $value
) {
}
public function getValue(): ?TestInterface
{
return $this->value;
}
public function getValue2(): TestInterface
{
if ($this->value === null) {
throw new Exception('Error');
}
return $this->value;
}
public function getValue3(): TestInterface
{
return $this->value ?? throw new Exception('Error');
}
}
$obj = new Test(null);
$r1 = $obj->getValue();
$r2 = $obj->getValue2();
$r3 = $obj->getValue3();
/** @psalm-trace $r1, $r2, $r3 */
https://psalm.dev/r/a80b2963c6<?php
interface TestInterface
{
public function test(): void;
}
/**
* @template T of TestInterface|null
*/
class Test
{
/**
* @psalm-param T $value
*/
public function __construct(
public TestInterface|null $value
) {
}
public function getValue(): ?TestInterface
{
return $this->value;
}
public function getValue2(): TestInterface
{
if ($this->value === null) {
throw new Exception('Error');
}
return $this->value;
}
public function getValue3(): TestInterface
{
return $this->value ?? throw new Exception('Error');
}
}
$obj = new Test(null);
$r1 = $obj->getValue();
$r2 = $obj->getValue2();
$r3 = $obj->getValue3();
/** @psalm-trace $r1, $r2, $r3 */
|
That's probably a good first issue to give a PR a try. As a starting point src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/CoalesceAnalyzer.php |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the first and second examples everything is fine.
In the third, something goes wrong.
Using template results in unexpected NullableReturnStatement, InvalidNullableReturnType errors.
https://psalm.dev/r/23d995c8fa
https://psalm.dev/r/449c3a0338
https://psalm.dev/r/a80b2963c6
The text was updated successfully, but these errors were encountered: