Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 6 additions & 11 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4367,29 +4367,24 @@ public function specifyExpressionType(Expr $expr, Type $type, Type $nativeType,
&& !$expr->dim instanceof Expr\PostInc
) {
$dimType = $scope->getType($expr->dim)->toArrayKey();
if ($dimType->isInteger()->yes() || $dimType->isString()->yes()) {
if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) {
$exprVarType = $scope->getType($expr->var);
if (!$exprVarType instanceof MixedType && !$exprVarType->isArray()->no()) {
$types = [
new ArrayType(new MixedType(), new MixedType()),
new ObjectType(ArrayAccess::class),
new NullType(),
];
if ($dimType->isInteger()->yes()) {
if ($dimType instanceof ConstantIntegerType) {
$types[] = new StringType();
}
$offsetValueType = TypeCombinator::intersect($exprVarType, TypeCombinator::union(...$types));

if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) {
$offsetValueType = TypeCombinator::intersect(
$offsetValueType,
new HasOffsetValueType($dimType, $type),
);
}

$scope = $scope->specifyExpressionType(
$expr->var,
$offsetValueType,
TypeCombinator::intersect(
TypeCombinator::intersect($exprVarType, TypeCombinator::union(...$types)),
new HasOffsetValueType($dimType, $type),
),
$scope->getNativeType($expr->var),
$certainty,
);
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1169,4 +1169,14 @@ public function testBug8719(): void
$this->analyse([__DIR__ . '/data/bug-8719.php'], []);
}

public function testBug13694(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = true;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;

$this->analyse([__DIR__ . '/data/bug-13694.php'], []);
}

}
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-13694.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* @param int[] $keys
* @param array<int, int|null> $things
*/
function evaluateThings(array $keys, array $things): void
{
foreach ($keys as $key) {
if (array_key_exists($key, $things) && $things[$key] === null) {
echo "Value for key $key is null\n";
continue;
}

if (isset($things[$key])) {
echo "Key $key is set\n";
}
}
}
Loading