mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-07 03:40:50 +00:00
[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:
parent
2d94075444
commit
03ce06db76
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user