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

PossiblyNullReference not detected using callable classes #5809

Closed
bendavies opened this issue May 20, 2021 · 3 comments
Closed

PossiblyNullReference not detected using callable classes #5809

bendavies opened this issue May 20, 2021 · 3 comments
Labels

Comments

@bendavies
Copy link
Contributor

bendavies commented May 20, 2021

See the last line here, which should warn PossiblyNullReference:

https://psalm.dev/r/165262da3d

changing the method call to __invoke correctly surfaces the error:

https://psalm.dev/r/9a3e913407

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/165262da3d
<?php

declare(strict_types=1);

class Location {
    private int $id;

    public function __construct(int $id) {
        $this->id = $id;
    }

    public function getId(): int
    {
        return $this->id;
    }
}

/**
 * @psalm-immutable
 */
class Application
{
    private ?Location $location;

    public function __construct(?Location $location = null ) {
        $this->location = $location;
    }
  
    public function getLocation(): ?Location
    {
        return $this->location;
    }
}

class TakesId
{
    public function __invoke(
        int $location
    ): int {
        return $location;
    }
}

$takesId = new TakesId();

$application = new Application();

($takesId)($application->getLocation()->getId());
Psalm output (using commit 110c9ef):

No issues!
https://psalm.dev/r/9a3e913407
<?php

declare(strict_types=1);

class Location {
    private int $id;

    public function __construct(int $id) {
        $this->id = $id;
    }

    public function getId(): int
    {
        return $this->id;
    }
}

/**
 * @psalm-immutable
 */
class Application
{
    private ?Location $location;

    public function __construct(?Location $location = null ) {
        $this->location = $location;
    }
  
    public function getLocation(): ?Location
    {
        return $this->location;
    }
}

class TakesId
{
    public function __invoke(
        int $location
    ): int {
        return $location;
    }
}

$takesId = new TakesId();

$application = new Application();

$takesId->__invoke($application->getLocation()->getId());
Psalm output (using commit 110c9ef):

ERROR: PossiblyNullReference - 48:49 - Cannot call method getId on possibly null value

@weirdan
Copy link
Collaborator

weirdan commented May 20, 2021

Simplified: https://psalm.dev/r/024ec41453

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/024ec41453
<?php

interface Location {
    public function getId(): int;
}

/** @psalm-immutable */
interface Application {
    public function getLocation(): ?Location;
}

interface TakesId {
    public function __invoke(int $location): int;
}

function f(TakesId $takesId, Application $application): void {
   ($takesId)($application->getLocation()->getId());
}
Psalm output (using commit 110c9ef):

No issues!

@weirdan weirdan added the bug label May 20, 2021
@muglug muglug closed this as completed in 4b17cc9 May 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants