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
6 changes: 4 additions & 2 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,7 @@ public function obtainResultForNode(Expr $node): ExpressionResult
$storage = $this->expressionResultStorageStack->getCurrent();
if ($storage !== null) {
$result = $storage->findExpressionResult($node);
if ($result !== null) {
if ($result !== null && $result->askScopeVariableStateMatches($scope, $scope->nativeTypesPromoted)) {
return $result;
}
}
Expand All @@ -1434,7 +1434,9 @@ public function obtainResultForNode(Expr $node): ExpressionResult
));
}

// a synthetic node, or no analysis in progress
// a synthetic node, a counterfactual ask (the asking scope re-binds a
// variable the node reads, e.g. array_filter narrowing by its callback
// body per element), or no analysis in progress
return $this->container->getByType(NodeScopeResolver::class)->processExprOnDemand(
$node,
$scope,
Expand Down
2 changes: 1 addition & 1 deletion src/Turbo/TurboExtensionEnabler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
final class TurboExtensionEnabler
{

public const EXPECTED_EXTENSION_VERSION = '48d4823';
public const EXPECTED_EXTENSION_VERSION = '64716c0';

private static bool $active = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);

namespace ArrayFilterTypedCallbackParameter;

use function PHPStan\Testing\assertType;

/**
* @param array<mixed> $trace
* @param array{a: array{file: string}|int, b: array{line: int}, c: mixed} $shape
*/
function narrowsTheElementNotTheDeclaredParameter(array $trace, array $shape): void
{
assertType('list<mixed~null>', array_values(array_filter($trace, fn (array $frame) => isset($frame['file']))));
assertType('array<mixed~null>', array_filter($trace, function (array $frame) {
return isset($frame['file']);
}));
assertType('array<mixed~null>', array_filter($trace, fn ($frame) => isset($frame['file'])));
assertType('array<mixed~null>', array_filter($trace, fn (array $frame) => !empty($frame['file'])));
assertType('array{a?: array{file: string}, c?: mixed~null}', array_filter($shape, fn (array $t) => isset($t['file'])));
}
12 changes: 10 additions & 2 deletions turbo-ext/src/MutatingScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2938,7 +2938,13 @@ class MutatingScope
if (!storage.isNull()) {
zv::Val result = storageFind(storage, node);
if (UNEXPECTED(result.isUndef())) return zv::Val();
if (!result.isNull()) return result;
if (!result.isNull()) {
bool promoted;
if (UNEXPECTED(!scopeNativeTypesPromoted(Z_OBJ_P(scope.raw()), promoted))) return zv::Val();
bool matches;
if (UNEXPECTED(!pt_expression_result_ask_scope_variable_state_matches(result.raw(), scope.raw(), promoted, matches))) return zv::Val();
if (matches) return result;
}
}

bool fires;
Expand All @@ -2948,7 +2954,9 @@ class MutatingScope
return zv::Val();
}

/* a synthetic node, or no analysis in progress */
/* a synthetic node, a counterfactual ask (the asking scope re-binds a
* variable the node reads, e.g. array_filter narrowing by its
* callback body per element), or no analysis in progress */
zv::Val resolver = containerGetByType(PT_LC("PHPStan\\Analyser\\NodeScopeResolver"));
if (UNEXPECTED(resolver.isUndef())) return zv::Val();
zend_object *resolverObject = requireObject(resolver, "processExprOnDemand");
Expand Down
Loading