make return null consistent on no change

This commit is contained in:
Tomas Votruba 2018-10-21 12:19:14 +02:00
parent 985d38e267
commit f330c5e9c2
46 changed files with 103 additions and 96 deletions

View File

@ -104,23 +104,23 @@ final class MergeIsCandidateRector extends AbstractRector
public function refactor(Node $node): ?Node
{
if ($this->isType($node, 'Rector\Rector\AbstractRector')) {
return $node;
return null;
}
if (! $node->isAbstract()) {
return $node;
return null;
}
// has "isCandidate()" method?
if (! $this->hasClassIsCandidateMethod($node)) {
return $node;
return null;
}
[$isCandidateClassMethodPosition, $isCandidateClassMethod] = $this->getClassMethodByName($node, 'isCandidate');
[$refactorClassMethodPosition, $refactorClassMethod] = $this->getClassMethodByName($node, 'refactor');
if ($refactorClassMethod === null) {
return $node;
return null;
}
// 1. replace "isCandidate()" method by "getNodeType()" method

View File

@ -76,19 +76,19 @@ final class CombinedAssignRector extends AbstractRector
public function refactor(Node $node): ?Node
{
if (! $node->expr instanceof BinaryOp) {
return $node;
return null;
}
/** @var BinaryOp $binaryNode */
$binaryNode = $node->expr;
if (! $this->areNodesEqual($node->var, $binaryNode->left)) {
return $node;
return null;
}
$binaryNodeClass = get_class($binaryNode);
if (! isset($this->binaryOpClassToAssignOpClass[$binaryNodeClass])) {
return $node;
return null;
}
$newAssignNodeClass = $this->binaryOpClassToAssignOpClass[$binaryNodeClass];

View File

@ -33,11 +33,11 @@ final class SimplifyFuncGetArgsCountRector extends AbstractRector
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'count')) {
return $node;
return null;
}
if (! $node->args[0]->value instanceof FuncCall) {
return $node;
return null;
}
/** @var FuncCall $innerFuncCall */

View File

@ -31,17 +31,17 @@ final class SimplifyInArrayValuesRector extends AbstractRector
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'in_array')) {
return $node;
return null;
}
if (! $node->args[1]->value instanceof FuncCall) {
return $node;
return null;
}
/** @var FuncCall $innerFunCall */
$innerFunCall = $node->args[1]->value;
if (! $this->isName($innerFunCall, 'array_values')) {
return $node;
return null;
}
$node->args[1] = $innerFunCall->args[0];

View File

@ -33,21 +33,21 @@ final class SimplifyStrposLowerRector extends AbstractRector
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'strpos')) {
return $node;
return null;
}
if (! isset($node->args[0])) {
return $node;
return null;
}
if (! $node->args[0]->value instanceof FuncCall) {
return $node;
return null;
}
/** @var FuncCall $innerFuncCall */
$innerFuncCall = $node->args[0]->value;
if (! $this->isName($innerFuncCall, 'strtolower')) {
return $node;
return null;
}
// pop 1 level up

View File

@ -56,7 +56,7 @@ final class GetClassToInstanceOfRector extends AbstractRector
$className = $this->matchClassName($node->right);
} else {
return $node;
return null;
}
$instanceOfNode = new Instanceof_($varNode, new FullyQualified($className));

View File

@ -44,7 +44,7 @@ final class SimplifyArraySearchRector extends AbstractRector
{
$match = $this->matchArraySearchFuncCallAndBoolConstFetch($node);
if ($match === null) {
return $node;
return null;
}
[$arraySearchFuncCallNode, $boolConstFetchNode] = $match;

View File

@ -63,20 +63,20 @@ final class SimplifyConditionsRector extends AbstractRector
}
}
private function processBooleanNot(BooleanNot $node): Node
private function processBooleanNot(BooleanNot $node): ?Node
{
if (! $node->expr instanceof BinaryOp) {
return $node;
return null;
}
if ($this->shouldSkip($node->expr)) {
return $node;
return null;
}
return $this->createInversedBooleanOp($node->expr);
}
private function processIdenticalAndNotIdentical(BinaryOp $node): Node
private function processIdenticalAndNotIdentical(BinaryOp $node): ?Node
{
if ($node->left instanceof Identical || $node->left instanceof NotIdentical) {
$subBinaryOpNode = $node->left;
@ -85,7 +85,7 @@ final class SimplifyConditionsRector extends AbstractRector
$subBinaryOpNode = $node->right;
$shouldInverse = $this->isFalse($node->left);
} else {
return $node;
return null;
}
if ($shouldInverse) {

View File

@ -39,21 +39,21 @@ final class SimplifyIdenticalFalseToBooleanNotRector extends AbstractRector
$comparedNode = $node->right;
$shouldUnwrap = $node->right instanceof BooleanNot;
} else {
return $node;
return null;
}
if ($shouldUnwrap) {
/** @var BooleanNot $comparedNode */
$comparedNode = $comparedNode->expr;
if ($this->shouldSkip($comparedNode)) {
return $node;
return null;
}
return $comparedNode;
}
if ($this->shouldSkip($comparedNode)) {
return $node;
return null;
}
return new BooleanNot($comparedNode);

View File

@ -52,7 +52,7 @@ final class ClassAndMethodNodeVisitor extends NodeVisitorAbstract
public function enterNode(Node $node)
{
if ($node instanceof Class_ && $node->isAnonymous()) {
return $node;
return null;
}
$this->processClass($node);

View File

@ -57,11 +57,11 @@ CODE_SAMPLE
public function refactor(Node $node): ?Node
{
if (! $this->isInTestClass($node)) {
return $node;
return null;
}
if (! $node->stmts) {
return $node;
return null;
}
$proccesed = [];

View File

@ -81,7 +81,7 @@ CODE_SAMPLE
{
// only array with no explicit key assign, e.g. "$value[] = 5";
if (! $node->var instanceof ArrayDimFetch || $node->var->dim !== null) {
return $node;
return null;
}
$arrayDimFetchNode = $node->var;

View File

@ -50,12 +50,12 @@ CODE_SAMPLE
{
// already non-public
if (! $node->isPublic()) {
return $node;
return null;
}
// explicitly public
if ($node->flags !== 0) {
return $node;
return null;
}
$node->flags = Class_::MODIFIER_PUBLIC;

View File

@ -114,14 +114,14 @@ CODE_SAMPLE
{
// is system constant?
if (in_array(strtoupper((string) $node->name), $this->phpReservedConstants, true)) {
return $node;
return null;
}
$currentConstantName = (string) $node->name;
// is uppercase, all good
if ($currentConstantName === strtoupper($currentConstantName)) {
return $node;
return null;
}
$node->name = new FullyQualified(strtoupper($currentConstantName));

View File

@ -53,7 +53,7 @@ CODE_SAMPLE
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
return $node;
return null;
}
/** @var List_ $listNode */

View File

@ -68,13 +68,13 @@ CODE_SAMPLE
public function refactor(Node $node): ?Node
{
if (! $node->cond instanceof Assign) {
return $node;
return null;
}
/** @var Assign $assignNode */
$assignNode = $node->cond;
if (! $this->isListToEachAssign($assignNode)) {
return $node;
return null;
}
/** @var FuncCall $eachFuncCall */

View File

@ -73,7 +73,7 @@ CODE_SAMPLE
public function refactor(Node $node): ?Node
{
if ($node->stmts === null) {
return $node;
return null;
}
foreach ($node->stmts as $key => $stmt) {

View File

@ -57,7 +57,7 @@ final class CallUserMethodRector extends AbstractRector
{
$newName = $this->matchNewFunctionName($node);
if ($newName === null) {
return $node;
return null;
}
$node->name = new Name($newName);

View File

@ -53,23 +53,23 @@ CODE_SAMPLE
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'count')) {
return $node;
return null;
}
// check if it has some condition before already, if so, probably it's already handled
$parentNode = $node->getAttribute(Attribute::PARENT_NODE);
if ($parentNode instanceof Ternary) {
return $node;
return null;
}
if (! isset($node->args[0])) {
return $node;
return null;
}
$countedNode = $node->args[0]->value;
if ($this->isCountableType($countedNode)) {
return $node;
return null;
}
$conditionNode = new BooleanOr(

View File

@ -74,7 +74,7 @@ final class EregToPregMatchRector extends AbstractRector
$functionName = $this->getName($node);
if (! isset($this->oldNamesToNewOnes[$functionName])) {
return $node;
return null;
}
$patternNode = $node->args[0]->value;

View File

@ -41,7 +41,7 @@ final class MultiDirnameRector extends AbstractRector
$this->nestingLevel = 0;
if (! $this->isName($node, 'dirname')) {
return $node;
return null;
}
$activeFuncCallNode = $node;

View File

@ -33,11 +33,11 @@ final class PowToExpRector extends AbstractRector
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'pow')) {
return $node;
return null;
}
if (count($node->args) !== 2) {
return $node;
return null;
}
$firstArgument = $node->args[0]->value;

View File

@ -45,11 +45,11 @@ CODE_SAMPLE
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'define')) {
return $node;
return null;
}
if (! isset($node->args[2])) {
return $node;
return null;
}
unset($node->args[2]);

View File

@ -51,18 +51,18 @@ CODE_SAMPLE
{
// exception handle has 1 param exactly
if (count($node->params) !== 1) {
return $node;
return null;
}
$paramNode = $node->params[0];
// handle only Exception typehint
if ((string) $paramNode->type !== 'Exception') {
return $node;
return null;
}
// is probably handling exceptions
if (! Strings::match((string) $node->name, '#handle#i')) {
return $node;
return null;
}
$paramNode->type = new FullyQualified('Throwable');

View File

@ -76,7 +76,7 @@ CODE_SAMPLE
$namespace = $node->getAttribute(Attribute::NAMESPACE_NAME);
// catch only classes without namespace
if ($namespace) {
return $node;
return null;
}
/** @var Class_ $classNode */
@ -84,7 +84,7 @@ CODE_SAMPLE
// anonymous class → skip
if ($classNode->name === null || $node->isAbstract() || $node->isStatic()) {
return $node;
return null;
}
// process parent call references first
@ -92,7 +92,7 @@ CODE_SAMPLE
// not PSR-4 constructor
if (strtolower((string) $classNode->name) !== strtolower((string) $node->name)) {
return $node;
return null;
}
// does it already have a __construct method?

View File

@ -37,7 +37,7 @@ final class EmptyListRector extends AbstractRector
{
foreach ($node->items as $item) {
if ($item !== null) {
return $node;
return null;
}
}

View File

@ -41,11 +41,11 @@ final class ListSplitStringRector extends AbstractRector
public function refactor(Node $node): ?Node
{
if (! $node->var instanceof List_) {
return $node;
return null;
}
if (! $this->isStringType($node->expr)) {
return $node;
return null;
}
$node->expr = new FuncCall(new Name('str_split'), [new Arg($node->expr)]);

View File

@ -41,7 +41,7 @@ final class ListSwapArrayOrderRector extends AbstractRector
public function refactor(Node $node): ?Node
{
if (! $node->var instanceof List_) {
return $node;
return null;
}
$printerVars = [];
@ -51,13 +51,13 @@ final class ListSwapArrayOrderRector extends AbstractRector
if ($item->value instanceof ArrayDimFetch) {
$printerVars[] = $this->print($item->value->var);
} else {
return $node;
return null;
}
}
// relevant only in 1 variable type
if (count(array_unique($printerVars)) !== 1) {
return $node;
return null;
}
// wrap with array_reverse, to reflect reverse assign order in left

View File

@ -104,24 +104,24 @@ CODE_SAMPLE
// too many types to handle
if (count($varTypes) > 2) {
return $node;
return null;
}
$this->isNullableType = in_array('null', $varTypes, true);
// exactly 1 type only can be changed || 2 types with nullable; nothing else
if (count($varTypes) !== 1 && (count($varTypes) === 2 && ! $this->isNullableType)) {
return $node;
return null;
}
$propertyType = $this->getPropertyTypeWithoutNull($varTypes);
$propertyType = $this->shortenLongType($propertyType);
if (! $this->typeAnalyzer->isPropertyTypeHintableType($propertyType)) {
return $node;
return null;
}
if (! $this->matchesDocTypeAndDefaultValueType($propertyType, $node)) {
return $node;
return null;
}
if ($this->isNullableType) {

View File

@ -52,7 +52,7 @@ final class TernaryToNullCoalescingRector extends AbstractRector
[$checkedNode, $fallbackNode] = [$node->if, $node->else];
} else {
// not a match
return $node;
return null;
}
/** @var Identical|NotIdentical $ternaryCompareNode */

View File

@ -56,7 +56,7 @@ CODE_SAMPLE
public function refactor(Node $node): ?Node
{
if (count($node->catches) < 2) {
return $node;
return null;
}
$catchKeysByContent = $this->collectCatchKeysByContent($node);

View File

@ -53,7 +53,7 @@ final class CatchAndClosureUseNameRector extends AbstractRector
public function refactor(Node $node): ?Node
{
if (! $this->isTypes($node, ['PhpParser\Node\Stmt\Catch_', 'PhpParser\Node\Expr\ClosureUse'])) {
return $node;
return null;
}
if (! $this->isName($node, 'var')) {
@ -62,7 +62,7 @@ final class CatchAndClosureUseNameRector extends AbstractRector
$parentNode = $node->getAttribute(Attribute::PARENT_NODE);
if ($parentNode instanceof PropertyFetch) {
return $node;
return null;
}
/** @var Variable $variableNode */

View File

@ -94,7 +94,7 @@ CODE_SAMPLE
$parentNode = $node->getAttribute(Attribute::PARENT_NODE);
if ($parentNode instanceof MethodCall) {
return $node;
return null;
}
return $this->methodCallNodeFactory->createWithVariableAndMethodName($node, 'toString');

View File

@ -36,7 +36,7 @@ final class ConstantToStaticCallRector extends AbstractRector
public function refactor(Node $node): ?Node
{
if (! Strings::startsWith($node->name->toString(), 'SS_')) {
return $node;
return null;
}
$staticCallNode = new StaticCall(new FullyQualified('Environment'), 'getEnv');

View File

@ -11,7 +11,12 @@ final class AfterRectorCodingStyle
/**
* @var string
*/
private const ECS_BIN_PATH = __DIR__ . '/../../ecs-after-rector.yml';
private const ECS_BIN_PATH = 'vendor/bin/ecs';
/**
* @var string
*/
private const ECS_AFTER_RECTOR_CONFIG = __DIR__ . '/../../ecs-after-rector.yml';
/**
* @param string[] $source
@ -20,7 +25,12 @@ final class AfterRectorCodingStyle
{
$this->validate();
$command = sprintf('vendor/bin/ecs check %s --config %s --fix', implode(' ', $source), self::ECS_BIN_PATH);
$command = sprintf(
'%s check %s --config %s --fix',
self::ECS_BIN_PATH,
implode(' ', $source),
self::ECS_AFTER_RECTOR_CONFIG
);
$process = new Process($command);
$process->run();

View File

@ -40,7 +40,7 @@ final class ConstructorInjectionNodeVisitor extends NodeVisitorAbstract
public function enterNode(Node $node): ?Node
{
if (! $node instanceof Class_ || $node->isAnonymous()) {
return $node;
return null;
}
return $this->processClassNode($node);

View File

@ -24,7 +24,7 @@ final class ExpressionAddingNodeVisitor extends NodeVisitorAbstract
public function leaveNode(Node $node)
{
if (! isset($this->expressionsToAdd[$node])) {
return $node;
return null;
}
$nodes = array_merge([$node], $this->expressionsToAdd[$node]);

View File

@ -55,12 +55,12 @@ final class FunctionReplaceRector extends AbstractRector
{
// anonymous function
if (! $node->name instanceof Name) {
return $node;
return null;
}
$functionName = $node->name->toString();
if (! isset($this->oldFunctionToNewFunction[$functionName])) {
return $node;
return null;
}
$newFunctionName = $this->oldFunctionToNewFunction[$functionName];

View File

@ -56,19 +56,16 @@ final class FunctionToStaticCallRector extends AbstractRector
{
// anonymous function
if (! $node->name instanceof Name) {
return $node;
return null;
}
$functionName = $node->name->toString();
if (! isset($this->functionToStaticCall[$functionName])) {
return $node;
return null;
}
[$className, $methodName] = $this->functionToStaticCall[$functionName];
$staticCallNode = new StaticCall(new FullyQualified($className), $methodName);
$staticCallNode->args = $node->args;
return $staticCallNode;
return new StaticCall(new FullyQualified($className), $methodName, $node->args);
}
}

View File

@ -99,7 +99,7 @@ final class UnsetAndIssetToMethodCallRector extends AbstractRector
$method = $this->resolveMethod($node);
if ($method === null) {
return $node;
return null;
}
/** @var ArrayDimFetch $arrayDimFetchNode */

View File

@ -69,12 +69,12 @@ CODE_SAMPLE
{
// is chain method call
if (! $node->var instanceof MethodCall) {
return $node;
return null;
}
// is matching type
if (! $this->isTypes($node->var, $this->classesToDefluent)) {
return $node;
return null;
}
/** @var MethodCall $innerMethodCallNode */

View File

@ -70,7 +70,7 @@ CODE_SAMPLE
{
// process only existing statements
if ($node->stmts === null) {
return $node;
return null;
}
$classMethodStatementCount = count($node->stmts);

View File

@ -87,15 +87,15 @@ CODE_SAMPLE
public function refactor(Node $node): ?Node
{
if (! $node->expr instanceof Variable) {
return $node;
return null;
}
if ($node->expr->name !== 'this') {
return $node;
if (! $this->isName($node->expr, 'this')) {
return null;
}
if (! $this->isTypes($node->expr, $this->classesToDefluent)) {
return $node;
return null;
}
$this->removeNode($node);

View File

@ -160,7 +160,7 @@ CODE_SAMPLE
$methodName = $node->name;
if (! isset($oldToNewMethods[$methodName])) {
return $node;
return null;
}
$node->name = $oldToNewMethods[$methodName];
@ -197,7 +197,7 @@ CODE_SAMPLE
$currentMethodName = $this->getName($node);
if (! isset($oldToNewMethods[$currentMethodName])) {
return $node;
return null;
}
$this->identifierRenamer->renameNode($node, $oldToNewMethods[$currentMethodName]);

View File

@ -159,7 +159,7 @@ final class StaticMethodNameReplacerRector extends AbstractRector
$methodName = $node->name;
if (! isset($oldToNewMethods[$methodName])) {
return $node;
return null;
}
$node->name = $oldToNewMethods[$methodName];
@ -210,7 +210,7 @@ final class StaticMethodNameReplacerRector extends AbstractRector
$methodName = $identifierNode->toString();
if (! isset($oldToNewMethods[$methodName])) {
return $node;
return null;
}
if ($this->isClassRename($oldToNewMethods)) {

View File

@ -85,7 +85,7 @@ final class PropertyNameReplacerRector extends AbstractRector
$propertyName = $identifierNode->toString();
if (! isset($oldToNewProperties[$propertyName])) {
return $node;
return null;
}
foreach ($oldToNewProperties as $oldProperty => $newProperty) {