Git Product home page Git Product logo

Comments (1)

psalm-github-bot avatar psalm-github-bot commented on September 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.