Comments (1)
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 */
Psalm output (using commit ef3b018):
INFO: Trace - 32:34 - $r1: string
INFO: Trace - 32:34 - $r2: string
INFO: Trace - 32:34 - $r3: TestInterface
INFO: UnusedVariable - 28:1 - $r1 is never referenced or the value is not used
INFO: UnusedVariable - 29:1 - $r2 is never referenced or the value is not used
INFO: UnusedVariable - 30:1 - $r3 is never referenced or the value is not used
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 */
Psalm output (using commit ef3b018):
INFO: Trace - 40:34 - $r1: TestInterface|null
INFO: Trace - 40:34 - $r2: TestInterface
INFO: Trace - 40:34 - $r3: TestInterface
INFO: UnusedVariable - 36:1 - $r1 is never referenced or the value is not used
INFO: UnusedVariable - 37:1 - $r2 is never referenced or the value is not used
INFO: UnusedVariable - 38:1 - $r3 is never referenced or the value is not used
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 */
Psalm output (using commit ef3b018):
INFO: Trace - 46:34 - $r1: TestInterface|null
INFO: Trace - 46:34 - $r2: TestInterface
INFO: Trace - 46:34 - $r3: TestInterface
INFO: UnusedVariable - 42:1 - $r1 is never referenced or the value is not used
INFO: UnusedVariable - 43:1 - $r2 is never referenced or the value is not used
INFO: UnusedVariable - 44:1 - $r3 is never referenced or the value is not used
ERROR: NullableReturnStatement - 37:16 - The declared return type 'TestInterface' for Test::getValue3 is not nullable, but the function returns 'T:Test as TestInterface|null'
ERROR: InvalidNullableReturnType - 35:34 - The declared return type 'TestInterface' for Test::getValue3 is not nullable, but 'T' contains null
from psalm.
Related Issues (20)
- Allow suppressing a psalm issue for a single Attribute or parameter HOT 1
- "Could not get class storage for X" HOT 1
- Taint analysis: allow marking properties as taint sources? HOT 2
- `non-empty-string` false positive detection HOT 4
- Return type: subtype of type HOT 1
- Inherit `value-of` class constant (static behavior) HOT 1
- @psalm-require-extends does not consider generics HOT 1
- Non-null template value HOT 1
- False-positive `ReservedWord` issue HOT 3
- Docbloc: callable() is not a valid method HOT 1
- Implode of literal-string should be inferred as literal-string HOT 1
- `''` check is loosing literal-string type HOT 1
- `literal-string is non-empty-literal-string` seems to be considered as always true HOT 2
- Internal annotation is propagated to non internal child class HOT 1
- Proposed syntax to document the execution lifetime of callable parameters HOT 1
- [LSP] Method Psalm\Internal\LanguageServer\LanguageServer::cancelRequest() does not exist HOT 2
- UnusedParam false positive when param assigned by reference HOT 2
- `MismatchingDocblockReturnType` with `iterable|object` HOT 1
- UnusedFunctionCall false positive on mail standard extension HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from psalm.