Skip to content

Commit

Permalink
Fix nullable closure parameter type
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 5, 2022
1 parent 7a66cb6 commit c05ba98
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1588,7 +1588,7 @@ private function resolveType(Expr $node): Type
$parameters[] = new NativeParameterReflection(
$param->var->name,
$firstOptionalParameterIndex !== null && $i >= $firstOptionalParameterIndex,
$this->getFunctionType($param->type, $param->type === null, false),
$this->getFunctionType($param->type, $this->isParameterValueNullable($param), false),
$param->byRef
? PassedByReference::createCreatesNewVariable()
: PassedByReference::createNo(),
Expand Down
4 changes: 4 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,10 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5668.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/generics-empty-array.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/bug-5757.php');

if (PHP_VERSION_ID >= 70400) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/nullable-closure-parameter.php');
}
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6584.php');
}

Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStan/Analyser/data/nullable-closure-parameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php // lint >= 7.4

namespace NullableClosureParameter;

use function PHPStan\Testing\assertType;

class Foo
{

public function doFoo()
{
$a = function (string $test = null) {
assertType('string|null', $test);
return $test;
};
assertType('string|null', $a());

$b = fn (string $test = null) => $test;
assertType('string|null', $b());

fn (string $test = null): string => assertType('string|null', $test);
}

}
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Functions/CallCallablesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,25 @@ public function testFirstClassCallables(): void
]);
}

public function testBug6701(): void
{
if (PHP_VERSION_ID < 70400 && !self::$useStaticReflectionProvider) {
$this->markTestSkipped('Test requires PHP 7.4.');
}
$this->analyse([__DIR__ . '/data/bug-6701.php'], [
[
'Parameter #1 $test of closure expects string|null, int given.',
14,
],
[
'Parameter #1 $test of closure expects string|null, int given.',
18,
],
[
'Parameter #1 $test of closure expects string|null, int given.',
24,
],
]);
}

}
27 changes: 27 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-6701.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Bug6701;

class Foo
{

public function doFoo(int $i)
{
$a = function ( string $test = null ): string {
return $test ?? '';
};
$a(null);
$a($i);

$b = fn ( string $test = null ): string => $test ?? '';
$b(null);
$b($i);

$c = function ( ?string $test = null ): string {
return $test ?? '';
};
$c(null);
$c($i);
}

}

0 comments on commit c05ba98

Please sign in to comment.