Skip to content

Conversation

@staabm
Copy link
Contributor

@staabm staabm commented Nov 8, 2025

@staabm staabm marked this pull request as ready for review November 8, 2025 10:10
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm staabm force-pushed the Bug13546 branch 2 times, most recently from 50d0626 to 885233d Compare November 13, 2025 21:04
@staabm staabm force-pushed the Bug13546 branch 2 times, most recently from bd15a28 to d909a10 Compare January 27, 2026 09:15
}
}

// array_key_first($a) !== null
Copy link
Contributor

Choose a reason for hiding this comment

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

I thought you forgot null !== array_key_first($a) but seems like it works looking at the test.
I'm not familiar with this code to know why at first sight.

Copy link
Contributor Author

@staabm staabm Feb 10, 2026

Choose a reason for hiding this comment

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

we are in resolveNormalizedIdentical, which is invoked in both directions here:

// Normalize to: fn() === expr
if ($rightExpr instanceof FuncCall && !$leftExpr instanceof FuncCall) {
$specifiedTypes = $this->resolveNormalizedIdentical(new Expr\BinaryOp\Identical(
$rightExpr,
$leftExpr,
), $scope, $context);
} else {
$specifiedTypes = $this->resolveNormalizedIdentical(new Expr\BinaryOp\Identical(
$leftExpr,
$rightExpr,
), $scope, $context);
}

&& $unwrappedLeftExpr->name instanceof Name
&& in_array($unwrappedLeftExpr->name->toLowerString(), ['array_key_first', 'array_key_last'], true)
&& isset($unwrappedLeftExpr->getArgs()[0])
&& $rightType->isNull()->yes()
Copy link
Contributor

Choose a reason for hiding this comment

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

You're getting escaped mutant here, but if I understand correctly, it should be supported by

if (array_key_first($array) !== $nullOrInt) {
		assertType('list<string>', $array);
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agree. I tested it locally and the test starts failling when I manually mutate the code.

@staabm staabm merged commit 48301e6 into phpstan:2.1.x Feb 10, 2026
634 of 639 checks passed
@staabm staabm deleted the Bug13546 branch February 10, 2026 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive with array_key_first and reportPossiblyNonexistentGeneralArrayOffset

4 participants