0

PHPstan seems to incorrectly narrow type after if (empty(...)).

\PHPStan\dumpType($_GET); // array<mixed>
    
if (empty($_GET['ssid'])) { /* do nothing */ }

\PHPStan\dumpType($_GET); // non-empty-array&hasOffsetValue('ssid', mixed~(0|0.0|''|'0'|array{}|false|null))

In my opinion the type should remain array<mixed> even after the if test (which may result false). Does this narrowing make sense somehow or is it a bug?

(PHPstan 2.1.10, level 10)

2 Answers 2

1

You are right. Please report this bug on PHPStan’s GitHub.

Sign up to request clarification or add additional context in comments.

Comments

0

Your expectation that $_GET should remain array<mixed> makes sense in some cases because:

  • The empty() check only provides information about a single key (ssid), but PHPStan refines the entire $_GET array.

  • This could lead to incorrect inferences if the code is modified $_GET dynamically later.

    -> Use isset() instead of empty(), which might not refine the type in the same way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.