Skip to content

assertNotFalse() doesn't remove FALSE from the union type #83

Open
@morozov

Description

@morozov
$ composer show | grep phpstan
phpstan/phpdoc-parser                          0.4.4   PHPDoc parser with support for nul...
phpstan/phpstan                                0.12.50 PHPStan - PHP Static Analysis Tool
phpstan/phpstan-strict-rules

Consider the following test:

<?php

declare(strict_types=1);

namespace SomeNamespace;

use PHPUnit\Framework\TestCase;

use function rand;

class PHPStanTest extends TestCase
{
    public function testCurrentDate(): void
    {
        $data = $this->f2();
        self::assertNotFalse($data);

        $this->f1(...$data);
    }

    private function f1(int $a, int $b): void
    {
    }

    /** @return array<int,int>|false */
    private function f2()
    {
        return rand(-1, 1) > 0 ? [1, 2] : false;
    }
}

PHPStan flags it as:

$ phpstan a PHPStanTest.php
Note: Using configuration file phpstan.neon.dist.
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ -----------------------------------------------------------------------------
  Line   PHPStanTest.php
 ------ -----------------------------------------------------------------------------
  18     Only iterables can be unpacked, array<int, int>|false given in argument #1.
 ------ -----------------------------------------------------------------------------

 [ERROR] Found 1 error

Replacing self::assertNotFalse($data) with assert($data !== false) works around the problem.

Activity

ondrejmirtes

ondrejmirtes commented on Oct 18, 2020

@ondrejmirtes
Member

Weird. This is the equivalent of what the extension does, and it works... https://phpstan.org/r/267b2ee0-58b9-4748-9360-3da17b5a4196

InvisibleSmiley

InvisibleSmiley commented on Oct 21, 2021

@InvisibleSmiley

I have a similar problem with self::assertNotNull. Plain assert($var !== null) has the desired effect.
Shall I open a new issue or is it the same thing underneath?

 public function testFoo(): void {
        $items = [null, new \DateTimeImmutable()];
        $var = $items[rand(0, 1)];
        self::assertNotNull($var);
        echo $var->format('Ymd'); // PHPStan error reported here
        assert($var !== null);
        echo $var->format('Ymd'); // ... but not here
    }
ondrejmirtes

ondrejmirtes commented on Oct 21, 2021

@ondrejmirtes
Member

@InvisibleSmiley Are you sure you have the phpstan-phpunit extension installed and enabled? This should work without a problem.

InvisibleSmiley

InvisibleSmiley commented on Oct 22, 2021

@InvisibleSmiley

I'm very sorry, I forgot to require phpstan/extension-installer in the affected project. In light of that, please disregard my previous comment.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      assertNotFalse() doesn't remove FALSE from the union type · Issue #83 · phpstan/phpstan-phpunit