← All results

Strict in_array() narrowing of the needle

Group: Assertions and narrowing · Category: ecosystem · File: assertions_in_array_strict_narrowing.php

A strict in_array($needle, $haystack, true) that succeeds proves the needle is one of the haystack's elements, so a ?User needle searched in a list<User> is a User inside the branch. A tool that does not narrow on in_array reports a possibly-null method call on a valid line. Honour is silence on the narrowed name() call. A tool that does not narrow reports a possibly-null method call there. The loose comparison (null == '' style coercions) is not a control: an unsound nullable reading would fail it for a reason that is not in_array. References: - PHPStan InArrayFunctionTypeSpecifyingExtension - Psalm in_array assertion

Analyzer results

AnalyzerVersionResultDiagnostics
phan 6.0.10Phan 6.0.10 php-ast version 1.1.3 PHP version used to run Phan: 8.5.11 Pass
assertions_in_array_strict_narrowing.php:45: Call to undeclared method \Conformance\Tests\AssertionsInArrayStrictNarrowing\User::missing [PhanUndeclaredMethod]
phpstan 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Pass reported Lv.2+
assertions_in_array_strict_narrowing.php:45: Call to an undefined method Conformance\Tests\AssertionsInArrayStrictNarrowing\User::missing(). [reported-from-level=2]
assertions_in_array_strict_narrowing.php:45: Parameter #1 (mixed) of echo cannot be converted to string. [reported-from-level=10]
With strict-rules
assertions_in_array_strict_narrowing.php:45: Call to an undefined method Conformance\Tests\AssertionsInArrayStrictNarrowing\User::missing().
assertions_in_array_strict_narrowing.php:45: Parameter #1 (mixed) of echo cannot be converted to string.
psalm 6.20.0Psalm 6.20.0@804f03c0c183d149fcd4b8c7c0c9b3fbf836d34c
next: 7.0.0-rc2
Pass
assertions_in_array_strict_narrowing.php:45: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
assertions_in_array_strict_narrowing.php:45: Method Conformance\Tests\AssertionsInArrayStrictNarrowing\User::missing does not exist [UndefinedMethod]
mago 1.57.0mago 1.57.0 Pass
assertions_in_array_strict_narrowing.php:45: Method `missing` does not exist on type `Conformance\Tests\AssertionsInArrayStrictNarrowing\User`. [non-existent-method]
assertions_in_array_strict_narrowing.php:45: The first value for `echo` is too general. [mixed-argument]
mir 0.87.0mir 0.87.0 Fail
assertions_in_array_strict_narrowing.php:37: PossiblyNullMethodCall: Cannot call method name() on possibly null value [MIR0107]
assertions_in_array_strict_narrowing.php:45: PossiblyNullMethodCall: Cannot call method missing() on possibly null value [MIR0107]
assertions_in_array_strict_narrowing.php:45: UndefinedMethod: Method Conformance\Tests\AssertionsInArrayStrictNarrowing\User::missing() does not exist [MIR0004]
Expectation diff
Line 37: Expected silence (quiet probe), got ["PossiblyNullMethodCall: Cannot call method name() on possibly null value [MIR0107]"]
phpantom 0.11.1phpantom_lsp 0.11.1 Pass
assertions_in_array_strict_narrowing.php:45: Method 'missing' not found on class 'Conformance\Tests\AssertionsInArrayStrictNarrowing\User' [unknown_member]
intelephense 1.18.5intelephense 1.18.5 Fail
assertions_in_array_strict_narrowing.php:37: Expected type 'object'. Found 'null|Conformance\Tests\AssertionsInArrayStrictNarrowing\User'. [P1006]
assertions_in_array_strict_narrowing.php:45: Expected type 'string|Stringable|null'. Found 'mixed'. [P1006]
assertions_in_array_strict_narrowing.php:45: Expected type 'object'. Found 'null|Conformance\Tests\AssertionsInArrayStrictNarrowing\User'. [P1006]
assertions_in_array_strict_narrowing.php:45: Undefined method 'missing'. [P1013]
Expectation diff
Line 37: Expected silence (quiet probe), got ["Expected type 'object'. Found 'null|Conformance\\Tests\\AssertionsInArrayStrictNarrowing\\User'. [P1006]"]
phpactor 2026.10.07.0Phpactor 2026.10.07.0 Pass
assertions_in_array_strict_narrowing.php:45: Method "missing" does not exist on class "Conformance\Tests\AssertionsInArrayStrictNarrowing\User" [missing_member]
phpy 1.0.19409phpy 1.0.19409 Pass
assertions_in_array_strict_narrowing.php:45: Call to unknown method: Conformance\Tests\AssertionsInArrayStrictNarrowing\User::missing()
qodana 262.10968.76PhpStorm 262.10968.76 Fail

No diagnostics reported.

Expectation diff
Line 45: Expected 1 error(s)
noverify 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 Fail
assertions_in_array_strict_narrowing.php:34: param $users miss matched with phpdoc type <<list<User>>> [funcParamTypeMissMatch]
assertions_in_array_strict_narrowing.php:37: potential null dereference in candidate when accessing method [notNullSafetyVariable]
assertions_in_array_strict_narrowing.php:42: param $users miss matched with phpdoc type <<list<User>>> [funcParamTypeMissMatch]
assertions_in_array_strict_narrowing.php:45: Call to undefined method {\Conformance\Tests\AssertionsInArrayStrictNarrowing\User|null}->missing() [undefinedMethod]
assertions_in_array_strict_narrowing.php:45: potential null dereference in candidate when accessing method [notNullSafetyVariable]
assertions_in_array_strict_narrowing.php:50: param $status miss matched with phpdoc type <<'draft'|'published'>> [funcParamTypeMissMatch]
Expectation diff
Line 37: Expected silence (quiet probe), got ["potential null dereference in candidate when accessing method [notNullSafetyVariable]"]
Line 34: Unexpected errors ["param $users miss matched with phpdoc type <<list<User>>> [funcParamTypeMissMatch]"]
Line 42: Unexpected errors ["param $users miss matched with phpdoc type <<list<User>>> [funcParamTypeMissMatch]"]
Line 50: Unexpected errors ["param $status miss matched with phpdoc type <<'draft'|'published'>> [funcParamTypeMissMatch]"]
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Fail

No diagnostics reported.

Expectation diff
Line 45: Expected 1 error(s)
phpstan-strict 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Pass
assertions_in_array_strict_narrowing.php:45: Call to an undefined method Conformance\Tests\AssertionsInArrayStrictNarrowing\User::missing().
assertions_in_array_strict_narrowing.php:45: Parameter #1 (mixed) of echo cannot be converted to string.
psalm-next 7.0.0-rc2Psalm 7.0.0-rc2@20339e3fe5b8d03bcd8061030eb2a83ad50e5783 Pass
assertions_in_array_strict_narrowing.php:45: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
assertions_in_array_strict_narrowing.php:45: Method Conformance\Tests\AssertionsInArrayStrictNarrowing\User::missing does not exist [UndefinedMethod]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\AssertionsInArrayStrictNarrowing;

/**
 * Strict `in_array()` narrowing of the needle.
 *
 * A strict `in_array($needle, $haystack, true)` that succeeds proves the
 * needle is one of the haystack's elements, so a `?User` needle searched in a
 * `list<User>` is a User inside the branch. A tool that does not narrow on
 * `in_array` reports a possibly-null method call on a valid line.
 *
 * Honour is silence on the narrowed `name()` call. A tool that does not
 * narrow reports a possibly-null method call there. The loose comparison
 * (`null == ''` style coercions) is not a control: an unsound nullable
 * reading would fail it for a reason that is not `in_array`.
 *
 * References:
 * - PHPStan InArrayFunctionTypeSpecifyingExtension
 * - Psalm in_array assertion
 */

final class User
{
    public function name(): string
    {
        return 'user';
    }
}

/** @param list<User> $users */
function strictNeedle(?User $candidate, array $users): void
{
    if (in_array($candidate, $users, true)) {
        echo $candidate->name(); // Q: the needle is a User here
    }
}

/** @param list<User> $users */
function strictNeedleMissing(?User $candidate, array $users): void
{
    if (in_array($candidate, $users, true)) {
        echo $candidate->missing(); // E: narrowed to User, which has no missing()
    }
}

/** @param 'draft'|'published' $status */
function takesStatus(string $status): void
{
    echo $status;
}

function strictLiteralHaystack(string $status): void
{
    if (in_array($status, ['draft', 'published'], true)) {
        takesStatus($status); // V: narrowed to the haystack's literals
    }
}