Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truthy isset($arr[$k]) should narrow $k #3453

Draft
wants to merge 7 commits into
base: 1.12.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,9 +822,14 @@ public function specifyTypesInCondition(
$varType = $scope->getType($var->var);
if (!$varType->isIterableAtLeastOnce()->no()) {
$varIterableKeyType = $varType->getIterableKeyType();

if ($varIterableKeyType->isString()->yes()
&& $varIterableKeyType->isNumericString()->no()
) {
if (!$varIterableKeyType->isNonEmptyString()->no()) {
$varIterableKeyType = TypeCombinator::addNull($varIterableKeyType);
}

$types = $types->unionWith(
$this->create(
$var->dim,
Expand Down
13 changes: 13 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-11716.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,16 @@ function emptyArrr($mixed, array $arr)
}
assertType('mixed', $mixed);
}

function emptyString($mixed)
{
// see https://3v4l.org/XHZdr
$arr = ['' => 1, 'a' => 2];

if (isset($arr[$mixed])) {
assertType("''|'a'|null", $mixed);
} else {
assertType('mixed', $mixed);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mixed~(''|'a'|null)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this comment went on the wrong line (or I have puhsed a change at the same time you did the comment) and therefore it does not make sense atm?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see it on correct line, it is for falsy context:

    $arr = [0 => 0, 1 => 1, 2 => 2];
    if (isset($arr[$mixed])) {
    } else {
        // falsy context
    }

if $arr[$k] is set (and not null because of isset), then the consts types in truthy context can be substracted in the falsy context

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is sealed vs unsealed array territory. I would not do this until we have a clear separation of the 2 types

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really? If array specifies some non-optional keys, they must be present (in sealed and unsealed array) and can be substracted in the else branch, can't they?

}
assertType('mixed', $mixed);
}
Loading