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
78 changes: 78 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2719,6 +2719,7 @@

$closureScope = $scope->enterAnonymousFunction($expr, $callableParameters);
$closureScope = $closureScope->processClosureScope($scope, null, $byRefUses);
$closureScope = $this->applyArrayKeysSourceToScope($expr, $closureScope);
$closureType = $closureScope->getAnonymousFunctionReflection();
if (!$closureType instanceof ClosureType) {
throw new ShouldNotHappenException();
Expand Down Expand Up @@ -2881,6 +2882,7 @@
$arrowFunctionCallArgs,
$passedToType,
));
$arrowFunctionScope = $this->applyArrayKeysSourceToScope($expr, $arrowFunctionScope);
$arrowFunctionType = $arrowFunctionScope->getAnonymousFunctionReflection();
if ($arrowFunctionType === null) {
throw new ShouldNotHappenException();
Expand Down Expand Up @@ -3331,6 +3333,8 @@
}
}

$this->detectArrayKeysInSiblingArgs($args, $i, $arg->value);

$this->callNodeCallbackWithExpression($nodeCallback, $arg->value, $scopeToPass, $storage, $context);
$closureResult = $this->processClosureNode($stmt, $arg->value, $scopeToPass, $storage, $nodeCallback, $context, $parameterType ?? null);
if ($this->callCallbackImmediately($parameter, $parameterType, $calleeReflection)) {
Expand Down Expand Up @@ -3389,6 +3393,8 @@
}
}

$this->detectArrayKeysInSiblingArgs($args, $i, $arg->value);

$this->callNodeCallbackWithExpression($nodeCallback, $arg->value, $scopeToPass, $storage, $context);
$arrowFunctionResult = $this->processArrowFunctionNode($stmt, $arg->value, $scopeToPass, $storage, $nodeCallback, $parameterType ?? null);
if ($this->callCallbackImmediately($parameter, $parameterType, $calleeReflection)) {
Expand Down Expand Up @@ -3920,6 +3926,78 @@
return $this->processVarAnnotation($scope, $vars, $stmt);
}

private const ARRAY_KEYS_SOURCE_ATTRIBUTE = 'arrayKeysSourceExprs';

/**
* @param Arg[] $args
*/
private function detectArrayKeysInSiblingArgs(array $args, int $currentIndex, Expr $callbackExpr): void
{
$arrayKeysSourceExprs = [];
foreach ($args as $j => $otherArg) {
if ($j === $currentIndex) {
continue;
}
if (
!($otherArg->value instanceof FuncCall)
|| !($otherArg->value->name instanceof Name)
|| $otherArg->value->name->toLowerString() !== 'array_keys'
) {
continue;
}

$funcArgs = $otherArg->value->getArgs();
if (count($funcArgs) !== 1) {
continue;
}

$arrayKeysSourceExprs[] = $funcArgs[0]->value;
}
if ($arrayKeysSourceExprs === []) {
return;
}

$callbackExpr->setAttribute(self::ARRAY_KEYS_SOURCE_ATTRIBUTE, $arrayKeysSourceExprs);
}

private function applyArrayKeysSourceToScope(Expr $callbackExpr, MutatingScope $scope): MutatingScope
{
/** @var Expr[]|null $arrayKeysSourceExprs */
$arrayKeysSourceExprs = $callbackExpr->getAttribute(self::ARRAY_KEYS_SOURCE_ATTRIBUTE);
if ($arrayKeysSourceExprs === null) {
return $scope;
}

$params = [];
if ($callbackExpr instanceof Expr\ArrowFunction || $callbackExpr instanceof Expr\Closure) {
$params = $callbackExpr->params;
}

foreach ($arrayKeysSourceExprs as $sourceExpr) {
$sourceType = $scope->getType($sourceExpr);
$sourceNativeType = $scope->getNativeType($sourceExpr);
$keyType = $sourceType->getIterableKeyType();

foreach ($params as $param) {
if (!$param->var instanceof Variable || !is_string($param->var->name)) {
continue;
}
$paramType = $scope->getVariableType($param->var->name);
if (!$keyType->isSuperTypeOf($paramType)->yes()) {

Check warning on line 3986 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ continue; } $paramType = $scope->getVariableType($param->var->name); - if (!$keyType->isSuperTypeOf($paramType)->yes()) { + if ($keyType->isSuperTypeOf($paramType)->no()) { continue; }

Check warning on line 3986 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ continue; } $paramType = $scope->getVariableType($param->var->name); - if (!$keyType->isSuperTypeOf($paramType)->yes()) { + if (!$paramType->isSuperTypeOf($keyType)->yes()) { continue; }

Check warning on line 3986 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ continue; } $paramType = $scope->getVariableType($param->var->name); - if (!$keyType->isSuperTypeOf($paramType)->yes()) { + if ($keyType->isSuperTypeOf($paramType)->no()) { continue; }

Check warning on line 3986 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ continue; } $paramType = $scope->getVariableType($param->var->name); - if (!$keyType->isSuperTypeOf($paramType)->yes()) { + if (!$paramType->isSuperTypeOf($keyType)->yes()) { continue; }
continue;
}

$scope = $scope->assignExpression(
new ArrayDimFetch($sourceExpr, $param->var),
$sourceType->getIterableValueType(),
$sourceNativeType->getIterableValueType(),
);
}
}

return $scope;
}

/**
* @param callable(Node $node, Scope $scope): void $nodeCallback
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1263,4 +1263,11 @@ public function testBug14308(): void
$this->analyse([__DIR__ . '/data/bug-14308.php'], []);
}

public function testBug14265(): void
{
$this->reportPossiblyNonexistentConstantArrayOffset = true;

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

}
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-14265.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types = 1);

namespace Bug14265;

/**
* @param mixed $someVar
*/
function doFoo($someVar): string
{
$a = [
'k1' => '1',
];
if (!empty($someVar)) {
$a['k2'] = '1';
}
$b = array_reduce(
array_keys($a),
fn($carry, $key) => $carry . ' ' . $key . '="' . htmlspecialchars($a[$key]) . '"',
''
);

return $b;
}
Loading