[Php74] Handle Multiple types with NullType on TypedPropertyRector when PHP 8.0 Feature enabled (#1803)

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
Co-authored-by: Jordan Gillet <jordan.gillet51@gmail.com>
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2022-02-11 15:31:25 +07:00 committed by GitHub
parent 2d94075444
commit 03ce06db76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 29 deletions

View File

@ -258,8 +258,16 @@ final class UnionTypeMapper implements TypeMapperInterface
return null;
}
/** @var Identifier|Name|null $phpParserNode */
$phpParserNode = $this->phpStanStaticTypeMapper->mapToPhpParserNode($unionedType, $typeKind);
/**
* NullType inside UnionType is allowed
* make it on TypeKind property as changing other type, eg: return type may conflict with parent child implementation
*
* @var Identifier|Name|null $phpParserNode
*/
$phpParserNode = $unionedType instanceof NullType && $typeKind->equals(TypeKind::PROPERTY())
? new Name('null')
: $this->phpStanStaticTypeMapper->mapToPhpParserNode($unionedType, $typeKind);
if ($phpParserNode === null) {
return null;
}
@ -268,6 +276,11 @@ final class UnionTypeMapper implements TypeMapperInterface
}
$phpParserUnionedTypes = array_unique($phpParserUnionedTypes);
if (count($phpParserUnionedTypes) < 2) {
return null;
}
return new PhpParserUnionType($phpParserUnionedTypes);
}

View File

@ -0,0 +1,31 @@
<?php
namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\FixtureUnionIntersectionTypes;
final class MultipleTypes
{
/**
* @var string|int|float
*/
private $nonNullableVariable;
/**
* @var string|int|float|null
*/
private $nullableVariable;
}
?>
-----
<?php
namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\FixtureUnionIntersectionTypes;
final class MultipleTypes
{
private float|int|string $nonNullableVariable;
private float|int|string|null $nullableVariable = null;
}
?>

View File

@ -0,0 +1,48 @@
<?php
namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\FixtureUnionIntersectionTypes;
use Rector\Tests\Php74\Rector\Property\TypedPropertyRector\Source\AnotherClass;
final class NullableVarUnionClassLike
{
/**
* @var null|AnotherClass|\stdClass
*/
private $anotherClass;
public function __construct(?AnotherClass $anotherClass)
{
$this->anotherClass = $anotherClass;
}
public function setStdClass()
{
$this->anotherClass = new \stdClass;
}
}
?>
-----
<?php
namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\FixtureUnionIntersectionTypes;
use Rector\Tests\Php74\Rector\Property\TypedPropertyRector\Source\AnotherClass;
final class NullableVarUnionClassLike
{
private \Rector\Tests\Php74\Rector\Property\TypedPropertyRector\Source\AnotherClass|\stdClass|null $anotherClass;
public function __construct(?AnotherClass $anotherClass)
{
$this->anotherClass = $anotherClass;
}
public function setStdClass()
{
$this->anotherClass = new \stdClass;
}
}
?>

View File

@ -1,25 +0,0 @@
<?php
namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\FixtureClassLikeTypeOnly;
use Rector\Tests\Php74\Rector\Property\TypedPropertyRector\Source\AnotherClass;
final class SkipNullableVarUnion
{
/**
* @var null|AnotherClass|\stdClass
*/
private $anotherClass;
public function __construct(?AnotherClass $anotherClass)
{
$this->anotherClass = $anotherClass;
}
public function setStdClass()
{
$this->anotherClass = new \stdClass;
}
}
?>

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Rector\Php74\TypeAnalyzer;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
@ -23,10 +24,11 @@ final class ObjectTypeAnalyzer
: [$varType];
foreach ($types as $type) {
if ($type instanceof \PHPStan\Type\MixedType) {
if ($type instanceof MixedType) {
// mixed does not exists in PHP 7.4
return true;
}
}
if (! $type instanceof FullyQualifiedObjectType) {
continue;
}