mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-28 13:43:31 +00:00
Misc (#318)
This commit is contained in:
parent
947da2c680
commit
dc056580e0
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user