This commit is contained in:
Tomas Votruba 2021-06-28 01:47:42 +02:00 committed by GitHub
parent 947da2c680
commit dc056580e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 13 deletions

View File

@ -12,21 +12,25 @@ use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\MixedType;
use PHPStan\Type\TypeWithClassName;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\Isset_\IssetOnPropertyObjectToPropertyExistsRector\IssetOnPropertyObjectToPropertyExistsRectorTest
*
* @see https://3v4l.org/TI8XL Change isset on property object to property_exists() with not null check
*/
final class IssetOnPropertyObjectToPropertyExistsRector extends AbstractRector
{
public function __construct(
private ReflectionProvider $reflectionProvider
private ReflectionProvider $reflectionProvider,
private ReflectionResolver $reflectionResolver
) {
}
@ -81,8 +85,8 @@ CODE_SAMPLE
continue;
}
$property = $this->nodeRepository->findPropertyByPropertyFetch($issetVar);
if ($property instanceof Property && $property->type) {
// has property PHP 7.4 type?
if ($this->hasPropertyTypeDeclaration($issetVar)) {
continue;
}
@ -136,4 +140,14 @@ CODE_SAMPLE
{
return new NotIdentical($expr, $this->nodeFactory->createNull());
}
private function hasPropertyTypeDeclaration(PropertyFetch $propertyFetch): bool
{
$phpPropertyReflection = $this->reflectionResolver->resolvePropertyReflectionFromPropertyFetch($propertyFetch);
if (! $phpPropertyReflection instanceof PhpPropertyReflection) {
return false;
}
return ! $phpPropertyReflection->getNativeType() instanceof MixedType;
}
}

View File

@ -78,15 +78,7 @@ CODE_SAMPLE
*/
public function refactor(Node $node): ?Node
{
if ($node->isFinal()) {
return null;
}
if ($node->isAbstract()) {
return null;
}
if ($this->classAnalyzer->isAnonymousClass($node)) {
if ($this->shouldSkipClass($node)) {
return null;
}
@ -125,4 +117,17 @@ CODE_SAMPLE
return false;
}
private function shouldSkipClass(Class_ $class): bool
{
if ($class->isFinal()) {
return true;
}
if ($class->isAbstract()) {
return true;
}
return $this->classAnalyzer->isAnonymousClass($class);
}
}