phpstan-webmozart-assert icon indicating copy to clipboard operation
phpstan-webmozart-assert copied to clipboard

False positive: Call to static method Webmozart\Assert\Assert::allInArray() with array<string> and array<string> will always evaluate to true

Open melkamar opened this issue 2 months ago • 0 comments

The following code causes a false PHPStan error. The Assert::allInArray($enabledNames, $supportedNames); assertion certainly isn't always true.

use Webmozart\Assert\Assert;

class C
{
    /**
     * @param array<string> $supportedNames
     * @param array<string> $enabledNames
     */
    public function __construct(
        array $supportedNames,
        array $enabledNames,
    )
    {
        Assert::allInArray($enabledNames, $supportedNames);
    }
}
$ vendor/bin/phpstan analyse -c phpstan.neon test.php
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ --------------------------------------------------------------------------------------------------------------------------------
  Line   test.php
 ------ --------------------------------------------------------------------------------------------------------------------------------
  17     Call to static method Webmozart\Assert\Assert::allInArray() with array<string> and array<string> will always evaluate to true.
         🪪  staticMethod.alreadyNarrowedType
 ------ --------------------------------------------------------------------------------------------------------------------------------



 [ERROR] Found 1 error


===============


$ vendor/bin/phpstan diagnose
Note: Using configuration file /Users/melka/work/rh/phpstan.neon.
PHP runtime version: 8.3.22
PHP version for analysis: 8.3.22 (from runtime)

PHPStan version: 2.1.29
PHPStan running from:
/Users/melka/work/rh/vendor/phpstan/phpstan

Extension installer: Not installed

Included configs from Composer packages:
phpstan/phpstan-webmozart-assert (extension.neon): 2.0.0

Discovered Composer project root:
/Users/melka/work/rh


===============


$ cat phpstan.neon
includes:
    - vendor/phpstan/phpstan-webmozart-assert/extension.neon
parameters:
    level: 4  # lowest level where this starts happening

Also discussed in https://github.com/phpstan/phpstan-webmozart-assert/issues/142#issuecomment-3372330454

melkamar avatar Oct 06 '25 18:10 melkamar