Phpstan: Misleading error message: count() always result in number 1.

Created on 23 Aug 2017  路  3Comments  路  Source: phpstan/phpstan

Call to function count() with argument type mixed[]|mixed[][]|string will always result in number 1.
https://phpstan.org/r/3334ad999449bb29b040df8d583d3b1f

/** @var mixed[]|mixed[][]|string */
$var = [];
count($var);

Actually, not always, but may sometimes.

minor-bug

Most helpful comment

Closing this since there's no longer a problem on dev-master:

https://phpstan.org/r/92d13b0f2fd544371f2eed4075949634 (no problem reported until level 6)

All 3 comments

I get that the always word is meant in other meaning, e.g. string, but there may be a better message such as: Call to function count() with argument type mixed[]|mixed[][]|string, where string type will always result in number 1., etc.

Same issue here, minimal failing test:

<?php
declare(strict_types=1);

class Foo
{
    /** @var int[]|null */
    private $array;

    public function __construct()
    {
        // no expensive operations
    }

    private function init(): void
    {
        // expensive operations for lazy init
        $this->array = [1, 2, 3];
    }

    private function doCalculations(): void
    {
        // identified as error
        if (count($this->array) > 0) {
            // do some stuff
        }

        // no error
        if (is_array($this->array) && count($this->array) > 0) {
            // do some stuff
        }
    }
}

Causes:
screen shot 2018-06-11 at 18 26 17

Closing this since there's no longer a problem on dev-master:

https://phpstan.org/r/92d13b0f2fd544371f2eed4075949634 (no problem reported until level 6)

Was this page helpful?
0 / 5 - 0 ratings