Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutating scope refactor and regression test #1934

Merged
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
36 changes: 22 additions & 14 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2880,6 +2880,7 @@ private function enterAnonymousFunctionWithoutReflection(
$nativeTypes[$paramExprString] = $parameterType;
}

$nonRefVariableNames = [];
foreach ($closure->uses as $use) {
if (!is_string($use->var->name)) {
throw new ShouldNotHappenException();
Expand All @@ -2891,34 +2892,41 @@ private function enterAnonymousFunctionWithoutReflection(
$nativeTypes[$paramExprString] = new MixedType();
continue;
}
$nonRefVariableNames[$variableName] = true;
if ($this->hasVariableType($variableName)->no()) {
$variableType = new ErrorType();
} else {
$variableType = $this->getVariableType($variableName);
$nativeTypes[$paramExprString] = $this->getNativeType($use->var);
}
$expressionTypes[$paramExprString] = ExpressionTypeHolder::createYes($variableType);
}

foreach ($this->expressionTypes as $exprString => $typeHolder) {
$expr = $this->exprStringToExpr((string) $exprString);
if ($expr === null) {
continue;
}
if ($expr instanceof Variable) {
continue;
}
$variable = (new NodeFinder())->findFirst([$expr], static fn (Node $node): bool => $node instanceof Variable);
foreach ($this->expressionTypes as $exprString => $typeHolder) {
$expr = $this->exprStringToExpr((string) $exprString);
if ($expr === null) {
continue;
}
if ($expr instanceof Variable) {
continue;
}
$variables = (new NodeFinder())->findInstanceOf([$expr], Variable::class);
if ($variables === []) {
continue;
rajyan marked this conversation as resolved.
Show resolved Hide resolved
}
foreach ($variables as $variable) {
if (!$variable instanceof Variable) {
continue;
continue 2;
}
if (!is_string($variable->name)) {
continue;
continue 2;
}
if ($variable->name !== $variableName) {
continue;
if (!array_key_exists($variable->name, $nonRefVariableNames)) {
continue 2;
}
$expressionTypes[$exprString] = $typeHolder;
}

$expressionTypes[$exprString] = $typeHolder;
}

if ($this->hasVariableType('this')->yes() && !$closure->static) {
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/nullsafe.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/specified-types-closure-use.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/specified-types-closure-edge.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/cast-to-numeric-string.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2539.php');
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/data/specified-types-closure-edge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace SpecifiedTypesClouserEdge;
use function PHPStan\Testing\assertType;

function doFoo(callable $cb): void
{
}

class HelloWorld
{
public function sayHello(): void
{
if (HelloWorld::someValue() === 5) {
assertType('5', HelloWorld::someValue());
doFoo(function () {
assertType('int', HelloWorld::someValue());
});
}
}

public static function someValue(): int
{

}
}
35 changes: 35 additions & 0 deletions tests/PHPStan/Analyser/data/specified-types-closure-use.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,39 @@ function () use ($call, &$a): void {
}
}

public function doBaz(array $arr, string $key): void
{
$arr[$key] = 'test';
assertType('non-empty-array', $arr);
assertType("'test'", $arr[$key]);
function ($arr) use ($key): void {
assertType('string', $key);
assertType('mixed', $arr);
assertType('mixed', $arr[$key]);
};
}
public function doBuzz(array $arr, string $key): void
{
if (isset($arr[$key])) {
assertType('array', $arr);
assertType("mixed~null", $arr[$key]);
function () use ($arr, $key): void {
assertType('array', $arr);
assertType("mixed~null", $arr[$key]);
};
}
}

public function doBuzz(array $arr, string $key): void
{
if (isset($arr[$key])) {
assertType('array', $arr);
assertType("mixed~null", $arr[$key]);
function ($key) use ($arr): void {
assertType('array', $arr);
assertType("mixed", $arr[$key]);
Comment on lines +66 to +67
Copy link
Contributor Author

Choose a reason for hiding this comment

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

$arr[$key] was mixed~null before this PR

};
}
}

}