Skip to content

phpantom_lsp only take into account the last @phpstan-assert-if-true, leading to type_mismatch_argument false positive #434

Description

@landure

Verified on latest main

  • I have confirmed this bug still occurs when built from the latest main branch.

PHPantom version / commit

phpantom_lsp 33718a6

Installation method

Built from source

Operating system

Linux x86_64

Editor

Zed

Bug description

phpantom_lsp only take into account the last @phpstan-assert-if-true

Steps to reproduce

  1. create a file with:
declare(strict_types=1);

class Example {
   public function compute(?\DateTimeInterface $start, ?\DateTimeInterface $end): int|false {
       if ($this->areBothDatesDefined($this->startDate, $this->endDate)) {
            return $this->getMinutesDifference($this->startDate, $this->endDate);
        }

        return false;
    }

    /**
     * @phpstan-assert-if-true \DateTimeInterface $start
     * @phpstan-assert-if-true \DateTimeInterface $end
     */
    private function areBothDatesDefined(?\DateTimeInterface $start, ?\DateTimeInterface $end): bool
    {
        return $start instanceof \DateTimeInterface && $end instanceof \DateTimeInterface;
    }

    /**
     * Get the difference in minutes between end date and start date.
     */
    private function getMinutesDifference(\DateTimeInterface $startDate, \DateTimeInterface $endDate): int
    {
        return (int) round(($endDate->getTimestamp() - $startDate->getTimestamp()) / 60);
    }
}

Error output or panic trace

Argument 1 ($startDate) expects DateTimeInterface, got null|DateTimeImmutable (null does not satisfy DateTimeInterface) 
         🪪  type_mismatch_argument

.phpantom.toml

[diagnostics]
unresolved-member-access = true
extra-arguments = true
workspace = true

[indexing]
strategy = "full"

[phpstan]
command="tools/phpstan/vendor/bin/phpstan"

[phpcs]
command="tools/phpcs/vendor/bin/phpcs"

[formatting]
php-cs-fixer="tools/php-cs-fixer/vendor/bin/php-cs-fixer"
phpcbf="tools/phpcs/vendor/bin/phpcbf"

Additional context

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions