mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 05:18:18 +01:00
Updated Rector to commit f22ddc8f18ef345d5e9c99cbb667548c3c854d7d
f22ddc8f18
Add not null compare to FlipTypeControlToUseExclusiveTypeRector (#3513)
This commit is contained in:
parent
2ba3739f7b
commit
0b3c5d9bcc
@ -17,7 +17,7 @@ final class CurrentTokenIteratorProvider
|
||||
}
|
||||
public function provide() : BetterTokenIterator
|
||||
{
|
||||
if ($this->betterTokenIterator === null) {
|
||||
if (!$this->betterTokenIterator instanceof BetterTokenIterator) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
return $this->betterTokenIterator;
|
||||
|
@ -241,7 +241,7 @@ final class PhpDocInfo
|
||||
*/
|
||||
public function hasByAnnotationClasses(array $annotationsClasses) : bool
|
||||
{
|
||||
return $this->getByAnnotationClasses($annotationsClasses) !== null;
|
||||
return $this->getByAnnotationClasses($annotationsClasses) instanceof DoctrineAnnotationTagValueNode;
|
||||
}
|
||||
/**
|
||||
* @param string[] $desiredClasses
|
||||
@ -371,7 +371,7 @@ final class PhpDocInfo
|
||||
{
|
||||
$this->hasChanged = \true;
|
||||
$node = $this->currentNodeProvider->getNode();
|
||||
if ($node !== null) {
|
||||
if ($node instanceof \PhpParser\Node) {
|
||||
$this->rectorChangeCollector->notifyNodeFileInfo($node);
|
||||
}
|
||||
}
|
||||
@ -434,7 +434,7 @@ final class PhpDocInfo
|
||||
*/
|
||||
private function getTypeOrMixed(?PhpDocTagValueNode $phpDocTagValueNode)
|
||||
{
|
||||
if ($phpDocTagValueNode === null) {
|
||||
if (!$phpDocTagValueNode instanceof PhpDocTagValueNode) {
|
||||
return new MixedType();
|
||||
}
|
||||
return $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType($phpDocTagValueNode, $this->node);
|
||||
|
@ -7,6 +7,7 @@ use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PHPStan\PhpDocParser\Ast\Node;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
||||
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
|
||||
@ -106,7 +107,7 @@ final class PhpDocTypeChanger
|
||||
// override existing type
|
||||
$newPHPStanPhpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($newType, TypeKind::PROPERTY);
|
||||
$currentVarTagValueNode = $phpDocInfo->getVarTagValueNode();
|
||||
if ($currentVarTagValueNode !== null) {
|
||||
if ($currentVarTagValueNode instanceof VarTagValueNode) {
|
||||
// only change type
|
||||
$currentVarTagValueNode->type = $newPHPStanPhpDocType;
|
||||
$phpDocInfo->markAsChanged();
|
||||
@ -132,7 +133,7 @@ final class PhpDocTypeChanger
|
||||
// override existing type
|
||||
$newPHPStanPhpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($newType, TypeKind::RETURN);
|
||||
$currentReturnTagValueNode = $phpDocInfo->getReturnTagValue();
|
||||
if ($currentReturnTagValueNode !== null) {
|
||||
if ($currentReturnTagValueNode instanceof ReturnTagValueNode) {
|
||||
// only change type
|
||||
$currentReturnTagValueNode->type = $newPHPStanPhpDocType;
|
||||
$phpDocInfo->markAsChanged();
|
||||
@ -155,7 +156,7 @@ final class PhpDocTypeChanger
|
||||
$phpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($newType, TypeKind::PARAM);
|
||||
$paramTagValueNode = $phpDocInfo->getParamTagValueByName($paramName);
|
||||
// override existing type
|
||||
if ($paramTagValueNode !== null) {
|
||||
if ($paramTagValueNode instanceof ParamTagValueNode) {
|
||||
// already set
|
||||
$currentType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($paramTagValueNode->type, $param);
|
||||
// avoid overriding better type
|
||||
|
@ -69,7 +69,7 @@ final class PlainValueParser
|
||||
$tokenIterator->next();
|
||||
// normalize value
|
||||
$constExprNode = $this->matchConstantValue($currentTokenValue);
|
||||
if ($constExprNode !== null) {
|
||||
if ($constExprNode instanceof ConstExprNode) {
|
||||
return $constExprNode;
|
||||
}
|
||||
while ($tokenIterator->isCurrentTokenType(Lexer::TOKEN_DOUBLE_COLON) || $tokenIterator->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER)) {
|
||||
|
@ -145,7 +145,7 @@ final class PhpDocInfoPrinter
|
||||
}
|
||||
private function getCurrentPhpDocInfo() : PhpDocInfo
|
||||
{
|
||||
if ($this->phpDocInfo === null) {
|
||||
if (!$this->phpDocInfo instanceof PhpDocInfo) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
return $this->phpDocInfo;
|
||||
|
@ -22,7 +22,7 @@ final class SpacingAwareTemplateTagValueNode extends TemplateTagValueNode
|
||||
{
|
||||
// @see https://github.com/rectorphp/rector/issues/3438
|
||||
# 'as'/'of'
|
||||
$bound = $this->bound !== null ? ' ' . $this->preposition . ' ' . $this->bound : '';
|
||||
$bound = $this->bound instanceof TypeNode ? ' ' . $this->preposition . ' ' . $this->bound : '';
|
||||
$content = $this->name . $bound . ' ' . $this->description;
|
||||
return \trim($content);
|
||||
}
|
||||
|
@ -119,10 +119,10 @@ final class ArrayCallableMethodMatcher
|
||||
}
|
||||
private function shouldSkipNullItems(Array_ $array) : bool
|
||||
{
|
||||
if ($array->items[0] === null) {
|
||||
if (!$array->items[0] instanceof ArrayItem) {
|
||||
return \true;
|
||||
}
|
||||
return $array->items[1] === null;
|
||||
return !$array->items[1] instanceof ArrayItem;
|
||||
}
|
||||
/**
|
||||
* @param mixed $values
|
||||
@ -182,7 +182,7 @@ final class ArrayCallableMethodMatcher
|
||||
$extendedMethodReflection = $classReflection->getMethod(MethodName::CONSTRUCT, $scope);
|
||||
$parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::selectSingle($extendedMethodReflection->getVariants());
|
||||
foreach ($parametersAcceptorWithPhpDocs->getParameters() as $parameterReflectionWithPhpDoc) {
|
||||
if ($parameterReflectionWithPhpDoc->getDefaultValue() === null) {
|
||||
if (!$parameterReflectionWithPhpDoc->getDefaultValue() instanceof Type) {
|
||||
return new MixedType();
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\NodeNameResolver\NodeNameResolver;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
|
||||
@ -37,7 +38,7 @@ final class ClassNameResolver implements NodeNameResolverInterface
|
||||
if ($node->namespacedName instanceof Name) {
|
||||
return $node->namespacedName->toString();
|
||||
}
|
||||
if ($node->name === null) {
|
||||
if (!$node->name instanceof Identifier) {
|
||||
return null;
|
||||
}
|
||||
return $this->nodeNameResolver->getName($node->name);
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\NodeNestingScope;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
@ -81,7 +82,7 @@ final class ContextAnalyzer
|
||||
}
|
||||
$nextNode = $node->getAttribute(AttributeKey::NEXT_NODE);
|
||||
if ($nextNode instanceof Node) {
|
||||
if ($nextNode instanceof Return_ && $nextNode->expr === null) {
|
||||
if ($nextNode instanceof Return_ && !$nextNode->expr instanceof Expr) {
|
||||
continue;
|
||||
}
|
||||
$hasAssign = (bool) $this->betterNodeFinder->findFirstInstanceOf($if->stmts, Assign::class);
|
||||
|
@ -61,7 +61,7 @@ final class ScopeNestingComparator
|
||||
$foundIfNode = (bool) $this->betterNodeFinder->findFirst($foundParentNode->stmts, function (Node $node) use($seekedExpr) : bool {
|
||||
return $this->nodeComparator->areNodesEqual($node, $seekedExpr);
|
||||
});
|
||||
if ($foundParentNode->else === null) {
|
||||
if (!$foundParentNode->else instanceof Else_) {
|
||||
return \false;
|
||||
}
|
||||
$foundElseNode = (bool) $this->betterNodeFinder->findFirst($foundParentNode->else, function (Node $node) use($seekedExpr) : bool {
|
||||
|
@ -177,7 +177,7 @@ final class NodeTypeResolver
|
||||
}
|
||||
}
|
||||
$type = $this->resolveByNodeTypeResolvers($node);
|
||||
if ($type !== null) {
|
||||
if ($type instanceof Type) {
|
||||
$type = $this->accessoryNonEmptyStringTypeCorrector->correct($type);
|
||||
$type = $this->genericClassStringTypeCorrector->correct($type);
|
||||
if ($type instanceof ObjectType) {
|
||||
@ -359,7 +359,7 @@ final class NodeTypeResolver
|
||||
*/
|
||||
private function resolveTernaryType(Ternary $ternary)
|
||||
{
|
||||
if ($ternary->if !== null) {
|
||||
if ($ternary->if instanceof Expr) {
|
||||
$first = $this->getType($ternary->if);
|
||||
$second = $this->getType($ternary->else);
|
||||
if ($this->isUnionTypeable($first, $second)) {
|
||||
|
@ -76,7 +76,7 @@ final class NewTypeResolver implements NodeTypeResolverInterface
|
||||
$directParentTypes = [];
|
||||
/** @var Class_ $class */
|
||||
$class = $new->class;
|
||||
if ($class->extends !== null) {
|
||||
if ($class->extends instanceof Name) {
|
||||
$parentClass = (string) $class->extends;
|
||||
$directParentTypes[] = new FullyQualifiedObjectType($parentClass);
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\NodeTypeResolver\NodeTypeResolver;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use PHPStan\Type\Type;
|
||||
use PHPStan\Type\VoidType;
|
||||
@ -38,7 +39,7 @@ final class ReturnTypeResolver implements NodeTypeResolverInterface
|
||||
*/
|
||||
public function resolve(Node $node) : Type
|
||||
{
|
||||
if ($node->expr === null) {
|
||||
if (!$node->expr instanceof Expr) {
|
||||
return new VoidType();
|
||||
}
|
||||
return $this->nodeTypeResolver->getType($node->expr);
|
||||
|
@ -15,6 +15,7 @@ use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\Ternary;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
@ -364,7 +365,7 @@ final class PHPStanNodeScopeResolver
|
||||
if ($classLike->namespacedName instanceof Name) {
|
||||
return (string) $classLike->namespacedName;
|
||||
}
|
||||
if ($classLike->name === null) {
|
||||
if (!$classLike->name instanceof Identifier) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
return $classLike->name->toString();
|
||||
|
@ -70,7 +70,7 @@ final class NameImportingPhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
|
||||
}
|
||||
public function beforeTraverse(Node $node) : void
|
||||
{
|
||||
if ($this->currentPhpParserNode === null) {
|
||||
if (!$this->currentPhpParserNode instanceof PhpParserNode) {
|
||||
throw new ShouldNotHappenException('Set "$currentPhpParserNode" first');
|
||||
}
|
||||
}
|
||||
@ -98,7 +98,7 @@ final class NameImportingPhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
|
||||
if (!$file instanceof File) {
|
||||
return null;
|
||||
}
|
||||
if ($this->currentPhpParserNode === null) {
|
||||
if (!$this->currentPhpParserNode instanceof PhpParserNode) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
return $this->processFqnNameImport($this->currentPhpParserNode, $node, $staticType, $file);
|
||||
@ -220,7 +220,7 @@ final class NameImportingPhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
|
||||
return null;
|
||||
}
|
||||
$importedName = $this->processFqnNameImport($currentPhpParserNode, $identifierTypeNode, $staticType, $file);
|
||||
if ($importedName !== null) {
|
||||
if ($importedName instanceof IdentifierTypeNode) {
|
||||
$spacelessPhpDocTagNode->name = '@' . $importedName->name;
|
||||
return $spacelessPhpDocTagNode;
|
||||
}
|
||||
|
@ -157,7 +157,7 @@ final class ArrayTypeAnalyzer
|
||||
}
|
||||
// A. local property
|
||||
$property = $classLike->getProperty($propertyName);
|
||||
if ($property !== null) {
|
||||
if ($property instanceof Property) {
|
||||
$propertyProperty = $property->props[0];
|
||||
return $propertyProperty->default instanceof Array_;
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ final class DoctrineTypeAnalyzer
|
||||
if (!$hasDoctrineCollectionType) {
|
||||
return \false;
|
||||
}
|
||||
return $arrayType !== null;
|
||||
return $arrayType instanceof ArrayType;
|
||||
}
|
||||
private function isCollectionObjectType(Type $type) : bool
|
||||
{
|
||||
|
@ -285,7 +285,7 @@ final class UnionTypeMapper implements TypeMapperInterface
|
||||
if ($phpParserUnionType instanceof NullableType) {
|
||||
return $phpParserUnionType;
|
||||
}
|
||||
if ($phpParserUnionType !== null) {
|
||||
if ($phpParserUnionType instanceof PhpParserUnionType) {
|
||||
return $this->narrowBoolType($unionType, $phpParserUnionType, $typeKind);
|
||||
}
|
||||
if ($this->boolUnionTypeAnalyzer->isBoolUnionType($unionType)) {
|
||||
|
@ -80,7 +80,7 @@ final class ArrayAnnotationToAttributeMapper implements AnnotationToAttributeMap
|
||||
}
|
||||
private function resolveValueExprWithSingleQuoteHandling(ArrayItem $arrayItem) : ArrayItem
|
||||
{
|
||||
if ($arrayItem->key === null && $arrayItem->value instanceof ClassConstFetch && $arrayItem->value->class instanceof Name && \strpos((string) $arrayItem->value->class, "'") !== \false) {
|
||||
if (!$arrayItem->key instanceof Expr && $arrayItem->value instanceof ClassConstFetch && $arrayItem->value->class instanceof Name && \strpos((string) $arrayItem->value->class, "'") !== \false) {
|
||||
$arrayItem->value = new String_($this->valueResolver->getValue($arrayItem->value));
|
||||
return $arrayItem;
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\PhpAttribute;
|
||||
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Array_;
|
||||
use PhpParser\Node\Expr\ArrayItem;
|
||||
use PhpParser\Node\Identifier;
|
||||
@ -61,7 +62,7 @@ final class AttributeArrayNameInliner
|
||||
} elseif ($arrayItem->key instanceof String_) {
|
||||
$arrayItemString = $arrayItem->key;
|
||||
$newArgs[] = new Arg($arrayItem->value, \false, \false, [], new Identifier($arrayItemString->value));
|
||||
} elseif ($arrayItem->key === null) {
|
||||
} elseif (!$arrayItem->key instanceof Expr) {
|
||||
// silent key
|
||||
$newArgs[] = new Arg($arrayItem->value);
|
||||
} else {
|
||||
|
@ -3,6 +3,7 @@
|
||||
declare (strict_types=1);
|
||||
namespace Rector\PhpAttribute;
|
||||
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Stmt\Use_;
|
||||
use PhpParser\Node\Stmt\UseUse;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
@ -26,7 +27,7 @@ final class UseAliasNameMatcher
|
||||
if (!$originalUseUse instanceof UseUse) {
|
||||
continue;
|
||||
}
|
||||
if ($originalUseUse->alias === null) {
|
||||
if (!$originalUseUse->alias instanceof Identifier) {
|
||||
continue;
|
||||
}
|
||||
$alias = $originalUseUse->alias->toString();
|
||||
|
@ -64,7 +64,7 @@ final class NodesToRemoveCollector implements NodeCollectorInterface
|
||||
{
|
||||
/** Node|null $parentNode */
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($parentNode !== null && $this->isUsedInArg($node, $parentNode)) {
|
||||
if ($parentNode instanceof Node && $this->isUsedInArg($node, $parentNode)) {
|
||||
return;
|
||||
}
|
||||
// chain call: "->method()->another()"
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\PostRector\Collector;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use Rector\Core\Provider\CurrentFileProvider;
|
||||
use Rector\Core\ValueObject\Application\File;
|
||||
@ -63,7 +64,7 @@ final class UseNodesToAddCollector implements NodeCollectorInterface
|
||||
foreach ($uses as $use) {
|
||||
$prefix = $this->useImportsResolver->resolvePrefix($use);
|
||||
foreach ($use->uses as $useUse) {
|
||||
if ($useUse->alias !== null) {
|
||||
if ($useUse->alias instanceof Identifier) {
|
||||
$objectTypes[] = new AliasedObjectType($useUse->alias->toString(), $prefix . $useUse->name);
|
||||
} else {
|
||||
$objectTypes[] = new FullyQualifiedObjectType($prefix . $useUse->name);
|
||||
|
@ -3,6 +3,7 @@
|
||||
declare (strict_types=1);
|
||||
namespace Rector\PostRector\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
||||
@ -62,7 +63,7 @@ final class NetteInjectDetector
|
||||
return \false;
|
||||
}
|
||||
// has no parent class
|
||||
if ($class->extends === null) {
|
||||
if (!$class->extends instanceof Name) {
|
||||
return \false;
|
||||
}
|
||||
$parentClass = $this->nodeNameResolver->getName($class->extends);
|
||||
|
@ -110,7 +110,7 @@ final class StaticTypeMapper
|
||||
if ($phpDocTagValueNode instanceof TemplateTagValueNode) {
|
||||
// special case
|
||||
$nameScope = $this->nameScopeFactory->createNameScopeFromNodeWithoutTemplateTypes($node);
|
||||
if ($phpDocTagValueNode->bound === null) {
|
||||
if (!$phpDocTagValueNode->bound instanceof TypeNode) {
|
||||
return new MixedType();
|
||||
}
|
||||
return $this->phpDocTypeMapper->mapToPHPStanType($phpDocTagValueNode->bound, $node, $nameScope);
|
||||
|
@ -47,7 +47,7 @@ abstract class AbstractTestCase extends TestCase
|
||||
*/
|
||||
protected function getService(string $type) : object
|
||||
{
|
||||
if (self::$currentContainer === null) {
|
||||
if (!self::$currentContainer instanceof ContainerInterface) {
|
||||
throw new ShouldNotHappenException('First, create container with "boot()" or "bootWithConfigFileInfos([...])"');
|
||||
}
|
||||
$object = self::$currentContainer->get($type);
|
||||
|
@ -58,7 +58,7 @@ final class ArgumentDefaultValueReplacer
|
||||
if ($value === ReplaceArgumentDefaultValue::ANY_VALUE_BEFORE) {
|
||||
return \true;
|
||||
}
|
||||
if ($expr === null) {
|
||||
if (!$expr instanceof Expr) {
|
||||
return \false;
|
||||
}
|
||||
if ($this->valueResolver->isValue($expr, $value)) {
|
||||
|
@ -3,6 +3,7 @@
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Arguments\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Param;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\Core\PhpParser\Node\Value\ValueResolver;
|
||||
@ -36,7 +37,7 @@ final class ChangedArgumentsDetector
|
||||
*/
|
||||
public function isDefaultValueChanged(Param $param, $value) : bool
|
||||
{
|
||||
if ($param->default === null) {
|
||||
if (!$param->default instanceof Expr) {
|
||||
return \false;
|
||||
}
|
||||
return !$this->valueResolver->isValue($param->default, $value);
|
||||
@ -46,7 +47,7 @@ final class ChangedArgumentsDetector
|
||||
if ($param->type === null) {
|
||||
return \false;
|
||||
}
|
||||
if ($newType === null) {
|
||||
if (!$newType instanceof Type) {
|
||||
return \true;
|
||||
}
|
||||
$currentParamType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);
|
||||
|
@ -251,7 +251,7 @@ CODE_SAMPLE
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
$param = new Param(new Variable($argumentName), BuilderHelpers::normalizeValue($defaultValue));
|
||||
if ($type !== null) {
|
||||
if ($type instanceof Type) {
|
||||
$param->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($type, TypeKind::PARAM);
|
||||
}
|
||||
$classMethod->params[$position] = $param;
|
||||
|
@ -120,7 +120,7 @@ final class ForAnalyzer
|
||||
return \false;
|
||||
}
|
||||
$arrayDimFetch = $node->var;
|
||||
if ($arrayDimFetch->dim === null) {
|
||||
if (!$arrayDimFetch->dim instanceof Expr) {
|
||||
return \false;
|
||||
}
|
||||
if (!$arrayDimFetch->dim instanceof Variable) {
|
||||
|
@ -77,7 +77,7 @@ final class ForeachAnalyzer
|
||||
if (!$onlyStatement->var instanceof ArrayDimFetch) {
|
||||
return null;
|
||||
}
|
||||
if ($onlyStatement->var->dim !== null) {
|
||||
if ($onlyStatement->var->dim instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->nodeComparator->areNodesEqual($foreach->valueVar, $onlyStatement->expr)) {
|
||||
|
@ -81,7 +81,7 @@ final class VariableDimFetchAssignResolver
|
||||
$alwaysNullKey = \true;
|
||||
$alwaysStringKey = \true;
|
||||
foreach ($keysAndExprs as $keyAndExpr) {
|
||||
if ($keyAndExpr->getKeyExpr() !== null) {
|
||||
if ($keyAndExpr->getKeyExpr() instanceof Expr) {
|
||||
$alwaysNullKey = \false;
|
||||
} else {
|
||||
$alwaysStringKey = \false;
|
||||
|
@ -12,7 +12,7 @@ final class ForeachFactory
|
||||
{
|
||||
public function createFromFor(For_ $for, string $iteratedVariableName, ?Expr $iteratedExpr, ?string $keyValueName) : Foreach_
|
||||
{
|
||||
if ($iteratedExpr === null) {
|
||||
if (!$iteratedExpr instanceof Expr) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
if ($keyValueName === null) {
|
||||
|
@ -91,7 +91,7 @@ CODE_SAMPLE
|
||||
{
|
||||
$standaloneAssignExpresssions = [];
|
||||
foreach ($expr->items as $key => $leftArrayItem) {
|
||||
if ($leftArrayItem === null) {
|
||||
if (!$leftArrayItem instanceof ArrayItem) {
|
||||
continue;
|
||||
}
|
||||
$rightArrayItem = $rightArray->items[$key];
|
||||
|
@ -21,16 +21,16 @@ final class JoinStringConcatRector extends AbstractRector
|
||||
* @var int
|
||||
*/
|
||||
private const LINE_BREAK_POINT = 100;
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $nodeReplacementIsRestricted = \false;
|
||||
/**
|
||||
* @var string
|
||||
* @see https://regex101.com/r/VaXM1t/1
|
||||
* @see https://stackoverflow.com/questions/4147646/determine-if-utf-8-text-is-all-ascii
|
||||
*/
|
||||
private const ASCII_REGEX = '#[^\\x00-\\x7F]#';
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $nodeReplacementIsRestricted = \false;
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Joins concat of 2 strings, unless the length is too long', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
|
@ -128,7 +128,7 @@ CODE_SAMPLE
|
||||
if (!$this->forAnalyzer->isLoopMatch($node->loop, $this->keyValueName)) {
|
||||
return null;
|
||||
}
|
||||
if ($this->iteratedExpr === null) {
|
||||
if (!$this->iteratedExpr instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if ($this->keyValueName === null) {
|
||||
|
@ -93,7 +93,7 @@ CODE_SAMPLE
|
||||
$returnToRemove = $node->getAttribute(AttributeKey::NEXT_NODE);
|
||||
/** @var Return_ $return */
|
||||
$return = $firstNodeInsideForeach->stmts[0];
|
||||
if ($returnToRemove->expr === null) {
|
||||
if (!$returnToRemove->expr instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->valueResolver->isTrueOrFalse($returnToRemove->expr)) {
|
||||
@ -114,7 +114,7 @@ CODE_SAMPLE
|
||||
}
|
||||
private function shouldSkipForeach(Foreach_ $foreach) : bool
|
||||
{
|
||||
if ($foreach->keyVar !== null) {
|
||||
if ($foreach->keyVar instanceof Expr) {
|
||||
return \true;
|
||||
}
|
||||
if (\count($foreach->stmts) > 1) {
|
||||
@ -163,7 +163,7 @@ CODE_SAMPLE
|
||||
if (!$ifStatment instanceof Return_) {
|
||||
return \false;
|
||||
}
|
||||
if ($ifStatment->expr === null) {
|
||||
if (!$ifStatment->expr instanceof Expr) {
|
||||
return \false;
|
||||
}
|
||||
return $this->valueResolver->isTrueOrFalse($ifStatment->expr);
|
||||
|
@ -9,6 +9,7 @@ use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Foreach_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
@ -96,7 +97,7 @@ CODE_SAMPLE
|
||||
$ifNode = $node->stmts[0];
|
||||
$condExpr = $ifNode->cond;
|
||||
$foreachKeyVar = $node->keyVar;
|
||||
if ($foreachKeyVar !== null && $this->shouldSkipForeachKeyUsage($ifNode, $foreachKeyVar)) {
|
||||
if ($foreachKeyVar instanceof Expr && $this->shouldSkipForeachKeyUsage($ifNode, $foreachKeyVar)) {
|
||||
return null;
|
||||
}
|
||||
if ($condExpr instanceof FuncCall) {
|
||||
@ -119,7 +120,7 @@ CODE_SAMPLE
|
||||
}
|
||||
/** @var If_ $ifNode */
|
||||
$ifNode = $foreach->stmts[0];
|
||||
if ($ifNode->else !== null) {
|
||||
if ($ifNode->else instanceof Else_) {
|
||||
return \true;
|
||||
}
|
||||
return $ifNode->elseifs !== [];
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\CodeQuality\Rector\Foreach_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\BinaryOp\Coalesce;
|
||||
@ -68,7 +69,7 @@ CODE_SAMPLE
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
$this->return = null;
|
||||
if ($node->keyVar === null) {
|
||||
if (!$node->keyVar instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
/** @var Return_|Assign|null $returnOrAssignNode */
|
||||
@ -137,8 +138,8 @@ CODE_SAMPLE
|
||||
$this->return = $nextNode;
|
||||
$this->removeNode($this->return);
|
||||
}
|
||||
$coalesce = new Coalesce(new ArrayDimFetch($foreach->expr, $checkedNode), $this->return instanceof Return_ && $this->return->expr !== null ? $this->return->expr : $checkedNode);
|
||||
if ($this->return !== null) {
|
||||
$coalesce = new Coalesce(new ArrayDimFetch($foreach->expr, $checkedNode), $this->return instanceof Return_ && $this->return->expr instanceof Expr ? $this->return->expr : $checkedNode);
|
||||
if ($this->return instanceof Return_) {
|
||||
return new Return_($coalesce);
|
||||
}
|
||||
return null;
|
||||
|
@ -68,7 +68,7 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if ($node->keyVar === null) {
|
||||
if (!$node->keyVar instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
// special case of nested array items
|
||||
|
@ -75,7 +75,7 @@ CODE_SAMPLE
|
||||
return null;
|
||||
}
|
||||
foreach ($nestedArrayItem->items as $nestedArrayItemItem) {
|
||||
if ($nestedArrayItemItem === null) {
|
||||
if (!$nestedArrayItemItem instanceof ArrayItem) {
|
||||
continue;
|
||||
}
|
||||
$array->items[] = new ArrayItem($nestedArrayItemItem->value, $nestedArrayItemItem->key);
|
||||
|
@ -4,11 +4,13 @@ declare (strict_types=1);
|
||||
namespace Rector\CodeQuality\Rector\FunctionLike;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PhpParser\NodeTraverser;
|
||||
@ -111,7 +113,7 @@ CODE_SAMPLE
|
||||
return null;
|
||||
}
|
||||
// there is some else
|
||||
if ($if->else !== null) {
|
||||
if ($if->else instanceof Else_) {
|
||||
return null;
|
||||
}
|
||||
// only property fetch, because of constructor set
|
||||
@ -146,11 +148,11 @@ CODE_SAMPLE
|
||||
$propertyType = $this->resolvePropertyTypeAfterConstructor($classLike, $propertyName);
|
||||
$resolvedTypes = [$propertyType];
|
||||
$defaultValue = $property->props[0]->default;
|
||||
if ($defaultValue !== null) {
|
||||
if ($defaultValue instanceof Expr) {
|
||||
$resolvedTypes[] = $this->getType($defaultValue);
|
||||
}
|
||||
$resolveAssignedType = $this->resolveAssignedTypeInStmtsByPropertyName($classLike->stmts, $propertyName);
|
||||
if ($resolveAssignedType !== null) {
|
||||
if ($resolveAssignedType instanceof Type) {
|
||||
$resolvedTypes[] = $resolveAssignedType;
|
||||
}
|
||||
return $this->typeFactory->createMixedPassedOrUnionTypeAndKeepConstant($resolvedTypes);
|
||||
@ -159,10 +161,10 @@ CODE_SAMPLE
|
||||
{
|
||||
$propertyTypeFromConstructor = null;
|
||||
$constructClassMethod = $class->getMethod(MethodName::CONSTRUCT);
|
||||
if ($constructClassMethod !== null) {
|
||||
if ($constructClassMethod instanceof ClassMethod) {
|
||||
$propertyTypeFromConstructor = $this->resolveAssignedTypeInStmtsByPropertyName((array) $constructClassMethod->stmts, $propertyName);
|
||||
}
|
||||
if ($propertyTypeFromConstructor !== null) {
|
||||
if ($propertyTypeFromConstructor instanceof Type) {
|
||||
return $propertyTypeFromConstructor;
|
||||
}
|
||||
// undefined property is null by default
|
||||
|
@ -3,6 +3,7 @@
|
||||
declare (strict_types=1);
|
||||
namespace Rector\CodeQuality\Rector\FunctionLike;
|
||||
|
||||
use PhpParser\Comment\Doc;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Array_;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
@ -199,7 +200,7 @@ CODE_SAMPLE
|
||||
if ($expression->getComments() !== []) {
|
||||
return \true;
|
||||
}
|
||||
return $expression->getDocComment() !== null;
|
||||
return $expression->getDocComment() instanceof Doc;
|
||||
}
|
||||
/**
|
||||
* @param Stmt[] $stmts
|
||||
|
@ -3,6 +3,7 @@
|
||||
declare (strict_types=1);
|
||||
namespace Rector\CodeQuality\Rector\FunctionLike;
|
||||
|
||||
use PhpParser\Comment\Doc;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\AssignOp;
|
||||
@ -161,7 +162,7 @@ CODE_SAMPLE
|
||||
if ($stmt->getComments() !== []) {
|
||||
return \true;
|
||||
}
|
||||
return $stmt->getDocComment() !== null;
|
||||
return $stmt->getDocComment() instanceof Doc;
|
||||
}
|
||||
private function isReturnWithVarAnnotation(Return_ $return) : bool
|
||||
{
|
||||
|
@ -5,23 +5,16 @@ namespace Rector\CodeQuality\Rector\Identical;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\BinaryOp\Identical;
|
||||
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
||||
use PhpParser\Node\Expr\BooleanNot;
|
||||
use PhpParser\Node\Expr\Instanceof_;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
||||
use PHPStan\Type\NullType;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\Type;
|
||||
use PHPStan\Type\TypeCombinator;
|
||||
use PHPStan\Type\UnionType;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
|
||||
use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
@ -30,28 +23,22 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
*/
|
||||
final class FlipTypeControlToUseExclusiveTypeRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover
|
||||
*/
|
||||
private $phpDocTagRemover;
|
||||
public function __construct(PhpDocTagRemover $phpDocTagRemover)
|
||||
{
|
||||
$this->phpDocTagRemover = $phpDocTagRemover;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Flip type control to use exclusive type', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
/** @var PhpDocInfo|null $phpDocInfo */
|
||||
$phpDocInfo = $functionLike->getAttribute(AttributeKey::PHP_DOC_INFO);
|
||||
if ($phpDocInfo === null) {
|
||||
return;
|
||||
return new RuleDefinition('Flip type control from null compare to use exclusive instanceof type', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
function process(?DateTime $dateTime)
|
||||
{
|
||||
if ($dateTime === null) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
$phpDocInfo = $functionLike->getAttribute(AttributeKey::PHP_DOC_INFO);
|
||||
if (! $phpDocInfo instanceof PhpDocInfo) {
|
||||
return;
|
||||
function process(?DateTime $dateTime)
|
||||
{
|
||||
if (! $dateTime instanceof DateTime) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
@ -61,10 +48,10 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [Identical::class];
|
||||
return [Identical::class, NotIdentical::class];
|
||||
}
|
||||
/**
|
||||
* @param Identical $node
|
||||
* @param Identical|NotIdentical $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
@ -72,29 +59,11 @@ CODE_SAMPLE
|
||||
if (!$expr instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
$assign = $this->getVariableAssign($node, $expr);
|
||||
if (!$assign instanceof Assign) {
|
||||
return null;
|
||||
}
|
||||
$bareType = $this->matchBareNullableType($expr);
|
||||
if (!$bareType instanceof Type) {
|
||||
return null;
|
||||
}
|
||||
$expression = $assign->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (!$expression instanceof Expression) {
|
||||
return null;
|
||||
}
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($expression);
|
||||
return $this->processConvertToExclusiveType($bareType, $expr, $phpDocInfo);
|
||||
}
|
||||
private function getVariableAssign(Identical $identical, Expr $expr) : ?Node
|
||||
{
|
||||
return $this->betterNodeFinder->findFirstPrevious($identical, function (Node $node) use($expr) : bool {
|
||||
if (!$node instanceof Assign) {
|
||||
return \false;
|
||||
}
|
||||
return $this->nodeComparator->areNodesEqual($node->var, $expr);
|
||||
});
|
||||
return $this->processConvertToExclusiveType($bareType, $expr, $node);
|
||||
}
|
||||
private function matchBareNullableType(Expr $expr) : ?Type
|
||||
{
|
||||
@ -110,26 +79,32 @@ CODE_SAMPLE
|
||||
}
|
||||
return TypeCombinator::removeNull($exprType);
|
||||
}
|
||||
private function processConvertToExclusiveType(Type $type, Expr $expr, PhpDocInfo $phpDocInfo) : ?BooleanNot
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\NotIdentical $binaryOp
|
||||
* @return \PhpParser\Node\Expr\BooleanNot|\PhpParser\Node\Expr\Instanceof_|null
|
||||
*/
|
||||
private function processConvertToExclusiveType(Type $type, Expr $expr, $binaryOp)
|
||||
{
|
||||
// $type = $types[0] instanceof NullType ? $types[1] : $types[0];
|
||||
if (!$type instanceof FullyQualifiedObjectType && !$type instanceof ObjectType) {
|
||||
if (!$type instanceof ObjectType) {
|
||||
return null;
|
||||
}
|
||||
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
|
||||
if ($varTagValueNode instanceof VarTagValueNode) {
|
||||
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $varTagValueNode);
|
||||
}
|
||||
$fullyQualifiedType = $type instanceof ShortenedObjectType ? $type->getFullyQualifiedName() : $type->getClassName();
|
||||
return new BooleanNot(new Instanceof_($expr, new FullyQualified($fullyQualifiedType)));
|
||||
}
|
||||
private function matchNullComparedExpr(Identical $identical) : ?Expr
|
||||
{
|
||||
if ($this->valueResolver->isNull($identical->left)) {
|
||||
return $identical->right;
|
||||
$instanceof = new Instanceof_($expr, new FullyQualified($fullyQualifiedType));
|
||||
if ($binaryOp instanceof NotIdentical) {
|
||||
return $instanceof;
|
||||
}
|
||||
if ($this->valueResolver->isNull($identical->right)) {
|
||||
return $identical->left;
|
||||
return new BooleanNot($instanceof);
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\NotIdentical $binaryOp
|
||||
*/
|
||||
private function matchNullComparedExpr($binaryOp) : ?Expr
|
||||
{
|
||||
if ($this->valueResolver->isNull($binaryOp->left)) {
|
||||
return $binaryOp->right;
|
||||
}
|
||||
if ($this->valueResolver->isNull($binaryOp->right)) {
|
||||
return $binaryOp->left;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ namespace Rector\CodeQuality\Rector\If_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
||||
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
|
||||
@ -81,7 +82,7 @@ CODE_SAMPLE
|
||||
}
|
||||
private function shouldSkip(If_ $if) : bool
|
||||
{
|
||||
if ($if->else !== null) {
|
||||
if ($if->else instanceof Else_) {
|
||||
return \true;
|
||||
}
|
||||
if (\count($if->stmts) !== 1) {
|
||||
@ -93,7 +94,7 @@ CODE_SAMPLE
|
||||
if (!$if->stmts[0] instanceof If_) {
|
||||
return \true;
|
||||
}
|
||||
if ($if->stmts[0]->else !== null) {
|
||||
if ($if->stmts[0]->else instanceof Else_) {
|
||||
return \true;
|
||||
}
|
||||
return (bool) $if->stmts[0]->elseifs;
|
||||
|
@ -81,10 +81,10 @@ CODE_SAMPLE
|
||||
{
|
||||
$this->reset();
|
||||
$currentNode = $node;
|
||||
while ($currentNode !== null) {
|
||||
while ($currentNode instanceof Node) {
|
||||
if ($currentNode instanceof If_) {
|
||||
$comparedNode = $this->ifManipulator->matchIfNotNullReturnValue($currentNode);
|
||||
if ($comparedNode !== null) {
|
||||
if ($comparedNode instanceof Expr) {
|
||||
$this->coalescingNodes[] = $comparedNode;
|
||||
$this->nodesToRemove[] = $currentNode;
|
||||
$currentNode = $currentNode->getAttribute(AttributeKey::NEXT_NODE);
|
||||
@ -119,7 +119,7 @@ CODE_SAMPLE
|
||||
if (!$node instanceof Return_) {
|
||||
return \false;
|
||||
}
|
||||
if ($node->expr === null) {
|
||||
if (!$node->expr instanceof Expr) {
|
||||
return \false;
|
||||
}
|
||||
return $this->valueResolver->isNull($node->expr);
|
||||
|
@ -95,7 +95,7 @@ CODE_SAMPLE
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
// skip short ternary
|
||||
if ($node instanceof Ternary && $node->if === null) {
|
||||
if ($node instanceof Ternary && !$node->if instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if ($node->cond instanceof BooleanNot) {
|
||||
|
@ -78,7 +78,7 @@ CODE_SAMPLE
|
||||
}
|
||||
// Try to shorten the nested if before transforming it to elseif
|
||||
$refactored = $this->shortenElseIf($if);
|
||||
if ($refactored !== null) {
|
||||
if ($refactored instanceof If_) {
|
||||
$if = $refactored;
|
||||
}
|
||||
if ($if->stmts === []) {
|
||||
|
@ -9,6 +9,7 @@ use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\Ternary;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
|
||||
@ -71,7 +72,7 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if ($node->else === null) {
|
||||
if (!$node->else instanceof Else_) {
|
||||
return null;
|
||||
}
|
||||
if ($node->elseifs !== []) {
|
||||
|
@ -62,7 +62,7 @@ CODE_SAMPLE
|
||||
if (!$nextNode instanceof Return_) {
|
||||
return null;
|
||||
}
|
||||
if ($nextNode->expr === null) {
|
||||
if (!$nextNode->expr instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->valueResolver->isNull($nextNode->expr)) {
|
||||
|
@ -8,6 +8,7 @@ use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\BinaryOp\Identical;
|
||||
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
||||
use PhpParser\Node\Expr\BooleanNot;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
|
||||
@ -135,7 +136,7 @@ CODE_SAMPLE
|
||||
}
|
||||
private function processReturnTrue(If_ $if, Return_ $nextReturnNode) : Return_
|
||||
{
|
||||
if ($if->cond instanceof BooleanNot && $nextReturnNode->expr !== null && $this->valueResolver->isTrue($nextReturnNode->expr)) {
|
||||
if ($if->cond instanceof BooleanNot && $nextReturnNode->expr instanceof Expr && $this->valueResolver->isTrue($nextReturnNode->expr)) {
|
||||
return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded($if->cond->expr));
|
||||
}
|
||||
return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded($if->cond));
|
||||
@ -146,7 +147,7 @@ CODE_SAMPLE
|
||||
$notIdentical = new NotIdentical($if->cond->left, $if->cond->right);
|
||||
return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded($notIdentical));
|
||||
}
|
||||
if ($nextReturnNode->expr === null) {
|
||||
if (!$nextReturnNode->expr instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->valueResolver->isTrue($nextReturnNode->expr)) {
|
||||
@ -162,7 +163,7 @@ CODE_SAMPLE
|
||||
*/
|
||||
private function isElseSeparatedThenIf(If_ $if) : bool
|
||||
{
|
||||
if ($if->else === null) {
|
||||
if (!$if->else instanceof Else_) {
|
||||
return \false;
|
||||
}
|
||||
if (\count($if->else->stmts) !== 1) {
|
||||
@ -184,6 +185,6 @@ CODE_SAMPLE
|
||||
return \false;
|
||||
}
|
||||
// return must have value
|
||||
return $ifInnerNode->expr !== null;
|
||||
return $ifInnerNode->expr instanceof Expr;
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\CodeQuality\Rector\Switch_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\BinaryOp\Identical;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\Switch_;
|
||||
@ -76,7 +77,7 @@ CODE_SAMPLE
|
||||
}
|
||||
$onlyCase = $node->cases[0];
|
||||
// only default → basically unwrap
|
||||
if ($onlyCase->cond === null) {
|
||||
if (!$onlyCase->cond instanceof Expr) {
|
||||
return $onlyCase->stmts;
|
||||
}
|
||||
$if = new If_(new Identical($node->cond, $onlyCase->cond));
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\CodeQuality\Rector\Ternary;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\BooleanNot;
|
||||
use PhpParser\Node\Expr\Ternary;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
@ -56,7 +57,7 @@ CODE_SAMPLE
|
||||
if (!$node->cond instanceof BooleanNot) {
|
||||
return null;
|
||||
}
|
||||
if ($node->if === null) {
|
||||
if (!$node->if instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
$node->cond = $node->cond->expr;
|
||||
|
@ -3,6 +3,7 @@
|
||||
declare (strict_types=1);
|
||||
namespace Rector\CodeQuality\TypeResolver;
|
||||
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PHPStan\Type\ArrayType;
|
||||
@ -29,7 +30,7 @@ final class ArrayDimFetchTypeResolver
|
||||
}
|
||||
private function resolveDimType(ArrayDimFetch $arrayDimFetch) : Type
|
||||
{
|
||||
if ($arrayDimFetch->dim !== null) {
|
||||
if ($arrayDimFetch->dim instanceof Expr) {
|
||||
return $this->nodeTypeResolver->getType($arrayDimFetch->dim);
|
||||
}
|
||||
return new MixedType();
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\Application;
|
||||
|
||||
use RectorPrefix202303\Nette\Utils\Strings;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Declare_;
|
||||
use PhpParser\Node\Stmt\Namespace_;
|
||||
@ -144,7 +145,7 @@ final class UseImportsAdder
|
||||
}
|
||||
private function getNamespaceName(Namespace_ $namespace) : ?string
|
||||
{
|
||||
if ($namespace->name === null) {
|
||||
if (!$namespace->name instanceof Name) {
|
||||
return null;
|
||||
}
|
||||
return $namespace->name->toString();
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\ClassNameImport;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Namespace_;
|
||||
use PhpParser\Node\Stmt\UseUse;
|
||||
@ -46,7 +47,7 @@ final class AliasUsesResolver
|
||||
{
|
||||
$aliasedUses = [];
|
||||
$this->useImportsTraverser->traverserStmts($stmts, static function (UseUse $useUse, string $name) use(&$aliasedUses) : void {
|
||||
if ($useUse->alias === null) {
|
||||
if (!$useUse->alias instanceof Identifier) {
|
||||
return;
|
||||
}
|
||||
$aliasedUses[] = $name;
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\ClassNameImport;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\Namespace_;
|
||||
@ -62,7 +63,7 @@ final class UsedImportsResolver
|
||||
$usedImports[] = new FullyQualifiedObjectType($className);
|
||||
}
|
||||
$this->useImportsTraverser->traverserStmts($stmts, static function (UseUse $useUse, string $name) use(&$usedImports) : void {
|
||||
if ($useUse->alias !== null) {
|
||||
if ($useUse->alias instanceof Identifier) {
|
||||
$usedImports[] = new AliasedObjectType($useUse->alias->toString(), $name);
|
||||
} else {
|
||||
$usedImports[] = new FullyQualifiedObjectType($name);
|
||||
|
@ -15,7 +15,7 @@ final class ClassNaming
|
||||
public function getShortName($name) : string
|
||||
{
|
||||
if ($name instanceof ClassLike) {
|
||||
if ($name->name === null) {
|
||||
if (!$name->name instanceof Identifier) {
|
||||
return '';
|
||||
}
|
||||
return $this->getShortName($name->name);
|
||||
|
@ -70,7 +70,7 @@ final class UseImportNameMatcher
|
||||
if (!$originalUseUse instanceof UseUse) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
if ($originalUseUse->alias === null) {
|
||||
if (!$originalUseUse->alias instanceof Identifier) {
|
||||
return $prefix . $originalUseUse->name->toString();
|
||||
}
|
||||
$unaliasedShortClass = Strings::substring($tag, Strings::length($originalUseUse->alias->toString()));
|
||||
|
@ -87,7 +87,7 @@ CODE_SAMPLE
|
||||
if (\count($node->types) !== 1) {
|
||||
return null;
|
||||
}
|
||||
if ($node->var === null) {
|
||||
if (!$node->var instanceof Variable) {
|
||||
return null;
|
||||
}
|
||||
$oldVariableName = $this->getName($node->var);
|
||||
|
@ -122,7 +122,7 @@ CODE_SAMPLE
|
||||
if (!$node instanceof PropertyProperty) {
|
||||
return null;
|
||||
}
|
||||
if ($node->default !== null) {
|
||||
if ($node->default instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
$varType = $this->resolveVarType($node);
|
||||
|
@ -74,11 +74,11 @@ CODE_SAMPLE
|
||||
return null;
|
||||
}
|
||||
$processIdentical = $this->processIdenticalOrNotIdentical($parentNode, $node, $expr);
|
||||
if ($processIdentical !== null) {
|
||||
if ($processIdentical instanceof Expr) {
|
||||
return $processIdentical;
|
||||
}
|
||||
$processGreaterOrSmaller = $this->processGreaterOrSmaller($parentNode, $node, $expr);
|
||||
if ($processGreaterOrSmaller !== null) {
|
||||
if ($processGreaterOrSmaller instanceof NotIdentical) {
|
||||
return $processGreaterOrSmaller;
|
||||
}
|
||||
return $this->processMarkTruthy($parentNode, $node, $expr);
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\Rector\Property;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
@ -65,7 +66,7 @@ CODE_SAMPLE
|
||||
return null;
|
||||
}
|
||||
$onlyProperty = $node->props[0];
|
||||
if ($onlyProperty->default !== null) {
|
||||
if ($onlyProperty->default instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->isBoolDocType($node)) {
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\Rector\Switch_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
|
||||
use PhpParser\Node\Expr\BinaryOp\Equal;
|
||||
use PhpParser\Node\Stmt\Case_;
|
||||
@ -76,7 +77,7 @@ CODE_SAMPLE
|
||||
$cases = $node->cases;
|
||||
/** @var Case_ $firstCase */
|
||||
$firstCase = \array_shift($cases);
|
||||
if ($firstCase->cond === null) {
|
||||
if (!$firstCase->cond instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if ($this->exprAnalyzer->isDynamicExpr($firstCase->cond)) {
|
||||
@ -85,7 +86,7 @@ CODE_SAMPLE
|
||||
$secondCase = \array_shift($cases);
|
||||
// special case with empty first case → ||
|
||||
$isFirstCaseEmpty = $firstCase->stmts === [];
|
||||
if ($isFirstCaseEmpty && $secondCase !== null && $secondCase->cond !== null) {
|
||||
if ($isFirstCaseEmpty && $secondCase instanceof Case_ && $secondCase->cond instanceof Expr) {
|
||||
$else = new BooleanOr(new Equal($node->cond, $firstCase->cond), new Equal($node->cond, $secondCase->cond));
|
||||
$ifNode = new If_($else);
|
||||
$ifNode->stmts = $this->switchManipulator->removeBreakNodes($secondCase->stmts);
|
||||
@ -97,7 +98,7 @@ CODE_SAMPLE
|
||||
if (!$secondCase instanceof Case_) {
|
||||
return $ifNode;
|
||||
}
|
||||
if ($secondCase->cond !== null) {
|
||||
if ($secondCase->cond instanceof Expr) {
|
||||
// has condition
|
||||
$equal = new Equal($node->cond, $secondCase->cond);
|
||||
$ifNode->elseifs[] = new ElseIf_($equal, $this->switchManipulator->removeBreakNodes($secondCase->stmts));
|
||||
|
@ -64,7 +64,7 @@ CODE_SAMPLE
|
||||
return null;
|
||||
}
|
||||
$previousNode = $node->getAttribute(AttributeKey::PREVIOUS_NODE);
|
||||
if ($previousNode !== null) {
|
||||
if ($previousNode instanceof Node) {
|
||||
return null;
|
||||
}
|
||||
$node->if = $nodeIf->expr;
|
||||
|
@ -12,6 +12,7 @@ use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use PHPStan\Reflection\ExtendedMethodReflection;
|
||||
use PHPStan\Reflection\ParametersAcceptorSelector;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\Core\Enum\ObjectReference;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
||||
@ -150,7 +151,7 @@ final class CurrentAndParentClassMethodComparator
|
||||
$parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::selectSingle($extendedMethodReflection->getVariants());
|
||||
foreach ($parametersAcceptorWithPhpDocs->getParameters() as $key => $parameterReflectionWithPhpDoc) {
|
||||
if (!isset($classMethod->params[$key])) {
|
||||
if ($parameterReflectionWithPhpDoc->getDefaultValue() !== null) {
|
||||
if ($parameterReflectionWithPhpDoc->getDefaultValue() instanceof Type) {
|
||||
continue;
|
||||
}
|
||||
return \true;
|
||||
|
@ -6,6 +6,7 @@ namespace Rector\DeadCode\Comparator\Parameter;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Param;
|
||||
use PHPStan\Reflection\ParameterReflection;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
||||
use Rector\DowngradePhp80\Reflection\DefaultParameterValueResolver;
|
||||
final class ParameterDefaultsComparator
|
||||
@ -27,7 +28,7 @@ final class ParameterDefaultsComparator
|
||||
}
|
||||
public function areDefaultValuesDifferent(ParameterReflection $parameterReflection, Param $param) : bool
|
||||
{
|
||||
if ($parameterReflection->getDefaultValue() === null && $param->default === null) {
|
||||
if (!$parameterReflection->getDefaultValue() instanceof Type && !$param->default instanceof Expr) {
|
||||
return \false;
|
||||
}
|
||||
if ($this->isMutuallyExclusiveNull($parameterReflection, $param)) {
|
||||
@ -40,12 +41,12 @@ final class ParameterDefaultsComparator
|
||||
}
|
||||
private function isMutuallyExclusiveNull(ParameterReflection $parameterReflection, Param $param) : bool
|
||||
{
|
||||
if ($parameterReflection->getDefaultValue() === null && $param->default !== null) {
|
||||
if (!$parameterReflection->getDefaultValue() instanceof Type && $param->default instanceof Expr) {
|
||||
return \true;
|
||||
}
|
||||
if ($parameterReflection->getDefaultValue() === null) {
|
||||
if (!$parameterReflection->getDefaultValue() instanceof Type) {
|
||||
return \false;
|
||||
}
|
||||
return $param->default === null;
|
||||
return !$param->default instanceof Expr;
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
declare (strict_types=1);
|
||||
namespace Rector\DeadCode\NodeManipulator;
|
||||
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
|
||||
@ -53,7 +54,7 @@ final class ControllerClassMethodManipulator
|
||||
}
|
||||
private function hasParentClassController(Class_ $class) : bool
|
||||
{
|
||||
if ($class->extends === null) {
|
||||
if (!$class->extends instanceof Name) {
|
||||
return \false;
|
||||
}
|
||||
return $this->nodeNameResolver->isName($class->extends, '#(Controller|Presenter)$#');
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\DeadCode\Rector\Array_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Array_;
|
||||
use PhpParser\Node\Expr\ArrayItem;
|
||||
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
|
||||
@ -73,7 +74,7 @@ CODE_SAMPLE
|
||||
if (!$arrayItem instanceof ArrayItem) {
|
||||
continue;
|
||||
}
|
||||
if ($arrayItem->key === null) {
|
||||
if (!$arrayItem->key instanceof Expr) {
|
||||
continue;
|
||||
}
|
||||
$keyValue = $this->nodePrinter->print($arrayItem->key);
|
||||
|
@ -6,6 +6,7 @@ namespace Rector\DeadCode\Rector\ClassMethod;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
@ -106,7 +107,7 @@ CODE_SAMPLE
|
||||
if (!$classLike instanceof Class_) {
|
||||
return \true;
|
||||
}
|
||||
return $classLike->extends === null;
|
||||
return !$classLike->extends instanceof Name;
|
||||
}
|
||||
private function isMethodReturnType(ClassMethod $classMethod, string $type) : bool
|
||||
{
|
||||
|
@ -7,6 +7,7 @@ use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Scalar;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\For_;
|
||||
use PhpParser\Node\Stmt\Foreach_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
@ -99,7 +100,7 @@ CODE_SAMPLE
|
||||
if ($if->elseifs !== []) {
|
||||
return;
|
||||
}
|
||||
if ($if->else !== null) {
|
||||
if ($if->else instanceof Else_) {
|
||||
$if->cond = $this->conditionInverter->createInvertedCondition($if->cond);
|
||||
$if->stmts = $if->else->stmts;
|
||||
$if->else = null;
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\DeadCode\Rector\Foreach_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Stmt\Foreach_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
@ -41,7 +42,7 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if ($node->keyVar === null) {
|
||||
if (!$node->keyVar instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
$keyVar = $node->keyVar;
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\DeadCode\Rector\FunctionLike;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
@ -71,7 +72,7 @@ CODE_SAMPLE
|
||||
if (!$lastStmt instanceof Return_) {
|
||||
return null;
|
||||
}
|
||||
if ($lastStmt->expr !== null) {
|
||||
if ($lastStmt->expr instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
$this->removeNode($lastStmt);
|
||||
|
@ -10,6 +10,7 @@ use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Expr\StaticPropertyFetch;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use PHPStan\Type\Constant\ConstantBooleanType;
|
||||
@ -79,7 +80,7 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function refactor(Node $node)
|
||||
{
|
||||
if ($node->else !== null) {
|
||||
if ($node->else instanceof Else_) {
|
||||
return null;
|
||||
}
|
||||
// just one if
|
||||
|
@ -65,7 +65,7 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function refactor(Node $node) : ?array
|
||||
{
|
||||
if ($node->else === null) {
|
||||
if (!$node->else instanceof Else_) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->isIfWithConstantReturns($node)) {
|
||||
|
@ -5,6 +5,7 @@ namespace Rector\DeadCode\Rector\If_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\DeadCode\ConditionEvaluator;
|
||||
@ -99,7 +100,7 @@ CODE_SAMPLE
|
||||
private function refactorIsNotMatch(If_ $if) : ?array
|
||||
{
|
||||
// no else → just remove the node
|
||||
if ($if->else === null) {
|
||||
if (!$if->else instanceof Else_) {
|
||||
$this->removeNode($if);
|
||||
return null;
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\DeadCode\Rector\Ternary;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
||||
use PhpParser\Node\Expr\Ternary;
|
||||
use PHPStan\Type\BooleanType;
|
||||
@ -59,7 +60,7 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if ($node->if === null) {
|
||||
if (!$node->if instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->valueResolver->isFalse($node->else)) {
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\EarlyReturn\Rector\If_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
@ -86,7 +87,7 @@ CODE_SAMPLE
|
||||
if (!$nextNode instanceof Return_) {
|
||||
return null;
|
||||
}
|
||||
if ($nextNode->expr === null) {
|
||||
if (!$nextNode->expr instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->ifManipulator->isIfAndElseWithSameVariableAssignAsLastStmts($node, $nextNode->expr)) {
|
||||
|
@ -8,6 +8,7 @@ use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
@ -79,7 +80,7 @@ CODE_SAMPLE
|
||||
if (!$returnVariable instanceof Variable) {
|
||||
continue;
|
||||
}
|
||||
if ($stmt instanceof If_ && $stmt->else === null && $stmt->elseifs === []) {
|
||||
if ($stmt instanceof If_ && !$stmt->else instanceof Else_ && $stmt->elseifs === []) {
|
||||
// is single condition if
|
||||
$if = $stmt;
|
||||
if (\count($if->stmts) !== 1) {
|
||||
|
@ -5,11 +5,13 @@ namespace Rector\Naming\Guard;
|
||||
|
||||
use DateTimeInterface;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\ArrowFunction;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\Foreach_;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
@ -181,7 +183,7 @@ final class BreakingVariableRenameGuard
|
||||
if ($this->nodeNameResolver->isName($previousForeach->valueVar, $currentName)) {
|
||||
return \true;
|
||||
}
|
||||
if ($previousForeach->keyVar === null) {
|
||||
if (!$previousForeach->keyVar instanceof Expr) {
|
||||
return \false;
|
||||
}
|
||||
if ($this->nodeNameResolver->isName($previousForeach->keyVar, $currentName)) {
|
||||
@ -197,7 +199,7 @@ final class BreakingVariableRenameGuard
|
||||
if ($previousIf instanceof If_) {
|
||||
$variableUses = [];
|
||||
$variableUses[] = $this->betterNodeFinder->findVariableOfName($previousIf->stmts, $currentVariableName);
|
||||
$previousStmts = $previousIf->else !== null ? $previousIf->else->stmts : [];
|
||||
$previousStmts = $previousIf->else instanceof Else_ ? $previousIf->else->stmts : [];
|
||||
$variableUses[] = $this->betterNodeFinder->findVariableOfName($previousStmts, $currentVariableName);
|
||||
$variableUses[] = $this->betterNodeFinder->findVariableOfName($previousIf->elseifs, $currentVariableName);
|
||||
$variableUses = \array_filter($variableUses);
|
||||
|
@ -66,7 +66,7 @@ final class VariableNaming
|
||||
}
|
||||
public function createCountedValueName(string $valueName, ?Scope $scope) : string
|
||||
{
|
||||
if ($scope === null) {
|
||||
if (!$scope instanceof Scope) {
|
||||
return $valueName;
|
||||
}
|
||||
// make sure variable name is unique
|
||||
|
@ -59,11 +59,11 @@ final class VariableRenamer
|
||||
public function renameVariableInFunctionLike($functionLike, string $oldName, string $expectedName, ?Assign $assign = null) : void
|
||||
{
|
||||
$isRenamingActive = \false;
|
||||
if ($assign === null) {
|
||||
if (!$assign instanceof Assign) {
|
||||
$isRenamingActive = \true;
|
||||
}
|
||||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $functionLike->getStmts(), function (Node $node) use($oldName, $expectedName, $assign, &$isRenamingActive) : ?Variable {
|
||||
if ($assign !== null && $node === $assign) {
|
||||
if ($assign instanceof Assign && $node === $assign) {
|
||||
$isRenamingActive = \true;
|
||||
return null;
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\Php52\Rector\Switch_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Scalar\LNumber;
|
||||
use PhpParser\Node\Stmt\Break_;
|
||||
@ -92,7 +93,7 @@ CODE_SAMPLE
|
||||
*/
|
||||
private function processContinueStatement(Continue_ $continue)
|
||||
{
|
||||
if ($continue->num === null) {
|
||||
if (!$continue->num instanceof Expr) {
|
||||
return new Break_();
|
||||
}
|
||||
if ($continue->num instanceof LNumber) {
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\Php54\Rector\Break_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Scalar\LNumber;
|
||||
use PhpParser\Node\Stmt\Break_;
|
||||
@ -72,7 +73,7 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if ($node->num === null) {
|
||||
if (!$node->num instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if ($node->num instanceof LNumber) {
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\Php70\Rector\Assign;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\ArrayItem;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
@ -56,7 +57,7 @@ final class ListSwapArrayOrderRector extends AbstractRector implements MinPhpVer
|
||||
if (!$arrayItem instanceof ArrayItem) {
|
||||
continue;
|
||||
}
|
||||
if ($arrayItem->value instanceof ArrayDimFetch && $arrayItem->value->dim === null) {
|
||||
if ($arrayItem->value instanceof ArrayDimFetch && !$arrayItem->value->dim instanceof Expr) {
|
||||
$printedVariables[] = $this->nodePrinter->print($arrayItem->value->var);
|
||||
} else {
|
||||
return null;
|
||||
|
@ -142,7 +142,7 @@ CODE_SAMPLE
|
||||
private function matchOnEqualFirstValueAndSecondValue(If_ $if) : void
|
||||
{
|
||||
$this->matchOnEqual($if);
|
||||
if ($if->else !== null) {
|
||||
if ($if->else instanceof Else_) {
|
||||
$this->processElse($if->else);
|
||||
} else {
|
||||
$nextNode = $if->getAttribute(AttributeKey::NEXT_NODE);
|
||||
@ -172,7 +172,7 @@ CODE_SAMPLE
|
||||
}
|
||||
$onlyIfStmt = $if->stmts[0];
|
||||
if ($onlyIfStmt instanceof Return_) {
|
||||
if ($onlyIfStmt->expr === null) {
|
||||
if (!$onlyIfStmt->expr instanceof Expr) {
|
||||
return;
|
||||
}
|
||||
// on Enum usage not in same file, it got object
|
||||
@ -203,7 +203,7 @@ CODE_SAMPLE
|
||||
if ($ternary->cond instanceof Smaller) {
|
||||
$this->firstValue = $ternary->cond->left;
|
||||
$this->secondValue = $ternary->cond->right;
|
||||
if ($ternary->if !== null) {
|
||||
if ($ternary->if instanceof Expr) {
|
||||
$this->onSmaller = $this->valueResolver->getValue($ternary->if);
|
||||
}
|
||||
$this->onGreater = $this->valueResolver->getValue($ternary->else);
|
||||
@ -211,7 +211,7 @@ CODE_SAMPLE
|
||||
} elseif ($ternary->cond instanceof Greater) {
|
||||
$this->firstValue = $ternary->cond->right;
|
||||
$this->secondValue = $ternary->cond->left;
|
||||
if ($ternary->if !== null) {
|
||||
if ($ternary->if instanceof Expr) {
|
||||
$this->onGreater = $this->valueResolver->getValue($ternary->if);
|
||||
}
|
||||
$this->onSmaller = $this->valueResolver->getValue($ternary->else);
|
||||
|
@ -45,7 +45,7 @@ CODE_SAMPLE
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
foreach ($node->items as $item) {
|
||||
if ($item !== null) {
|
||||
if ($item instanceof ArrayItem) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\Php70\Rector\Switch_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Stmt\Case_;
|
||||
use PhpParser\Node\Stmt\Switch_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
@ -60,7 +61,7 @@ CODE_SAMPLE
|
||||
{
|
||||
$defaultCases = [];
|
||||
foreach ($node->cases as $case) {
|
||||
if ($case->cond !== null) {
|
||||
if ($case->cond instanceof Expr) {
|
||||
continue;
|
||||
}
|
||||
$defaultCases[] = $case;
|
||||
|
@ -71,7 +71,7 @@ final class TernaryToNullCoalescingRector extends AbstractRector implements MinP
|
||||
}
|
||||
private function processTernaryWithIsset(Ternary $ternary, Isset_ $isset) : ?Coalesce
|
||||
{
|
||||
if ($ternary->if === null) {
|
||||
if (!$ternary->if instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if ($isset->vars === null) {
|
||||
|
@ -52,7 +52,7 @@ CODE_SAMPLE
|
||||
/** @var Ternary $nestedTernary */
|
||||
$nestedTernary = $node->else;
|
||||
$spaceshipNode = $this->processSmallerThanTernary($node, $nestedTernary);
|
||||
if ($spaceshipNode !== null) {
|
||||
if ($spaceshipNode instanceof Spaceship) {
|
||||
return $spaceshipNode;
|
||||
}
|
||||
return $this->processGreaterThanTernary($node, $nestedTernary);
|
||||
|
@ -77,7 +77,7 @@ CODE_SAMPLE
|
||||
continue;
|
||||
}
|
||||
// skip explicit assigns
|
||||
if ($parentNode->dim !== null) {
|
||||
if ($parentNode->dim instanceof Expr) {
|
||||
continue;
|
||||
}
|
||||
$shouldRetype = \true;
|
||||
|
@ -76,12 +76,12 @@ CODE_SAMPLE
|
||||
/** @var FuncCall $eachFuncCall */
|
||||
$eachFuncCall = $node->expr;
|
||||
// only key: list($key, ) = each($values);
|
||||
if ($listNode->items[0] instanceof ArrayItem && $listNode->items[1] === null) {
|
||||
if ($listNode->items[0] instanceof ArrayItem && !$listNode->items[1] instanceof ArrayItem) {
|
||||
$keyFuncCall = $this->nodeFactory->createFuncCall('key', $eachFuncCall->args);
|
||||
return new Assign($listNode->items[0]->value, $keyFuncCall);
|
||||
}
|
||||
// only value: list(, $value) = each($values);
|
||||
if ($listNode->items[1] instanceof ArrayItem && $listNode->items[0] === null) {
|
||||
if ($listNode->items[1] instanceof ArrayItem && !$listNode->items[0] instanceof ArrayItem) {
|
||||
$nextFuncCall = $this->nodeFactory->createFuncCall('next', $eachFuncCall->args);
|
||||
$this->nodesToAddCollector->addNodeAfterNode($nextFuncCall, $node);
|
||||
$currentFuncCall = $this->nodeFactory->createFuncCall('current', $eachFuncCall->args);
|
||||
@ -121,9 +121,9 @@ CODE_SAMPLE
|
||||
return \true;
|
||||
}
|
||||
// empty list → cannot handle
|
||||
if ($listNode->items[0] !== null) {
|
||||
if ($listNode->items[0] instanceof ArrayItem) {
|
||||
return \false;
|
||||
}
|
||||
return $listNode->items[1] === null;
|
||||
return !$listNode->items[1] instanceof ArrayItem;
|
||||
}
|
||||
}
|
||||
|
@ -106,7 +106,7 @@ CODE_SAMPLE
|
||||
if ($listNode->items !== []) {
|
||||
/** @var ArrayItem|null $keyItem */
|
||||
$keyItem = \array_pop($listNode->items);
|
||||
if ($keyItem !== null && !$isTrailingCommaLast) {
|
||||
if ($keyItem instanceof ArrayItem && !$isTrailingCommaLast) {
|
||||
$foreach->keyVar = $keyItem->value;
|
||||
}
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ final class ClosureArrowFunctionAnalyzer
|
||||
}
|
||||
/** @var Return_ $return */
|
||||
$return = $onlyStmt;
|
||||
if ($return->expr === null) {
|
||||
if (!$return->expr instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if ($this->shouldSkipForUsedReferencedValue($closure)) {
|
||||
|
@ -205,7 +205,7 @@ CODE_SAMPLE
|
||||
private function refactorReflectionFunctionGetReturnType(MethodCall $methodCall)
|
||||
{
|
||||
$refactoredMethodCall = $this->refactorIfHasReturnTypeWasCalled($methodCall);
|
||||
if ($refactoredMethodCall !== null) {
|
||||
if ($refactoredMethodCall instanceof Node) {
|
||||
return $refactoredMethodCall;
|
||||
}
|
||||
$getNameMethodCall = $this->nodeFactory->createMethodCall($methodCall, self::GET_NAME);
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\Php74\Rector\Property;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
@ -74,7 +75,7 @@ CODE_SAMPLE
|
||||
return \true;
|
||||
}
|
||||
$onlyProperty = $property->props[0];
|
||||
if ($onlyProperty->default !== null) {
|
||||
if ($onlyProperty->default instanceof Expr) {
|
||||
return \true;
|
||||
}
|
||||
if ($property->isReadonly()) {
|
||||
|
@ -16,7 +16,7 @@ final class JMSAccesorOrderAttributeDecorator implements AttributeDecoratorInter
|
||||
{
|
||||
// make first named arg explicit, @see https://github.com/rectorphp/rector/issues/7369
|
||||
$firstArg = $attribute->args[0];
|
||||
if ($firstArg->name !== null) {
|
||||
if ($firstArg->name instanceof Identifier) {
|
||||
return;
|
||||
}
|
||||
$firstArg->name = new Identifier('order');
|
||||
|
@ -23,7 +23,7 @@ final class JMSAccessTypeAttributeDecorator implements AttributeDecoratorInterfa
|
||||
return;
|
||||
}
|
||||
$currentArg = $args[0];
|
||||
if ($currentArg->name !== null) {
|
||||
if ($currentArg->name instanceof Identifier) {
|
||||
return;
|
||||
}
|
||||
if (!$currentArg->value instanceof String_) {
|
||||
|
@ -74,7 +74,7 @@ final class SwitchAnalyzer
|
||||
public function hasEachCaseSingleStmt(Switch_ $switch) : bool
|
||||
{
|
||||
foreach ($switch->cases as $case) {
|
||||
if ($case->cond === null) {
|
||||
if (!$case->cond instanceof Expr) {
|
||||
continue;
|
||||
}
|
||||
$stmtsWithoutBreak = \array_filter($case->stmts, static function (Node $node) : bool {
|
||||
@ -89,7 +89,7 @@ final class SwitchAnalyzer
|
||||
public function hasDefaultSingleStmt(Switch_ $switch) : bool
|
||||
{
|
||||
foreach ($switch->cases as $case) {
|
||||
if ($case->cond === null) {
|
||||
if (!$case->cond instanceof Expr) {
|
||||
$stmtsWithoutBreak = \array_filter($case->stmts, static function (Node $node) : bool {
|
||||
return !$node instanceof Break_;
|
||||
});
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user