Skip to content

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

@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.

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