mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-06 19:30:50 +00:00
[EarlyReturn] Register ChangeOrIfReturnToEarlyReturnRector to early-return set (#4975)
Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
parent
9433616c63
commit
ca0b4cfdc7
|
@ -6,6 +6,7 @@ use Rector\EarlyReturn\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRect
|
|||
use Rector\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector;
|
||||
use Rector\EarlyReturn\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector;
|
||||
use Rector\EarlyReturn\Rector\If_\ChangeNestedIfsToEarlyReturnRector;
|
||||
use Rector\EarlyReturn\Rector\If_\ChangeOrIfReturnToEarlyReturnRector;
|
||||
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
|
||||
use Rector\EarlyReturn\Rector\Return_\ReturnBinaryAndToEarlyReturnRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
@ -18,4 +19,5 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
$services->set(ChangeNestedIfsToEarlyReturnRector::class);
|
||||
$services->set(RemoveAlwaysElseRector::class);
|
||||
$services->set(ReturnBinaryAndToEarlyReturnRector::class);
|
||||
$services->set(ChangeOrIfReturnToEarlyReturnRector::class);
|
||||
};
|
||||
|
|
|
@ -182,8 +182,11 @@ final class NodeAnnotationReader
|
|||
|
||||
/** @var string|null $className */
|
||||
$className = $property->getAttribute(AttributeKey::CLASS_NAME);
|
||||
|
||||
if ($className === null || ! ClassExistenceStaticHelper::doesClassLikeExist($className)) {
|
||||
if ($className === null) {
|
||||
// probably fresh node
|
||||
return null;
|
||||
}
|
||||
if (! ClassExistenceStaticHelper::doesClassLikeExist($className)) {
|
||||
// probably fresh node
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -30,10 +30,12 @@ final class ArrayItemStaticHelper
|
|||
public static function resolveAnnotationItemsOrder(string $content, ?string $silentKey = null): array
|
||||
{
|
||||
// empty
|
||||
if ($content === '' || $content === '()') {
|
||||
if ($content === '') {
|
||||
return [];
|
||||
}
|
||||
if ($content === '()') {
|
||||
return [];
|
||||
}
|
||||
|
||||
$itemsOrder = [];
|
||||
|
||||
$matches = Strings::matchAll($content, self::ITEM_EQUALS_REGEX);
|
||||
|
|
|
@ -53,9 +53,11 @@ final class MovedFileWithNodesFactory
|
|||
): ?MovedFileWithNodes {
|
||||
/** @var Namespace_|null $currentNamespace */
|
||||
$currentNamespace = $this->betterNodeFinder->findFirstInstanceOf($nodes, Namespace_::class);
|
||||
|
||||
// file without namespace → skip
|
||||
if ($currentNamespace === null || $currentNamespace->name === null) {
|
||||
if ($currentNamespace === null) {
|
||||
return null;
|
||||
}
|
||||
if ($currentNamespace->name === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -53,7 +53,11 @@ final class ParsedClassConstFetchNodeCollector
|
|||
}
|
||||
|
||||
$constantName = $this->nodeNameResolver->getName($node->name);
|
||||
if ($constantName === 'class' || $constantName === null) {
|
||||
if ($constantName === 'class') {
|
||||
// this is not a manual constant
|
||||
return;
|
||||
}
|
||||
if ($constantName === null) {
|
||||
// this is not a manual constant
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -56,9 +56,11 @@ final class ParsedPropertyFetchNodeCollector
|
|||
}
|
||||
|
||||
$propertyType = $this->resolvePropertyCallerType($node);
|
||||
|
||||
// make sure name is valid
|
||||
if ($node->name instanceof StaticCall || $node->name instanceof MethodCall) {
|
||||
if ($node->name instanceof StaticCall) {
|
||||
return;
|
||||
}
|
||||
if ($node->name instanceof MethodCall) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -115,7 +115,7 @@ final class NodeNameResolver
|
|||
public function getName(Node $node): ?string
|
||||
{
|
||||
if ($node instanceof MethodCall || $node instanceof StaticCall) {
|
||||
if ($node->name instanceof MethodCall || $node->name instanceof StaticCall || $node->name instanceof Identifier) {
|
||||
if ($this->isCallOrIdentifier($node->name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -217,6 +217,18 @@ final class NodeNameResolver
|
|||
return $this->isNames($node, $names);
|
||||
}
|
||||
|
||||
private function isCallOrIdentifier(Node $node): bool
|
||||
{
|
||||
if ($node instanceof MethodCall) {
|
||||
return true;
|
||||
}
|
||||
if ($node instanceof StaticCall) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $node instanceof Identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MethodCall|StaticCall $node
|
||||
*/
|
||||
|
|
|
@ -36,8 +36,10 @@ final class ClassConstFetchNameResolver implements NodeNameResolverInterface
|
|||
{
|
||||
$class = $this->nodeNameResolver->getName($node->class);
|
||||
$name = $this->nodeNameResolver->getName($node->name);
|
||||
|
||||
if ($class === null || $name === null) {
|
||||
if ($class === null) {
|
||||
return null;
|
||||
}
|
||||
if ($name === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -169,14 +169,19 @@ final class NodeTypeResolver
|
|||
if ($node instanceof Arg) {
|
||||
$node = $node->value;
|
||||
}
|
||||
|
||||
if ($node instanceof Param || $node instanceof Scalar) {
|
||||
if ($node instanceof Param) {
|
||||
return $this->resolve($node);
|
||||
}
|
||||
if ($node instanceof Scalar) {
|
||||
return $this->resolve($node);
|
||||
}
|
||||
|
||||
/** @var Scope|null $nodeScope */
|
||||
$nodeScope = $node->getAttribute(AttributeKey::SCOPE);
|
||||
if (! $node instanceof Expr || $nodeScope === null) {
|
||||
if (! $node instanceof Expr) {
|
||||
return new MixedType();
|
||||
}
|
||||
if ($nodeScope === null) {
|
||||
return new MixedType();
|
||||
}
|
||||
|
||||
|
@ -354,7 +359,10 @@ final class NodeTypeResolver
|
|||
|
||||
/** @var Scope|null $nodeScope */
|
||||
$nodeScope = $node->getAttribute(AttributeKey::SCOPE);
|
||||
if ($nodeScope === null || ! $node instanceof Expr) {
|
||||
if ($nodeScope === null) {
|
||||
return new MixedType();
|
||||
}
|
||||
if (! $node instanceof Expr) {
|
||||
return new MixedType();
|
||||
}
|
||||
|
||||
|
|
|
@ -119,10 +119,12 @@ final class TypeComparator
|
|||
*/
|
||||
private function areArrayTypeWithSingleObjectChildToParent(Type $firstType, Type $secondType): bool
|
||||
{
|
||||
if (! $firstType instanceof ArrayType || ! $secondType instanceof ArrayType) {
|
||||
if (! $firstType instanceof ArrayType) {
|
||||
return false;
|
||||
}
|
||||
if (! $secondType instanceof ArrayType) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$firstArrayItemType = $firstType->getItemType();
|
||||
$secondArrayItemType = $secondType->getItemType();
|
||||
|
||||
|
|
|
@ -108,7 +108,10 @@ final class ArrayTypeAnalyzer
|
|||
|
||||
/** @var Class_|Trait_|Interface_|null $classLike */
|
||||
$classLike = $node->getAttribute(AttributeKey::CLASS_NODE);
|
||||
if ($classLike instanceof Interface_ || $classLike === null) {
|
||||
if ($classLike instanceof Interface_) {
|
||||
return false;
|
||||
}
|
||||
if ($classLike === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -102,10 +102,12 @@ final class NodesToAddCollector implements NodeCollectorInterface
|
|||
|
||||
private function resolveNearestExpressionPosition(Node $node): string
|
||||
{
|
||||
if ($node instanceof Expression || $node instanceof Stmt) {
|
||||
if ($node instanceof Expression) {
|
||||
return spl_object_hash($node);
|
||||
}
|
||||
if ($node instanceof Stmt) {
|
||||
return spl_object_hash($node);
|
||||
}
|
||||
|
||||
/** @var Expression|null $foundNode */
|
||||
$foundNode = $this->betterNodeFinder->findFirstAncestorInstanceOf($node, Expression::class);
|
||||
if ($foundNode === null) {
|
||||
|
|
|
@ -117,10 +117,12 @@ CODE_SAMPLE
|
|||
MethodCall $mainMethodCall,
|
||||
MethodCall $toBeRemovedMethodCall
|
||||
): bool {
|
||||
if (! $mainMethodCall instanceof MethodCall || ! $mainMethodCall->var instanceof MethodCall) {
|
||||
if (! $mainMethodCall instanceof MethodCall) {
|
||||
return false;
|
||||
}
|
||||
if (! $mainMethodCall->var instanceof MethodCall) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($toBeRemovedMethodCall !== $mainMethodCall->var) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -57,10 +57,12 @@ final class PropertyAddingPostRector extends AbstractPostRector
|
|||
|
||||
public function enterNode(Node $node): ?Node
|
||||
{
|
||||
if (! $node instanceof Class_ || $node->isAnonymous()) {
|
||||
if (! $node instanceof Class_) {
|
||||
return null;
|
||||
}
|
||||
if ($node->isAnonymous()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->addConstants($node);
|
||||
$this->addProperties($node);
|
||||
$this->addPropertiesWithoutConstructor($node);
|
||||
|
|
|
@ -53,7 +53,10 @@ final class IdentifierTypeMapper implements PhpDocTypeMapperInterface
|
|||
public function mapToPHPStanType(TypeNode $typeNode, Node $node, NameScope $nameScope): Type
|
||||
{
|
||||
$type = $this->scalarStringToTypeMapper->mapScalarStringToType($typeNode->name);
|
||||
if (! $type instanceof MixedType || $type->isExplicitMixed()) {
|
||||
if (! $type instanceof MixedType) {
|
||||
return $type;
|
||||
}
|
||||
if ($type->isExplicitMixed()) {
|
||||
return $type;
|
||||
}
|
||||
|
||||
|
|
|
@ -84,14 +84,18 @@ final class StaticTypeMapper
|
|||
|
||||
public function mapPHPStanPhpDocTypeToPHPStanType(PhpDocTagValueNode $phpDocTagValueNode, Node $node): Type
|
||||
{
|
||||
if ($phpDocTagValueNode instanceof ReturnTagValueNode ||
|
||||
$phpDocTagValueNode instanceof ParamTagValueNode ||
|
||||
$phpDocTagValueNode instanceof VarTagValueNode ||
|
||||
$phpDocTagValueNode instanceof ThrowsTagValueNode
|
||||
) {
|
||||
if ($phpDocTagValueNode instanceof ReturnTagValueNode) {
|
||||
return $this->mapPHPStanPhpDocTypeNodeToPHPStanType($phpDocTagValueNode->type, $node);
|
||||
}
|
||||
if ($phpDocTagValueNode instanceof ParamTagValueNode) {
|
||||
return $this->mapPHPStanPhpDocTypeNodeToPHPStanType($phpDocTagValueNode->type, $node);
|
||||
}
|
||||
if ($phpDocTagValueNode instanceof VarTagValueNode) {
|
||||
return $this->mapPHPStanPhpDocTypeNodeToPHPStanType($phpDocTagValueNode->type, $node);
|
||||
}
|
||||
if ($phpDocTagValueNode instanceof ThrowsTagValueNode) {
|
||||
return $this->mapPHPStanPhpDocTypeNodeToPHPStanType($phpDocTagValueNode->type, $node);
|
||||
}
|
||||
|
||||
throw new NotImplementedException(__METHOD__ . ' for ' . get_class($phpDocTagValueNode));
|
||||
}
|
||||
|
||||
|
|
|
@ -63,7 +63,10 @@ final class CakePHPFullyQualifiedClassNameResolver
|
|||
|
||||
// B. is Cake native class?
|
||||
$cakePhpVersion = 'Cake\\' . $pseudoNamespace . '\\' . $shortClass;
|
||||
if (class_exists($cakePhpVersion) || interface_exists($cakePhpVersion)) {
|
||||
if (class_exists($cakePhpVersion)) {
|
||||
return $cakePhpVersion;
|
||||
}
|
||||
if (interface_exists($cakePhpVersion)) {
|
||||
return $cakePhpVersion;
|
||||
}
|
||||
|
||||
|
|
|
@ -50,7 +50,10 @@ final class MethodCallToVariableNameResolver
|
|||
{
|
||||
$methodCallVarName = $this->nodeNameResolver->getName($methodCall->var);
|
||||
$methodCallName = $this->nodeNameResolver->getName($methodCall->name);
|
||||
if ($methodCallVarName === null || $methodCallName === null) {
|
||||
if ($methodCallVarName === null) {
|
||||
return null;
|
||||
}
|
||||
if ($methodCallName === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -91,8 +91,10 @@ CODE_SAMPLE
|
|||
if ($arrayCallable === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->isAssignedToNetteMagicOnProperty($node) || $this->isInsideProperty($node)) {
|
||||
if ($this->isAssignedToNetteMagicOnProperty($node)) {
|
||||
return null;
|
||||
}
|
||||
if ($this->isInsideProperty($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -170,7 +170,10 @@ CODE_SAMPLE
|
|||
private function refactorMethodCall(Param $param, MethodCall $methodCall): void
|
||||
{
|
||||
$paramName = $this->getName($param->var);
|
||||
if ($paramName === null || $this->shouldSkipMethodCallRefactor($paramName, $methodCall)) {
|
||||
if ($paramName === null) {
|
||||
return;
|
||||
}
|
||||
if ($this->shouldSkipMethodCallRefactor($paramName, $methodCall)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -72,8 +72,10 @@ CODE_SAMPLE
|
|||
if ($leftStaticType instanceof ObjectType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($leftStaticType instanceof MixedType || $rightStaticType instanceof MixedType) {
|
||||
if ($leftStaticType instanceof MixedType) {
|
||||
return null;
|
||||
}
|
||||
if ($rightStaticType instanceof MixedType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -107,8 +107,10 @@ CODE_SAMPLE
|
|||
|
||||
return new Variable($variableName);
|
||||
});
|
||||
|
||||
if ($countInCond === null || $variableName === null) {
|
||||
if ($countInCond === null) {
|
||||
return null;
|
||||
}
|
||||
if ($variableName === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -141,8 +141,10 @@ CODE_SAMPLE
|
|||
if (! $this->isLoopMatch((array) $node->loop)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->iteratedExpr === null || $this->keyValueName === null) {
|
||||
if ($this->iteratedExpr === null) {
|
||||
return null;
|
||||
}
|
||||
if ($this->keyValueName === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -251,8 +253,10 @@ CODE_SAMPLE
|
|||
if ($this->keyValueName === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($loopExprs[0] instanceof PreInc || $loopExprs[0] instanceof PostInc) {
|
||||
if ($loopExprs[0] instanceof PreInc) {
|
||||
return $this->isName($loopExprs[0]->var, $this->keyValueName);
|
||||
}
|
||||
if ($loopExprs[0] instanceof PostInc) {
|
||||
return $this->isName($loopExprs[0]->var, $this->keyValueName);
|
||||
}
|
||||
|
||||
|
|
|
@ -148,7 +148,10 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
$nextNode = $foreach->getAttribute(AttributeKey::NEXT_NODE);
|
||||
if ($nextNode === null || ! $nextNode instanceof Return_) {
|
||||
if ($nextNode === null) {
|
||||
return true;
|
||||
}
|
||||
if (! $nextNode instanceof Return_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -127,8 +127,10 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
$innerNode = $node->stmts[0] instanceof Expression ? $node->stmts[0]->expr : $node->stmts[0];
|
||||
|
||||
if ($innerNode instanceof Assign || $innerNode instanceof Return_) {
|
||||
if ($innerNode instanceof Assign) {
|
||||
return $innerNode;
|
||||
}
|
||||
if ($innerNode instanceof Return_) {
|
||||
return $innerNode;
|
||||
}
|
||||
|
||||
|
|
|
@ -100,9 +100,11 @@ CODE_SAMPLE
|
|||
|
||||
$varNode = $node->args[0]->value;
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
|
||||
// result of function or probably used
|
||||
if ($parentNode instanceof Expr || $parentNode instanceof Arg) {
|
||||
if ($parentNode instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if ($parentNode instanceof Arg) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -107,10 +107,12 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node->stmts === null || $node->stmts === []) {
|
||||
if ($node->stmts === null) {
|
||||
return null;
|
||||
}
|
||||
if ($node->stmts === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$haveNodeChanged = false;
|
||||
foreach ((array) $node->stmts as $key => $stmt) {
|
||||
if ($stmt instanceof Expression) {
|
||||
|
|
|
@ -82,8 +82,10 @@ CODE_SAMPLE
|
|||
|
||||
$ifAssignVar = $this->resolveOnlyStmtAssignVar($node->stmts);
|
||||
$elseAssignVar = $this->resolveOnlyStmtAssignVar($node->else->stmts);
|
||||
|
||||
if ($ifAssignVar === null || $elseAssignVar === null) {
|
||||
if ($ifAssignVar === null) {
|
||||
return null;
|
||||
}
|
||||
if ($elseAssignVar === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -93,7 +95,10 @@ CODE_SAMPLE
|
|||
|
||||
$ternaryIf = $this->resolveOnlyStmtAssignExpr($node->stmts);
|
||||
$ternaryElse = $this->resolveOnlyStmtAssignExpr($node->else->stmts);
|
||||
if ($ternaryIf === null || $ternaryElse === null) {
|
||||
if ($ternaryIf === null) {
|
||||
return null;
|
||||
}
|
||||
if ($ternaryElse === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -70,7 +70,10 @@ CODE_SAMPLE
|
|||
$insideIfNode = $node->stmts[0];
|
||||
|
||||
$nextNode = $node->getAttribute(AttributeKey::NEXT_NODE);
|
||||
if (! $nextNode instanceof Return_ || $nextNode->expr === null) {
|
||||
if (! $nextNode instanceof Return_) {
|
||||
return null;
|
||||
}
|
||||
if ($nextNode->expr === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -134,7 +134,10 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
$nextNode = $if->getAttribute(AttributeKey::NEXT_NODE);
|
||||
if (! $nextNode instanceof Return_ || $nextNode->expr === null) {
|
||||
if (! $nextNode instanceof Return_) {
|
||||
return true;
|
||||
}
|
||||
if ($nextNode->expr === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -62,10 +62,12 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $node->left instanceof Assign || ! $node->right instanceof Assign) {
|
||||
if (! $node->left instanceof Assign) {
|
||||
return null;
|
||||
}
|
||||
if (! $node->right instanceof Assign) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (! $parentNode instanceof Expression) {
|
||||
return null;
|
||||
|
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
namespace Rector\CodeQuality\Rector\Return_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\AssignOp;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
|
@ -80,8 +81,7 @@ CODE_SAMPLE
|
|||
$previousNode = $previousNode->expr;
|
||||
$previousVariableNode = $previousNode->var;
|
||||
|
||||
// has some comment
|
||||
if ($previousVariableNode->getComments() || $previousVariableNode->getDocComment()) {
|
||||
if ($this->hasSomeComment($previousVariableNode)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -116,7 +116,10 @@ CODE_SAMPLE
|
|||
$variableNode = $return->expr;
|
||||
|
||||
$previousExpression = $return->getAttribute(AttributeKey::PREVIOUS_NODE);
|
||||
if ($previousExpression === null || ! $previousExpression instanceof Expression) {
|
||||
if ($previousExpression === null) {
|
||||
return true;
|
||||
}
|
||||
if (! $previousExpression instanceof Expression) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -133,6 +136,15 @@ CODE_SAMPLE
|
|||
return $this->isPreviousExpressionVisuallySimilar($previousExpression, $previousNode);
|
||||
}
|
||||
|
||||
private function hasSomeComment(Expr $expr): bool
|
||||
{
|
||||
if ($expr->getComments() !== []) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $expr->getDocComment() !== null;
|
||||
}
|
||||
|
||||
private function isReturnWithVarAnnotation(Return_ $return): bool
|
||||
{
|
||||
$phpDocInfo = $return->getAttribute(AttributeKey::PHP_DOC_INFO);
|
||||
|
|
|
@ -68,8 +68,10 @@ CODE_SAMPLE
|
|||
if ($node->if === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isTrue($node->if) || ! $this->isFalse($node->else)) {
|
||||
if (! $this->isTrue($node->if)) {
|
||||
return null;
|
||||
}
|
||||
if (! $this->isFalse($node->else)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -72,8 +72,10 @@ final class UnnecessaryTernaryExpressionRector extends AbstractRector
|
|||
if (! $condition instanceof BinaryOp) {
|
||||
return $this->processNonBinaryCondition($ifExpression, $elseExpression, $condition);
|
||||
}
|
||||
|
||||
if ($this->isNull($ifExpression) || $this->isNull($elseExpression)) {
|
||||
if ($this->isNull($ifExpression)) {
|
||||
return null;
|
||||
}
|
||||
if ($this->isNull($elseExpression)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -102,7 +102,10 @@ CODE_SAMPLE
|
|||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$functionName = $this->getName($node);
|
||||
if (! $functionName || ! array_key_exists($functionName, $this->functionsToConstants)) {
|
||||
if (! $functionName) {
|
||||
return null;
|
||||
}
|
||||
if (! array_key_exists($functionName, $this->functionsToConstants)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -57,15 +57,19 @@ CODE_SAMPLE
|
|||
{
|
||||
$nodeIf = $node->if;
|
||||
$nodeElse = $node->else;
|
||||
|
||||
if (! $nodeIf instanceof Assign || ! $nodeElse instanceof Assign) {
|
||||
if (! $nodeIf instanceof Assign) {
|
||||
return null;
|
||||
}
|
||||
if (! $nodeElse instanceof Assign) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$nodeIfVar = $nodeIf->var;
|
||||
$nodeElseVar = $nodeElse->var;
|
||||
|
||||
if (! $nodeIfVar instanceof Variable || ! $nodeElseVar instanceof Variable) {
|
||||
if (! $nodeIfVar instanceof Variable) {
|
||||
return null;
|
||||
}
|
||||
if (! $nodeElseVar instanceof Variable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -272,8 +272,10 @@ CODE_SAMPLE
|
|||
{
|
||||
$fullyQualified = $this->classResolver->getClassFromMethodCall($methodCall);
|
||||
$methodName = $methodCall->name;
|
||||
|
||||
if (! $fullyQualified instanceof FullyQualified || ! $methodName instanceof Identifier) {
|
||||
if (! $fullyQualified instanceof FullyQualified) {
|
||||
return [];
|
||||
}
|
||||
if (! $methodName instanceof Identifier) {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
|
|
@ -61,8 +61,13 @@ final class LivingCodeManipulator
|
|||
if (! $expr instanceof Expr) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($expr instanceof Closure || $expr instanceof Scalar || $expr instanceof ConstFetch) {
|
||||
if ($expr instanceof Closure) {
|
||||
return [];
|
||||
}
|
||||
if ($expr instanceof Scalar) {
|
||||
return [];
|
||||
}
|
||||
if ($expr instanceof ConstFetch) {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
@ -87,8 +92,13 @@ final class LivingCodeManipulator
|
|||
$this->keepLivingCodeFromExpr($expr->dim)
|
||||
);
|
||||
}
|
||||
|
||||
if ($expr instanceof ClassConstFetch || $expr instanceof StaticPropertyFetch) {
|
||||
if ($expr instanceof ClassConstFetch) {
|
||||
return array_merge(
|
||||
$this->keepLivingCodeFromExpr($expr->class),
|
||||
$this->keepLivingCodeFromExpr($expr->name)
|
||||
);
|
||||
}
|
||||
if ($expr instanceof StaticPropertyFetch) {
|
||||
return array_merge(
|
||||
$this->keepLivingCodeFromExpr($expr->class),
|
||||
$this->keepLivingCodeFromExpr($expr->name)
|
||||
|
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
namespace Rector\DeadCode\Rector\Assign;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
|
@ -78,7 +79,7 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
if ($node->expr instanceof FuncCall || $node->expr instanceof StaticCall || $node->expr instanceof MethodCall) {
|
||||
if ($this->isCall($node->expr)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -96,6 +97,19 @@ CODE_SAMPLE
|
|||
return $node;
|
||||
}
|
||||
|
||||
private function isCall(Expr $expr): bool
|
||||
{
|
||||
if ($expr instanceof FuncCall) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($expr instanceof StaticCall) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $expr instanceof MethodCall;
|
||||
}
|
||||
|
||||
private function shouldSkipForDifferentScope(Assign $assign, Expression $expression): bool
|
||||
{
|
||||
if (! $this->areInSameClassMethod($assign, $expression)) {
|
||||
|
|
|
@ -83,8 +83,10 @@ CODE_SAMPLE
|
|||
if (! $this->isName($classMethod, MethodName::CONSTRUCT)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($classMethod->stmts === null || $classMethod->stmts !== []) {
|
||||
if ($classMethod->stmts === null) {
|
||||
return true;
|
||||
}
|
||||
if ($classMethod->stmts !== []) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -77,8 +77,10 @@ CODE_SAMPLE
|
|||
if ($this->shouldSkipClass($classLike)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node->stmts === null || count((array) $node->stmts) !== 1) {
|
||||
if ($node->stmts === null) {
|
||||
return null;
|
||||
}
|
||||
if (count((array) $node->stmts) !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -85,9 +85,11 @@ CODE_SAMPLE
|
|||
if ($classLike === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// unreliable to detect trait, interface doesn't make sense
|
||||
if ($classLike instanceof Trait_ || $classLike instanceof Interface_) {
|
||||
if ($classLike instanceof Trait_) {
|
||||
return true;
|
||||
}
|
||||
if ($classLike instanceof Interface_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -71,10 +71,12 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node->stmts === [] || $node->stmts === null) {
|
||||
if ($node->stmts === []) {
|
||||
return null;
|
||||
}
|
||||
if ($node->stmts === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$stmtValues = array_values($node->stmts);
|
||||
$lastStmt = end($stmtValues);
|
||||
if (! $lastStmt instanceof Return_) {
|
||||
|
|
|
@ -139,15 +139,19 @@ CODE_SAMPLE
|
|||
|
||||
private function processBinaryOp(Node $node): ?Expr
|
||||
{
|
||||
if ($node instanceof Plus || $node instanceof Minus) {
|
||||
if ($node instanceof Plus) {
|
||||
return $this->processBinaryPlusAndMinus($node);
|
||||
}
|
||||
if ($node instanceof Minus) {
|
||||
return $this->processBinaryPlusAndMinus($node);
|
||||
}
|
||||
|
||||
// *, /
|
||||
if ($node instanceof Mul || $node instanceof Div) {
|
||||
if ($node instanceof Mul) {
|
||||
return $this->processBinaryMulAndDiv($node);
|
||||
}
|
||||
if ($node instanceof Div) {
|
||||
return $this->processBinaryMulAndDiv($node);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
namespace Rector\Defluent\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
|
@ -57,10 +58,9 @@ final class FluentChainMethodCallNodeAnalyzer
|
|||
*/
|
||||
public function isFluentClassMethodOfMethodCall(MethodCall $methodCall): bool
|
||||
{
|
||||
if ($methodCall->var instanceof MethodCall || $methodCall->var instanceof StaticCall) {
|
||||
if ($this->isCall($methodCall->var)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$calleeStaticType = $this->nodeTypeResolver->getStaticType($methodCall->var);
|
||||
|
||||
// we're not sure
|
||||
|
@ -217,4 +217,13 @@ final class FluentChainMethodCallNodeAnalyzer
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function isCall(Expr $expr): bool
|
||||
{
|
||||
if ($expr instanceof MethodCall) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $expr instanceof StaticCall;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -84,10 +84,12 @@ final class FluentChainMethodCallRootExtractor
|
|||
}
|
||||
|
||||
foreach ($methodCalls as $methodCall) {
|
||||
if ($methodCall->var instanceof Variable || $methodCall->var instanceof PropertyFetch) {
|
||||
if ($methodCall->var instanceof Variable) {
|
||||
return $this->createAssignAndRootExprForVariableOrPropertyFetch($methodCall);
|
||||
}
|
||||
if ($methodCall->var instanceof PropertyFetch) {
|
||||
return $this->createAssignAndRootExprForVariableOrPropertyFetch($methodCall);
|
||||
}
|
||||
|
||||
if ($methodCall->var instanceof New_) {
|
||||
// direct = no parent
|
||||
if ($kind === FluentCallsKind::IN_ARGS) {
|
||||
|
|
|
@ -34,10 +34,12 @@ abstract class AbstractDowngradeParamDeclarationRector extends AbstractRector im
|
|||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node->params === null || $node->params === []) {
|
||||
if ($node->params === null) {
|
||||
return null;
|
||||
}
|
||||
if ($node->params === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($node->params as $param) {
|
||||
$this->refactorParam($param, $node);
|
||||
}
|
||||
|
|
|
@ -71,7 +71,10 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
$parentOfNextNode = $nextNode->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (! $parentOfNextNode instanceof ArrayDimFetch || ! $this->areNodesEqual($parentOfNextNode->dim, $nextNode)) {
|
||||
if (! $parentOfNextNode instanceof ArrayDimFetch) {
|
||||
return null;
|
||||
}
|
||||
if (! $this->areNodesEqual($parentOfNextNode->dim, $nextNode)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -103,10 +103,12 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node->params === null || $node->params === []) {
|
||||
if ($node->params === null) {
|
||||
return null;
|
||||
}
|
||||
if ($node->params === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($node->params as $position => $param) {
|
||||
$this->refactorParamForAncestorsAndSiblings($param, $node, (int) $position);
|
||||
}
|
||||
|
@ -193,6 +195,7 @@ CODE_SAMPLE
|
|||
$refactorableAncestorClassNames
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain the list of the implemented interfaces with a different signature
|
||||
* @return Interface_[]
|
||||
|
@ -289,6 +292,7 @@ CODE_SAMPLE
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the current param type in the PHPDoc
|
||||
*/
|
||||
|
|
|
@ -131,7 +131,10 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
$nodeReturnType = $classMethod->returnType;
|
||||
if ($nodeReturnType === null || $nodeReturnType instanceof UnionType) {
|
||||
if ($nodeReturnType === null) {
|
||||
return null;
|
||||
}
|
||||
if ($nodeReturnType instanceof UnionType) {
|
||||
return null;
|
||||
}
|
||||
$nodeReturnTypeName = $this->getName(
|
||||
|
|
|
@ -90,10 +90,18 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node instanceof MethodCall || $node instanceof FuncCall || $node instanceof StaticCall || $node instanceof New_) {
|
||||
if ($node instanceof MethodCall) {
|
||||
return $this->processArgs($node);
|
||||
}
|
||||
if ($node instanceof FuncCall) {
|
||||
return $this->processArgs($node);
|
||||
}
|
||||
if ($node instanceof StaticCall) {
|
||||
return $this->processArgs($node);
|
||||
}
|
||||
if ($node instanceof New_) {
|
||||
return $this->processArgs($node);
|
||||
}
|
||||
|
||||
if ($node instanceof Closure) {
|
||||
$node = $this->processUses($node);
|
||||
}
|
||||
|
|
|
@ -27,7 +27,10 @@ final class ConditionInverter
|
|||
// inverse condition
|
||||
if ($expr instanceof BinaryOp) {
|
||||
$inversedCondition = $this->binaryOpManipulator->invertCondition($expr);
|
||||
if ($inversedCondition === null || $inversedCondition instanceof BooleanAnd) {
|
||||
if ($inversedCondition === null) {
|
||||
return new BooleanNot($expr);
|
||||
}
|
||||
if ($inversedCondition instanceof BooleanAnd) {
|
||||
return new BooleanNot($expr);
|
||||
}
|
||||
return $inversedCondition;
|
||||
|
|
|
@ -110,9 +110,11 @@ CODE_SAMPLE
|
|||
$this->removeByName($node, $argumentRemover->getPosition(), $match['name']);
|
||||
return;
|
||||
}
|
||||
|
||||
// only argument specific value can be removed
|
||||
if ($node instanceof ClassMethod || ! isset($node->args[$argumentRemover->getPosition()])) {
|
||||
if ($node instanceof ClassMethod) {
|
||||
return;
|
||||
}
|
||||
if (! isset($node->args[$argumentRemover->getPosition()])) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -80,10 +80,12 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node->extends === null || $node->isAnonymous()) {
|
||||
if ($node->extends === null) {
|
||||
return null;
|
||||
}
|
||||
if ($node->isAnonymous()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$nodeParentClassName = $this->getName($node->extends);
|
||||
if (! isset($this->parentClassToTraits[$nodeParentClassName])) {
|
||||
return null;
|
||||
|
|
|
@ -102,10 +102,12 @@ CODE_SAMPLE
|
|||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node instanceof Assign) {
|
||||
if ($node->var instanceof PropertyFetch || $node->var instanceof StaticPropertyFetch) {
|
||||
if ($node->var instanceof PropertyFetch) {
|
||||
return $this->processMagicSet($node);
|
||||
}
|
||||
if ($node->var instanceof StaticPropertyFetch) {
|
||||
return $this->processMagicSet($node);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -153,7 +153,10 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
$expectedName = $this->expectedNameResolver->resolveForCall($call);
|
||||
if ($expectedName === null || $this->isName($node->var, $expectedName)) {
|
||||
if ($expectedName === null) {
|
||||
return null;
|
||||
}
|
||||
if ($this->isName($node->var, $expectedName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -119,7 +119,10 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
$expectedName = $this->expectedNameResolver->resolveForForeach($variableAndCallAssign->getCall());
|
||||
if ($expectedName === null || $this->isName($variableAndCallAssign->getVariable(), $expectedName)) {
|
||||
if ($expectedName === null) {
|
||||
return null;
|
||||
}
|
||||
if ($this->isName($variableAndCallAssign->getVariable(), $expectedName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -92,7 +92,7 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
$camelCaseName = StaticRectorStrings::underscoreToCamelCase($nodeName);
|
||||
if ($camelCaseName === 'this' || $camelCaseName === '' || is_numeric($camelCaseName[0])) {
|
||||
if ($this->isReserved($camelCaseName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -115,6 +115,19 @@ CODE_SAMPLE
|
|||
return $node;
|
||||
}
|
||||
|
||||
private function isReserved(string $string): bool
|
||||
{
|
||||
if ($string === 'this') {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($string === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return is_numeric($string[0]);
|
||||
}
|
||||
|
||||
private function isFoundInParentNode(Variable $variable): bool
|
||||
{
|
||||
/** @var ClassMethod|Function_|null $classMethodOrFunction */
|
||||
|
|
|
@ -109,7 +109,13 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
$camelCaseName = StaticRectorStrings::underscoreToCamelCase($nodeName);
|
||||
if ($camelCaseName === 'this' || $camelCaseName === '' || is_numeric($camelCaseName[0])) {
|
||||
if ($camelCaseName === 'this') {
|
||||
return null;
|
||||
}
|
||||
if ($camelCaseName === '') {
|
||||
return null;
|
||||
}
|
||||
if (is_numeric($camelCaseName[0])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -141,7 +141,10 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
$parent = $arrayDimFetch->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($parent instanceof Isset_ || $parent instanceof Unset_) {
|
||||
if ($parent instanceof Isset_) {
|
||||
return ! $arrayDimFetch->dim instanceof Variable;
|
||||
}
|
||||
if ($parent instanceof Unset_) {
|
||||
return ! $arrayDimFetch->dim instanceof Variable;
|
||||
}
|
||||
|
||||
|
|
|
@ -146,7 +146,7 @@ final class VariableNaming
|
|||
return $this->resolveFromPropertyFetch($node);
|
||||
}
|
||||
|
||||
if ($node instanceof MethodCall || $node instanceof NullsafeMethodCall || $node instanceof StaticCall) {
|
||||
if ($this->isCall($node)) {
|
||||
return $this->resolveFromMethodCall($node);
|
||||
}
|
||||
|
||||
|
@ -244,16 +244,34 @@ final class VariableNaming
|
|||
return $varName . ucfirst($propertyName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MethodCall|NullsafeMethodCall|StaticCall $expr
|
||||
*/
|
||||
private function resolveFromMethodCall(Expr $expr): ?string
|
||||
private function isCall(?Node $node): bool
|
||||
{
|
||||
if ($expr->name instanceof MethodCall) {
|
||||
return $this->resolveFromMethodCall($expr->name);
|
||||
if ($node instanceof MethodCall) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$methodName = $this->nodeNameResolver->getName($expr->name);
|
||||
if ($node instanceof NullsafeMethodCall) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $node instanceof StaticCall;
|
||||
}
|
||||
|
||||
private function resolveFromMethodCall(?Node $node): ?string
|
||||
{
|
||||
if ($node === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! property_exists($node, 'name')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node->name instanceof MethodCall) {
|
||||
return $this->resolveFromMethodCall($node->name);
|
||||
}
|
||||
|
||||
$methodName = $this->nodeNameResolver->getName($node->name);
|
||||
if (! is_string($methodName)) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -135,10 +135,12 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node->stmts === null || $node->stmts === []) {
|
||||
if ($node->stmts === null) {
|
||||
return null;
|
||||
}
|
||||
if ($node->stmts === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$inferedReturnType = $this->returnTypeInferer->inferFunctionLike($node);
|
||||
|
||||
$routeListObjectType = new ObjectType(RouteList::class);
|
||||
|
|
|
@ -45,19 +45,20 @@ final class RouteInfoFactory
|
|||
public function createFromNode(Node $node): ?RouteInfo
|
||||
{
|
||||
if ($node instanceof New_) {
|
||||
if (! isset($node->args[0]) || ! isset($node->args[1])) {
|
||||
if ($this->hasNoArg($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->createRouteInfoFromArgs($node);
|
||||
}
|
||||
|
||||
// Route::create()
|
||||
if ($node instanceof StaticCall) {
|
||||
if (! isset($node->args[0]) || ! isset($node->args[1])) {
|
||||
if (! isset($node->args[0])) {
|
||||
return null;
|
||||
}
|
||||
if (! isset($node->args[1])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->nodeNameResolver->isNames($node->name, ['get', 'head', 'post', 'put', 'patch', 'delete'])) {
|
||||
return null;
|
||||
}
|
||||
|
@ -77,6 +78,15 @@ final class RouteInfoFactory
|
|||
return null;
|
||||
}
|
||||
|
||||
private function hasNoArg(New_ $new): bool
|
||||
{
|
||||
if (! isset($new->args[0])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return ! isset($new->args[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param New_|StaticCall $node
|
||||
* @param string[] $methods
|
||||
|
@ -85,9 +95,11 @@ final class RouteInfoFactory
|
|||
{
|
||||
$pathArgument = $node->args[0]->value;
|
||||
$routePath = $this->valueResolver->getValue($pathArgument);
|
||||
|
||||
// route path is needed
|
||||
if ($routePath === null || ! is_string($routePath)) {
|
||||
if ($routePath === null) {
|
||||
return null;
|
||||
}
|
||||
if (! is_string($routePath)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -19,9 +19,27 @@ use PHPStan\Type\UnionType;
|
|||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\Core\Exception\NotImplementedException;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use PHPStan\Type\Type;
|
||||
|
||||
final class PropertyRanker
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private const TYPE_TO_RANK = [
|
||||
StringType::class => 5,
|
||||
IntegerType::class => 5,
|
||||
BooleanType::class => 5,
|
||||
FloatType::class => 5,
|
||||
ArrayType::class => 10,
|
||||
IterableType::class => 10,
|
||||
TypeWithClassName::class => 15,
|
||||
IntersectionType::class => 20,
|
||||
UnionType::class => 25,
|
||||
MixedType::class => 30,
|
||||
CallableType::class => 35,
|
||||
];
|
||||
|
||||
public function rank(Property $property): int
|
||||
{
|
||||
/** @var PhpDocInfo|null $phpDocInfo */
|
||||
|
@ -31,32 +49,10 @@ final class PropertyRanker
|
|||
}
|
||||
|
||||
$varType = $phpDocInfo->getVarType();
|
||||
if ($varType instanceof StringType || $varType instanceof IntegerType || $varType instanceof BooleanType || $varType instanceof FloatType) {
|
||||
return 5;
|
||||
}
|
||||
|
||||
if ($varType instanceof ArrayType || $varType instanceof IterableType) {
|
||||
return 10;
|
||||
}
|
||||
|
||||
if ($varType instanceof TypeWithClassName) {
|
||||
return 15;
|
||||
}
|
||||
|
||||
if ($varType instanceof IntersectionType) {
|
||||
return 20;
|
||||
}
|
||||
|
||||
if ($varType instanceof UnionType) {
|
||||
return 25;
|
||||
}
|
||||
|
||||
if ($varType instanceof MixedType) {
|
||||
return 30;
|
||||
}
|
||||
|
||||
if ($varType instanceof CallableType) {
|
||||
return 35;
|
||||
foreach (self::TYPE_TO_RANK as $type => $rank) {
|
||||
if (is_a($varType, $type, true)) {
|
||||
return $rank;
|
||||
}
|
||||
}
|
||||
|
||||
throw new NotImplementedException(get_class($varType));
|
||||
|
|
|
@ -100,10 +100,12 @@ CODE_SAMPLE
|
|||
|
||||
private function processMarkTruthyNegation(BooleanNot $booleanNot): ?Identical
|
||||
{
|
||||
if (! $booleanNot->expr instanceof FuncCall || $this->getName($booleanNot->expr) !== 'count') {
|
||||
if (! $booleanNot->expr instanceof FuncCall) {
|
||||
return null;
|
||||
}
|
||||
if ($this->getName($booleanNot->expr) !== 'count') {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var Expr $expr */
|
||||
$expr = $booleanNot->expr->args[0]->value;
|
||||
|
||||
|
|
|
@ -72,8 +72,10 @@ CODE_SAMPLE
|
|||
if (! $this->isName($node->name, 'handle')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node->args === null || $node->args !== []) {
|
||||
if ($node->args === null) {
|
||||
return null;
|
||||
}
|
||||
if ($node->args !== []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -186,8 +186,10 @@ CODE_SAMPLE
|
|||
)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($parentNode instanceof Unset_ || $parentNode instanceof UnsetCast) {
|
||||
if ($parentNode instanceof Unset_) {
|
||||
return true;
|
||||
}
|
||||
if ($parentNode instanceof UnsetCast) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -229,7 +231,10 @@ CODE_SAMPLE
|
|||
{
|
||||
if ($parentNode instanceof Node) {
|
||||
$parentParentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($parentParentNode instanceof List_ || $parentParentNode instanceof Array_) {
|
||||
if ($parentParentNode instanceof List_) {
|
||||
return true;
|
||||
}
|
||||
if ($parentParentNode instanceof Array_) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -112,8 +112,10 @@ CODE_SAMPLE
|
|||
if ($namespace !== null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($classMethod->isAbstract() || $classMethod->isStatic()) {
|
||||
if ($classMethod->isAbstract()) {
|
||||
return true;
|
||||
}
|
||||
if ($classMethod->isStatic()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -122,8 +122,10 @@ CODE_SAMPLE
|
|||
$this->reset();
|
||||
|
||||
$this->matchOnEqualFirstValueAndSecondValue($node);
|
||||
|
||||
if ($this->firstValue === null || $this->secondValue === null) {
|
||||
if ($this->firstValue === null) {
|
||||
return null;
|
||||
}
|
||||
if ($this->secondValue === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -174,10 +176,12 @@ CODE_SAMPLE
|
|||
|
||||
private function areVariablesEqual(BinaryOp $binaryOp, ?Expr $firstValue, ?Expr $secondValue): bool
|
||||
{
|
||||
if ($firstValue === null || $secondValue === null) {
|
||||
if ($firstValue === null) {
|
||||
return false;
|
||||
}
|
||||
if ($secondValue === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->areNodesEqual($binaryOp->left, $firstValue) && $this->areNodesEqual(
|
||||
$binaryOp->right,
|
||||
$secondValue
|
||||
|
|
|
@ -109,7 +109,10 @@ CODE_SAMPLE
|
|||
$methodName = $this->getName($node->name);
|
||||
|
||||
$className = $this->resolveStaticCallClassName($node);
|
||||
if ($methodName === null || $className === null) {
|
||||
if ($methodName === null) {
|
||||
return null;
|
||||
}
|
||||
if ($className === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -62,16 +62,19 @@ final class TernaryToNullCoalescingRector extends AbstractRector
|
|||
// not a match
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($checkedNode === null || $fallbackNode === null) {
|
||||
if ($checkedNode === null) {
|
||||
return null;
|
||||
}
|
||||
if ($fallbackNode === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var Identical|NotIdentical $ternaryCompareNode */
|
||||
$ternaryCompareNode = $node->cond;
|
||||
if ($this->isNullMatch($ternaryCompareNode->left, $ternaryCompareNode->right, $checkedNode) ||
|
||||
$this->isNullMatch($ternaryCompareNode->right, $ternaryCompareNode->left, $checkedNode)
|
||||
) {
|
||||
if ($this->isNullMatch($ternaryCompareNode->left, $ternaryCompareNode->right, $checkedNode)) {
|
||||
return new Coalesce($checkedNode, $fallbackNode);
|
||||
}
|
||||
if ($this->isNullMatch($ternaryCompareNode->right, $ternaryCompareNode->left, $checkedNode)) {
|
||||
return new Coalesce($checkedNode, $fallbackNode);
|
||||
}
|
||||
|
||||
|
@ -86,9 +89,11 @@ final class TernaryToNullCoalescingRector extends AbstractRector
|
|||
|
||||
/** @var Isset_ $issetNode */
|
||||
$issetNode = $ternary->cond;
|
||||
|
||||
// none or multiple isset values cannot be handled here
|
||||
if (! isset($issetNode->vars[0]) || count($issetNode->vars) > 1) {
|
||||
if (! isset($issetNode->vars[0])) {
|
||||
return null;
|
||||
}
|
||||
if (count($issetNode->vars) > 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -67,10 +67,12 @@ CODE_SAMPLE
|
|||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
// only array with no explicit key assign, e.g. "$value[] = 5";
|
||||
if (! $node->var instanceof ArrayDimFetch || $node->var->dim !== null) {
|
||||
if (! $node->var instanceof ArrayDimFetch) {
|
||||
return null;
|
||||
}
|
||||
if ($node->var->dim !== null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$arrayDimFetchNode = $node->var;
|
||||
|
||||
/** @var Variable|PropertyFetch|StaticPropertyFetch|Expr $variableNode */
|
||||
|
|
|
@ -247,10 +247,12 @@ CODE_SAMPLE
|
|||
|
||||
private function processAssignMayInNextNode(Node $nextNode): ?Node
|
||||
{
|
||||
if (! $nextNode instanceof Expression || ! $nextNode->expr instanceof Assign) {
|
||||
if (! $nextNode instanceof Expression) {
|
||||
return null;
|
||||
}
|
||||
if (! $nextNode->expr instanceof Assign) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$mayNextIf = $nextNode->getAttribute(AttributeKey::NEXT_NODE);
|
||||
if (! $mayNextIf instanceof If_) {
|
||||
return null;
|
||||
|
|
|
@ -287,7 +287,10 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
$fileName = $reflectionMethod->getFileName();
|
||||
if (! $fileName || ! file_exists($fileName)) {
|
||||
if (! $fileName) {
|
||||
return null;
|
||||
}
|
||||
if (! file_exists($fileName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -84,9 +84,11 @@ CODE_SAMPLE
|
|||
if ($this->isStaticType($node->args[1]->value, StringType::class)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
//when less then 5 arguments given: do nothing
|
||||
if (! isset($node->args[4]) || $node->args[4]->value === null) {
|
||||
if (! isset($node->args[4])) {
|
||||
return null;
|
||||
}
|
||||
if ($node->args[4]->value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -145,8 +145,10 @@ CODE_SAMPLE
|
|||
foreach ($this->pseudoNamespacesToNamespaces as $namespacePrefixWithExcludedClasses) {
|
||||
$this->phpDocTypeRenamer->changeUnderscoreType($node, $namespacePrefixWithExcludedClasses);
|
||||
}
|
||||
|
||||
if ($node instanceof Name || $node instanceof Identifier) {
|
||||
if ($node instanceof Name) {
|
||||
return $this->processNameOrIdentifier($node);
|
||||
}
|
||||
if ($node instanceof Identifier) {
|
||||
return $this->processNameOrIdentifier($node);
|
||||
}
|
||||
|
||||
|
|
|
@ -192,10 +192,12 @@ CODE_SAMPLE
|
|||
{
|
||||
$returns = [];
|
||||
$this->traverseNodesWithCallable($stmts, function (Node $node) use (&$returns): ?int {
|
||||
if ($node instanceof Closure || $node instanceof Function_) {
|
||||
if ($node instanceof Closure) {
|
||||
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
|
||||
}
|
||||
if ($node instanceof Function_) {
|
||||
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
|
||||
}
|
||||
|
||||
if (! $node instanceof Return_) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -63,10 +63,15 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node->isFinal() || $node->isAbstract() || $this->isAnonymousClass($node)) {
|
||||
if ($node->isFinal()) {
|
||||
return null;
|
||||
}
|
||||
if ($node->isAbstract()) {
|
||||
return null;
|
||||
}
|
||||
if ($this->isAnonymousClass($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->isDoctrineEntityClass($node)) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -90,20 +90,17 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
$parentExpression = $expression->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($this->isUsedAsArrayKey($parentExpression, $node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->isInsideCondition($expression)) {
|
||||
if ($this->isUsedAsArraykeyOrInsideIfCondition($expression, $node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->hasPropertyInExpr($expression, $parent->expr)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->hasReAssign($expression, $parent->var) || $this->hasReAssign($expression, $parent->expr)) {
|
||||
if ($this->hasReAssign($expression, $parent->var)) {
|
||||
return null;
|
||||
}
|
||||
if ($this->hasReAssign($expression, $parent->expr)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -124,40 +121,14 @@ CODE_SAMPLE
|
|||
return $node;
|
||||
}
|
||||
|
||||
private function isUsedAsArrayKey(?Node $node, Variable $variable): bool
|
||||
private function isUsedAsArraykeyOrInsideIfCondition(Expression $expression, Variable $variable): bool
|
||||
{
|
||||
if (! $node instanceof Node) {
|
||||
return false;
|
||||
$parentExpression = $expression->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($this->isUsedAsArrayKey($parentExpression, $variable)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$arrayDimFetches = $this->betterNodeFinder->findInstanceOf($node, ArrayDimFetch::class);
|
||||
|
||||
foreach ($arrayDimFetches as $arrayDimFetch) {
|
||||
/** @var Node|null $dim */
|
||||
$dim = $arrayDimFetch->dim;
|
||||
if (! $dim instanceof Node) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$isFoundInKey = (bool) $this->betterNodeFinder->findFirst($dim, function (Node $node) use (
|
||||
$variable
|
||||
): bool {
|
||||
return $this->areNodesEqual($node, $variable);
|
||||
});
|
||||
if ($isFoundInKey) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function isInsideCondition(Expression $expression): bool
|
||||
{
|
||||
return (bool) $this->scopeAwareNodeFinder->findParentType(
|
||||
$expression,
|
||||
[If_::class, Else_::class, ElseIf_::class]
|
||||
);
|
||||
return $this->isInsideCondition($expression);
|
||||
}
|
||||
|
||||
private function hasPropertyInExpr(Expression $expression, Expr $expr): bool
|
||||
|
@ -215,7 +186,10 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
$countFound = $this->getCountFound($next, $variable);
|
||||
if ($countFound === 0 || $countFound >= 2) {
|
||||
if ($countFound === 0) {
|
||||
return null;
|
||||
}
|
||||
if ($countFound >= 2) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -237,6 +211,42 @@ CODE_SAMPLE
|
|||
return (bool) $loopNode;
|
||||
}
|
||||
|
||||
private function isUsedAsArrayKey(?Node $node, Variable $variable): bool
|
||||
{
|
||||
if (! $node instanceof Node) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$arrayDimFetches = $this->betterNodeFinder->findInstanceOf($node, ArrayDimFetch::class);
|
||||
|
||||
foreach ($arrayDimFetches as $arrayDimFetch) {
|
||||
/** @var Node|null $dim */
|
||||
$dim = $arrayDimFetch->dim;
|
||||
if (! $dim instanceof Node) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$isFoundInKey = (bool) $this->betterNodeFinder->findFirst($dim, function (Node $node) use (
|
||||
$variable
|
||||
): bool {
|
||||
return $this->areNodesEqual($node, $variable);
|
||||
});
|
||||
if ($isFoundInKey) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function isInsideCondition(Expression $expression): bool
|
||||
{
|
||||
return (bool) $this->scopeAwareNodeFinder->findParentType(
|
||||
$expression,
|
||||
[If_::class, Else_::class, ElseIf_::class]
|
||||
);
|
||||
}
|
||||
|
||||
private function mayBeArrayDimFetch(Node $node): Node
|
||||
{
|
||||
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
|
|
|
@ -87,8 +87,10 @@ CODE_SAMPLE
|
|||
|
||||
/** @var Param[] $params */
|
||||
$params = $node->getParams();
|
||||
|
||||
if ($paramTypes === [] || $params === []) {
|
||||
if ($paramTypes === []) {
|
||||
return null;
|
||||
}
|
||||
if ($params === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -64,7 +64,10 @@ final class SelfContainerMethodCallCollector
|
|||
|
||||
/** @var MethodCall $node */
|
||||
$serviceType = $this->valueResolver->getValue($node->args[0]->value);
|
||||
if ($serviceType === null || ! is_string($serviceType)) {
|
||||
if ($serviceType === null) {
|
||||
return null;
|
||||
}
|
||||
if (! is_string($serviceType)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -54,8 +54,10 @@ final class VarDumperTestTraitMethodArgsRector extends AbstractRector
|
|||
if (! $this->isNames($node->name, ['assertDumpEquals', 'assertDumpMatchesFormat'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (count((array) $node->args) <= 2 || $node->args[2]->value instanceof ConstFetch) {
|
||||
if (count((array) $node->args) <= 2) {
|
||||
return null;
|
||||
}
|
||||
if ($node->args[2]->value instanceof ConstFetch) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ final class PhpParserTypeAnalyzer
|
|||
public function isSubtypeOf(Node $possibleSubtype, Node $possibleParentType): bool
|
||||
{
|
||||
// skip until PHP 8 is out
|
||||
if ($possibleSubtype instanceof UnionType || $possibleParentType instanceof UnionType) {
|
||||
if ($this->isUnionType($possibleSubtype, $possibleParentType)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -54,16 +54,24 @@ final class PhpParserTypeAnalyzer
|
|||
return $possibleParentType === 'object';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Name|NullableType|Identifier $node
|
||||
*/
|
||||
private function isUnionType(Node $possibleSubtype, Node $possibleParentType): bool
|
||||
{
|
||||
if ($possibleSubtype instanceof UnionType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $possibleParentType instanceof UnionType;
|
||||
}
|
||||
|
||||
private function unwrapNullableAndToString(Node $node): string
|
||||
{
|
||||
if (! $node instanceof NullableType) {
|
||||
if (! $node instanceof NullableType && method_exists($node, 'toString')) {
|
||||
return $node->toString();
|
||||
}
|
||||
|
||||
return $node->type->toString();
|
||||
/** @var NullableType $type */
|
||||
$type = $node;
|
||||
return $type->type->toString();
|
||||
}
|
||||
|
||||
private function isTraversableOrIterableSubtype(string $possibleSubtype, string $possibleParentType): bool
|
||||
|
|
|
@ -119,8 +119,10 @@ CODE_SAMPLE
|
|||
if (! $this->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node->params === null || $node->params === []) {
|
||||
if ($node->params === null) {
|
||||
return null;
|
||||
}
|
||||
if ($node->params === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -68,10 +68,12 @@ final class GetterNodeParamTypeInferer extends AbstractTypeInferer implements Pa
|
|||
$propertyNames,
|
||||
&$returnType
|
||||
): ?int {
|
||||
if (! $node instanceof Return_ || $node->expr === null) {
|
||||
if (! $node instanceof Return_) {
|
||||
return null;
|
||||
}
|
||||
if ($node->expr === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$isMatch = $this->propertyFetchManipulator->isLocalPropertyOfNames($node->expr, $propertyNames);
|
||||
if (! $isMatch) {
|
||||
return null;
|
||||
|
|
|
@ -175,8 +175,10 @@ final class CallReflectionResolver
|
|||
|
||||
$classType = $this->nodeTypeResolver->resolve($node instanceof MethodCall ? $node->var : $node->class);
|
||||
$methodName = $this->nodeNameResolver->getName($node->name);
|
||||
|
||||
if ($methodName === null || ! $classType->hasMethod($methodName)->yes()) {
|
||||
if ($methodName === null) {
|
||||
return null;
|
||||
}
|
||||
if (! $classType->hasMethod($methodName)->yes()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -45,8 +45,10 @@ final class ConstantArrayTypeToCallReflectionResolver implements TypeToCallRefle
|
|||
if ($constantArrayTypeAndMethod === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($constantArrayTypeAndMethod->isUnknown() || ! $constantArrayTypeAndMethod->getCertainty()->yes()) {
|
||||
if ($constantArrayTypeAndMethod->isUnknown()) {
|
||||
return null;
|
||||
}
|
||||
if (! $constantArrayTypeAndMethod->getCertainty()->yes()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -289,10 +289,15 @@ final class ClassMethodAssignManipulator
|
|||
|
||||
private function isExplicitlyReferenced(Node $node): bool
|
||||
{
|
||||
if ($node instanceof Arg || $node instanceof ClosureUse || $node instanceof Param) {
|
||||
if ($node instanceof Arg) {
|
||||
return $node->byRef;
|
||||
}
|
||||
if ($node instanceof ClosureUse) {
|
||||
return $node->byRef;
|
||||
}
|
||||
if ($node instanceof Param) {
|
||||
return $node->byRef;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -56,8 +56,10 @@ final class ClassMethodPropertyFetchManipulator
|
|||
if (! $this->nodeNameResolver->isName($node->var, $propertyName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node->expr instanceof MethodCall || $node->expr instanceof StaticCall) {
|
||||
if ($node->expr instanceof MethodCall) {
|
||||
return null;
|
||||
}
|
||||
if ($node->expr instanceof StaticCall) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -53,10 +53,12 @@ final class FunctionLikeManipulator
|
|||
$this->callableNodeTraverser->traverseNodesWithCallable($functionLike, function (Node $node) use (
|
||||
&$returnedLocalPropertyNames
|
||||
) {
|
||||
if (! $node instanceof Return_ || $node->expr === null) {
|
||||
if (! $node instanceof Return_) {
|
||||
return null;
|
||||
}
|
||||
if ($node->expr === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->propertyFetchAnalyzer->isLocalPropertyFetch($node->expr)) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -88,10 +88,12 @@ final class IdentifierManipulator
|
|||
|
||||
private function resolveOldMethodName(Node $node): ?string
|
||||
{
|
||||
if ($node instanceof StaticCall || $node instanceof MethodCall) {
|
||||
if ($node instanceof StaticCall) {
|
||||
return $this->nodeNameResolver->getName($node->name);
|
||||
}
|
||||
if ($node instanceof MethodCall) {
|
||||
return $this->nodeNameResolver->getName($node->name);
|
||||
}
|
||||
|
||||
return $this->nodeNameResolver->getName($node);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -101,13 +101,18 @@ final class IfManipulator
|
|||
if ($if->stmts === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $if->cond instanceof NotIdentical || ! $this->isNotIdenticalNullCompare($if->cond)) {
|
||||
if (! $if->cond instanceof NotIdentical) {
|
||||
return null;
|
||||
}
|
||||
if (! $this->isNotIdenticalNullCompare($if->cond)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$insideIfNode = $if->stmts[0];
|
||||
if (! $insideIfNode instanceof Expression || ! $insideIfNode->expr instanceof Assign) {
|
||||
if (! $insideIfNode instanceof Expression) {
|
||||
return null;
|
||||
}
|
||||
if (! $insideIfNode->expr instanceof Assign) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -34,7 +34,10 @@ final class NullsafeManipulator
|
|||
}
|
||||
|
||||
$parentIdentifier = $nextExprIdentifier->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($parentIdentifier instanceof MethodCall || $parentIdentifier instanceof NullsafeMethodCall) {
|
||||
if ($parentIdentifier instanceof MethodCall) {
|
||||
return new NullsafeMethodCall($expr, $nextExprIdentifier);
|
||||
}
|
||||
if ($parentIdentifier instanceof NullsafeMethodCall) {
|
||||
return new NullsafeMethodCall($expr, $nextExprIdentifier);
|
||||
}
|
||||
|
||||
|
|
|
@ -16,10 +16,12 @@ final class CallableNodeTraverser
|
|||
*/
|
||||
public function traverseNodesWithCallable($nodes, callable $callable): void
|
||||
{
|
||||
if ($nodes === [] || $nodes === null) {
|
||||
if ($nodes === []) {
|
||||
return;
|
||||
}
|
||||
if ($nodes === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! is_array($nodes)) {
|
||||
$nodes = [$nodes];
|
||||
}
|
||||
|
|
|
@ -96,8 +96,13 @@ final class InlineCodeParser
|
|||
if ($content instanceof Concat) {
|
||||
return $this->stringify($content->left) . $this->stringify($content->right);
|
||||
}
|
||||
|
||||
if ($content instanceof Variable || $content instanceof PropertyFetch || $content instanceof StaticPropertyFetch) {
|
||||
if ($content instanceof Variable) {
|
||||
return $this->betterStandardPrinter->print($content);
|
||||
}
|
||||
if ($content instanceof PropertyFetch) {
|
||||
return $this->betterStandardPrinter->print($content);
|
||||
}
|
||||
if ($content instanceof StaticPropertyFetch) {
|
||||
return $this->betterStandardPrinter->print($content);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user