[Downgrade PHP 7.2] Cover nullable param in DowngradeParameterTypeWideningRector (#6225)

This commit is contained in:
Tomas Votruba 2021-04-24 02:53:17 +02:00 committed by GitHub
parent cf58d68f6a
commit aa2c142038
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 1 deletions

View File

@ -0,0 +1,38 @@
<?php
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
interface SomeAskingInterfaceWithNullable
{
public function ask(?callable $callable = null);
}
final class AskForMore implements SomeAskingInterfaceWithNullable
{
public function ask($callable = null)
{
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
interface SomeAskingInterfaceWithNullable
{
/**
* @param callable|null $callable
*/
public function ask($callable = null);
}
final class AskForMore implements SomeAskingInterfaceWithNullable
{
public function ask($callable = null)
{
}
}
?>

View File

@ -7,6 +7,7 @@ namespace Rector\DowngradePhp72\PhpDoc;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\NullType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
@ -67,7 +68,9 @@ final class NativeParamToPhpDocDecorator
$mappedCurrentParamType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);
// add default null type
if ($param->default !== null && $this->valueResolver->isNull($param->default)) {
if ($param->default !== null && $this->valueResolver->isNull($param->default) && ! TypeCombinator::containsNull(
$mappedCurrentParamType
)) {
$mappedCurrentParamType = new UnionType([$mappedCurrentParamType, new NullType()]);
}