Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 76be530

Browse files
rvanvelzenondrejmirtes
authored andcommitted
Fix closure return type based on by-ref use
1 parent fde174f commit 76be530

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

‎src/Analyser/MutatingScope.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2893,11 +2893,13 @@ private function enterAnonymousFunctionWithoutReflection(
28932893
if (!is_string($use->var->name)) {
28942894
throw new ShouldNotHappenException();
28952895
}
2896+
$variableName = $use->var->name;
2897+
$paramExprString = '$' . $use->var->name;
28962898
if ($use->byRef) {
2899+
$variableTypes[$paramExprString] = VariableTypeHolder::createYes(new MixedType());
2900+
$nativeTypes[$paramExprString] = new MixedType();
28972901
continue;
28982902
}
2899-
$variableName = $use->var->name;
2900-
$paramExprString = '$' . $use->var->name;
29012903
if ($this->hasVariableType($variableName)->no()) {
29022904
$variableType = new ErrorType();
29032905
} else {

‎tests/PHPStan/Rules/Functions/ClosureReturnTypeRuleTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,9 @@ public function testBug3891(): void
9393
$this->analyse([__DIR__ . '/data/bug-3891.php'], []);
9494
}
9595

96+
public function testBug6806(): void
97+
{
98+
$this->analyse([__DIR__ . '/data/bug-6806.php'], []);
99+
}
100+
96101
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug6806;
4+
5+
function () {
6+
// to mimic dynamic type containing various "type"s
7+
$types = explode(',', $_REQUEST['types']);
8+
// static version throws same issue, though
9+
//$types = [ 'a', 'b' ];
10+
$defs = [ 'first' => [ 'type' => 'a' ], 'second' => [ 'type' => 'b' ] ];
11+
12+
$types = array_fill_keys($types, true);
13+
$defs = array_filter($defs, function($def) use(&$types) { return isset($types[$def['type']]); });
14+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /