Skip to content
Merged
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
25 changes: 25 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,31 @@ public function specifyTypesInCondition(
$specifiedTypes = $specifiedTypes->unionWith(
$this->create($dimFetch, $arrayType->getIterableValueType(), TypeSpecifierContext::createTrue(), $scope),
);
} elseif ($expr->var instanceof Expr\Variable && is_string($expr->var->name)) {
Copy link
Copy Markdown
Contributor

@staabm staabm Apr 27, 2026

Choose a reason for hiding this comment

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

doesn't this change mean we need a similar fix for array_search and !==false?

https://phpstan.org/r/e9efce03-3662-41f7-bfa7-449aa480f9ba

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, I agree, also array_find_key

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ok, created phpstan/phpstan#14537

// The array might be empty here, so we cannot register
// $arr[$key] unconditionally. Attach a conditional holder
// that fires once the user narrows $key to non-null
// (e.g. `if ($key !== null)`), giving the deep-write
// path the same shape information `isset($arr[$key])`
// would have provided.
$keyType = $scope->getType($expr->expr);
$nonNullKeyType = TypeCombinator::removeNull($keyType);
if (!$nonNullKeyType instanceof NeverType && !$keyType->isNull()->yes()) {
$dimFetch = new ArrayDimFetch($arrayArg, $expr->var);
$dimFetchString = $this->exprPrinter->printExpr($dimFetch);
$keyExprString = $this->exprPrinter->printExpr($expr->var);

$holder = new ConditionalExpressionHolder(
[$keyExprString => ExpressionTypeHolder::createYes($expr->var, $nonNullKeyType)],
ExpressionTypeHolder::createYes($dimFetch, $arrayType->getIterableValueType()),
);

$specifiedTypes = $specifiedTypes->unionWith(
(new SpecifiedTypes([], []))->setNewConditionalExpressionHolders([
$dimFetchString => [$holder->getKey() => $holder],
]),
);
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/Parser/RichParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ private function parseIdentifiers(string $text, int $ignorePos): array
throw new IgnoreParseException('Missing identifier', 1);
}

/** @phpstan-ignore return.type (return type is correct, not sure why it's being changed from array shape to key-value shape) */
return $identifiers;
}

Expand Down
63 changes: 63 additions & 0 deletions tests/PHPStan/Analyser/nsrt/array-key-last-existing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types=1);

namespace ArrayKeyLastExisting;

use function PHPStan\Testing\assertType;

/**
* Mirrors the RichParser pattern: the array starts empty, gets entries
* appended in some loop branches, and an existing entry's nested key is
* updated in others. `if ($key !== null)` should be enough to let PHPStan
* track that `$arr[$key]` exists and the deep write should preserve the
* outer shape, just like `isset($arr[$key])` does.
*/
function appendThenUpdateLast(string $name, string $comment): void
{
$identifiers = [];
$c = rand(100, 200);
for ($i = 0; $i < $c; $i++) {
if (rand(0, 1) === 1) {
$key = array_key_last($identifiers);
if ($key !== null) {
$identifiers[$key]['comment'] = $comment;
}
continue;
}

$identifiers[] = ['name' => $name, 'comment' => null];
}

assertType('list<array{name: string, comment: string|null}>', $identifiers);
}

function appendThenUpdateFirst(string $name, string $comment): void
{
$identifiers = [];
$c = rand(100, 200);
for ($i = 0; $i < $c; $i++) {
if (rand(0, 1) === 1) {
$key = array_key_first($identifiers);
if ($key !== null) {
$identifiers[$key]['comment'] = $comment;
}
continue;
}

$identifiers[] = ['name' => $name, 'comment' => null];
}

assertType('list<array{name: string, comment: string|null}>', $identifiers);
}

/**
* @param list<array{name: 'x', comment: null}> $list
*/
function maybeEmptyArray(array $list): void
{
$key = array_key_last($list);
if ($key !== null) {
assertType('array{name: \'x\', comment: null}', $list[$key]);
$list[$key]['comment'] = 'hello';
assertType('non-empty-list<array{name: \'x\', comment: \'hello\'}>', $list);
}
}
Loading