Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions abz/UnderElse.php

This file was deleted.

1 change: 0 additions & 1 deletion config/set/dead-code/dead-code.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ services:
Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector: ~
Rector\DeadCode\Rector\If_\SimplifyIfElseWithSameContentRector: ~
Rector\DeadCode\Rector\Ternary\TernaryToBooleanOrFalseToBooleanAndRector: ~
Rector\DeadCode\Rector\FunctionLike\RemoveUnusedElseForReturnedValueRector: ~
2 changes: 2 additions & 0 deletions config/set/solid/early-return.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
services:
Rector\SOLID\Rector\If_\RemoveAlwaysElseRector: null
1 change: 1 addition & 0 deletions ecs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ parameters:
- 'src/Rector/AbstractRector/ComplexRemovalTrait.php'
- 'packages/TypeDeclaration/src/VendorLock/VendorLockResolver.php'
- 'packages/TypeDeclaration/src/PhpParserTypeAnalyzer.php'
- 'src/PhpParser/Node/Manipulator/IfManipulator.php'
# aliases
- 'packages/CodingStyle/src/Rector/Namespace_/ImportFullyQualifiedNamesRector.php'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\Include_;
use PhpParser\Node\Scalar\MagicConst\Dir;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
Expand Down Expand Up @@ -85,7 +86,7 @@ public function refactor(Node $node): ?Node
}
}

$node->expr = new Concat(new Node\Scalar\MagicConst\Dir(), $node->expr);
$node->expr = new Concat(new Dir(), $node->expr);

return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ private function areArrayKeysExistsArgsMatchingDimFetch(FuncCall $funcCall, Arra
if (! $this->areNodesEqual($arrayDimFetch->var, $valuesExpr)) {
return false;
}

if (! $this->areNodesEqual($arrayDimFetch->dim, $keyExpr)) {
return false;
}

return true;
return $this->areNodesEqual($arrayDimFetch->dim, $keyExpr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\DeadCode\Rector\Ternary;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\Ternary;
use PHPStan\Type\BooleanType;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -80,6 +81,6 @@ public function refactor(Node $node): ?Node
return null;
}

return new Node\Expr\BinaryOp\BooleanAnd($node->cond, $node->if);
return new BooleanAnd($node->cond, $node->if);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,6 @@ private function shouldSkip(Class_ $class): bool
if (! $this->isTraitMatch($class)) {
return true;
}

if ($this->classManipulator->hasPropertyName($class, 'id')) {
return true;
}

return false;
return $this->classManipulator->hasPropertyName($class, 'id');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Type\ArrayType;
Expand Down Expand Up @@ -130,7 +131,7 @@ public function refactor(Node $node): ?Node

$this->classManipulator->addAsFirstTrait($node, 'Knp\DoctrineBehaviors\Model\Sluggable\SluggableTrait');

$node->implements[] = new Node\Name\FullyQualified('Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface');
$node->implements[] = new FullyQualified('Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface');

$this->addGetSluggableFieldsClassMethod($node, $slugFields);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ private function dumpEntityTranslation(Class_ $class, array $translatedPropertyT
throw new ShouldNotHappenException();
}

$classShortName = (string) $class->name . 'Translation';
$classShortName = $class->name . 'Translation';
$filePath = dirname($fileInfo->getRealPath()) . DIRECTORY_SEPARATOR . $classShortName . '.php';

$namespace = $class->getAttribute(AttributeKey::PARENT_NODE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,6 @@ private function shouldRemoveProperty(PhpDocInfo $phpDocInfo): bool
if ($phpDocInfo->hasByType(TreeParentTagValueNode::class)) {
return true;
}

if ($phpDocInfo->hasByType(TreeLevelTagValueNode::class)) {
return true;
}

return false;
return $phpDocInfo->hasByType(TreeLevelTagValueNode::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,6 @@ private function shouldSkipFuncCall(FuncCall $funcCall): bool
if ($classMethod === null) {
return true;
}

if ($classMethod->isStatic()) {
return true;
}

return false;
return $classMethod->isStatic();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nette\Application\UI\Control;
use Nette\Application\UI\Presenter;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\Nette\NodeFactory\ActionRenderFactory;
Expand Down Expand Up @@ -108,7 +109,7 @@ public function refactor(Node $node): ?Node
private function createRenderMethodCall(
ClassMethod $classMethod,
MagicTemplatePropertyCalls $magicTemplatePropertyCalls
): Node\Expr\MethodCall {
): MethodCall {
if ($this->isObjectType($classMethod, Presenter::class)) {
return $this->actionRenderFactory->createThisTemplateRenderMethodCall($magicTemplatePropertyCalls);
}
Expand Down
10 changes: 6 additions & 4 deletions packages/Nette/src/TemplatePropertyAssignCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -56,11 +58,11 @@ public function collectTemplateFileNameVariablesAndNodesToRemove(
$this->callableNodeTraverser->traverseNodesWithCallable(
(array) $classMethod->stmts,
function (Node $node): void {
if ($node instanceof Expr\MethodCall) {
if ($node instanceof MethodCall) {
$this->collectTemplateFileExpr($node);
}

if ($node instanceof Expr\Assign) {
if ($node instanceof Assign) {
$this->collectVariableFromAssign($node);
}
}
Expand All @@ -69,7 +71,7 @@ function (Node $node): void {
return new MagicTemplatePropertyCalls($this->templateFileExpr, $this->templateVariables, $this->nodesToRemove);
}

private function collectTemplateFileExpr(Expr\MethodCall $methodCall): void
private function collectTemplateFileExpr(MethodCall $methodCall): void
{
if ($this->nameResolver->isName($methodCall->name, 'render')) {
if (isset($methodCall->args[0])) {
Expand All @@ -85,7 +87,7 @@ private function collectTemplateFileExpr(Expr\MethodCall $methodCall): void
}
}

private function collectVariableFromAssign(Expr\Assign $assign): void
private function collectVariableFromAssign(Assign $assign): void
{
// $this->template = x
if ($assign->var instanceof PropertyFetch) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,12 @@ public function addReturnTag(Node $node, Type $newType): void
return;
}

if ($node->getDocComment()) {
if ($node->getDocComment() !== null) {
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
$returnTagValueNode = $phpDocInfo->getByType(ReturnTagValueNode::class);

// overide existing type
if ($returnTagValueNode) {
if ($returnTagValueNode !== null) {
$newPHPStanPhpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($newType);
$returnTagValueNode->type = $newPHPStanPhpDocType;

Expand Down Expand Up @@ -663,12 +663,7 @@ private function areBothSameScalarType(Type $firstType, Type $secondType): bool
if ($firstType instanceof FloatType && $secondType instanceof FloatType) {
return true;
}

if ($firstType instanceof BooleanType && $secondType instanceof BooleanType) {
return true;
}

return false;
return $firstType instanceof BooleanType && $secondType instanceof BooleanType;
}

private function areAliasedObjectMatchingFqnObject(Type $firstType, Type $secondType): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,6 @@ private function shouldSkip(FuncCall $funcCall): bool
if ($this->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_KEY_FIRST_LAST)) {
return false;
}

if (function_exists(self::ARRAY_KEY_FIRST) && function_exists(self::ARRAY_KEY_LAST)) {
return false;
}

return true;
return ! (function_exists(self::ARRAY_KEY_FIRST) && function_exists(self::ARRAY_KEY_LAST));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

declare(strict_types=1);

namespace Rector\DeadCode\Rector\FunctionLike;
namespace Rector\SOLID\Rector\If_;

use PhpParser\Node;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\If_;
use Rector\PhpParser\Node\Manipulator\IfManipulator;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

/**
* @see \Rector\DeadCode\Tests\Rector\FunctionLike\RemoveUnusedElseForReturnedValueRector\RemoveUnusedElseForReturnedValueRectorTest
* @see \Rector\SOLID\Tests\Rector\If_\RemoveAlwaysElseRector\RemoveAlwaysElseRectorTest
*/
final class RemoveUnusedElseForReturnedValueRector extends AbstractRector
final class RemoveAlwaysElseRector extends AbstractRector
{
/**
* @var IfManipulator
Expand All @@ -29,15 +28,15 @@ public function __construct(IfManipulator $ifManipulator)

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Remove if for last else, if previous values were awlways returned', [
return new RectorDefinition('Remove if for last else, if previous values were throw', [
new CodeSample(
<<<'PHP'
class SomeClass
{
public function run($value)
{
if ($value) {
return 5;
throw new \InvalidStateException;
} else {
return 10;
}
Expand All @@ -51,7 +50,7 @@ class SomeClass
public function run($value)
{
if ($value) {
return 5;
throw new \InvalidStateException;
}

return 10;
Expand All @@ -68,29 +67,23 @@ public function run($value)
*/
public function getNodeTypes(): array
{
return [FunctionLike::class];
return [If_::class];
}

/**
* @param FunctionLike $node
* @param If_ $node
*/
public function refactor(Node $node): ?Node
{
$this->traverseNodesWithCallable((array) $node->getStmts(), function (Node $node) {
if (! $node instanceof If_) {
return null;
}

if (! $this->ifManipulator->isWithElseAlwaysReturnValue($node)) {
return null;
}
if (! $this->ifManipulator->isEarlyElse($node)) {
return null;
}

foreach ($node->else->stmts as $elseStmt) {
$this->addNodeAfterNode($elseStmt, $node);
}
foreach ($node->else->stmts as $elseStmt) {
$this->addNodeAfterNode($elseStmt, $node);
}

$node->else = null;
});
$node->else = null;

return $node;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Rector\DeadCode\Tests\Rector\FunctionLike\RemoveUnusedElseForReturnedValueRector\Fixture;
namespace Rector\SOLID\Tests\Rector\If_\RemoveAlwaysElse\Fixture;

class SomeClass
class EarlyElse
{
public function run($value)
{
Expand All @@ -18,9 +18,9 @@ class SomeClass
-----
<?php

namespace Rector\DeadCode\Tests\Rector\FunctionLike\RemoveUnusedElseForReturnedValueRector\Fixture;
namespace Rector\SOLID\Tests\Rector\If_\RemoveAlwaysElse\Fixture;

class SomeClass
class EarlyElse
{
public function run($value)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Rector\SOLID\Tests\Rector\If_\RemoveAlwaysElse\Fixture;

class SomeClass
{
public function run($value)
{
if ($value) {
throw new \InvalidStateException;
} else {
return 10;
}
}
}

?>
-----
<?php

namespace Rector\SOLID\Tests\Rector\If_\RemoveAlwaysElse\Fixture;

class SomeClass
{
public function run($value)
{
if ($value) {
throw new \InvalidStateException;
}
return 10;
}
}

?>
Loading