[TypeDeclaration] Handle object|static return on ReturnTypeDeclarationRector (#840)

* [TypeDeclaration] Handle object|static return on ReturnTypeDeclarationRector

* Fixed 🎉

* clean up

* clean up

* [ci-review] Rector Rectify

* clean up

* final touch: clean up

* phpstan

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2021-09-06 19:18:02 +07:00 committed by GitHub
parent 2c84f127e9
commit ee95517c5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 1 deletions

View File

@ -25,6 +25,7 @@ use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
@ -47,7 +48,8 @@ final class UnionTypeMapper implements TypeMapperInterface
private PhpVersionProvider $phpVersionProvider,
private UnionTypeAnalyzer $unionTypeAnalyzer,
private BoolUnionTypeAnalyzer $boolUnionTypeAnalyzer,
private UnionTypeCommonTypeNarrower $unionTypeCommonTypeNarrower
private UnionTypeCommonTypeNarrower $unionTypeCommonTypeNarrower,
private NodeNameResolver $nodeNameResolver
) {
}
@ -185,6 +187,14 @@ final class UnionTypeMapper implements TypeMapperInterface
return null;
}
private function hasObjectAndStaticType(PhpParserUnionType $phpParserUnionType): bool
{
$typeNames = $this->nodeNameResolver->getNames($phpParserUnionType->types);
$diff = array_diff(['object', 'static'], $typeNames);
return $diff === [];
}
/**
* @return Name|FullyQualified|PhpParserUnionType|null
*/
@ -201,6 +211,10 @@ final class UnionTypeMapper implements TypeMapperInterface
return null;
}
if ($this->hasObjectAndStaticType($phpParserUnionType)) {
return null;
}
return $phpParserUnionType;
}

View File

@ -0,0 +1,21 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\FixtureForPhp80;
class SkipStaticObject
{
/**
* @var object
*/
protected $obj;
public function get($obj = null)
{
if (func_num_args() === 0) {
return $this->obj;
}
$this->obj = $obj;
return $this;
}
}