Skip to content

Commit

Permalink
[Php74] Handle Multiple types with NullType on TypedPropertyRector wh…
Browse files Browse the repository at this point in the history
…en 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>
  • Loading branch information
3 people committed Feb 11, 2022
1 parent 2d94075 commit 03ce06d
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 29 deletions.
17 changes: 15 additions & 2 deletions packages/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,16 @@ private function matchPhpParserUnionType(UnionType $unionType, TypeKind $typeKin
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;
}
Expand All @@ -268,6 +276,11 @@ private function matchPhpParserUnionType(UnionType $unionType, TypeKind $typeKin
}

$phpParserUnionedTypes = array_unique($phpParserUnionedTypes);

if (count($phpParserUnionedTypes) < 2) {
return null;
}

return new PhpParserUnionType($phpParserUnionedTypes);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

?>
Original file line number Diff line number Diff line change
@@ -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;
}
}

?>

This file was deleted.

6 changes: 4 additions & 2 deletions rules/Php74/TypeAnalyzer/ObjectTypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Php74\TypeAnalyzer;

use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
Expand All @@ -23,10 +24,11 @@ public function isSpecial(Type $varType): bool
: [$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;
}
Expand Down

0 comments on commit 03ce06d

Please sign in to comment.