Skip to content

Commit

Permalink
[Core] Remove BetterStandardPrinter::pStmt_Class() (#3072)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Nov 17, 2022
1 parent 8ac960d commit a8f322a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 36 deletions.
Expand Up @@ -5,6 +5,7 @@
namespace Rector\CodeQuality\Rector\Empty_;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\Empty_;
Expand Down Expand Up @@ -54,18 +55,16 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
return new Identical($node->expr, new Array_());
}

private function isAllowedExpr(Node\Expr $expr): bool
private function isAllowedExpr(Expr $expr): bool
{
if ($expr instanceof Variable) {
return true;
}

if ($expr instanceof PropertyFetch) {
return true;
}
if ($expr instanceof StaticPropertyFetch) {
return true;
}

return false;
return $expr instanceof StaticPropertyFetch;
}
}
11 changes: 3 additions & 8 deletions rules/Removing/Rector/Class_/RemoveTraitUseRector.php
Expand Up @@ -9,7 +9,6 @@
use PhpParser\Node\Stmt\Trait_;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
Expand All @@ -19,8 +18,6 @@
*/
final class RemoveTraitUseRector extends AbstractRector implements ConfigurableRectorInterface
{
private bool $classHasChanged = false;

/**
* @var string[]
*/
Expand Down Expand Up @@ -61,7 +58,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$this->classHasChanged = false;
$classHasChanged = false;

foreach ($node->getTraitUses() as $traitUse) {
foreach ($traitUse->traits as $trait) {
Expand All @@ -70,13 +67,11 @@ public function refactor(Node $node): ?Node
}

$this->removeNode($traitUse);
$this->classHasChanged = true;
$classHasChanged = true;
}
}

// invoke re-print
if ($this->classHasChanged) {
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
if ($classHasChanged) {
return $node;
}

Expand Down
23 changes: 0 additions & 23 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Expand Up @@ -25,7 +25,6 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\TraitUse;
use PhpParser\Node\Stmt\Use_;
use PhpParser\PrettyPrinter\Standard;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
Expand Down Expand Up @@ -363,28 +362,6 @@ protected function pStmt_ClassMethod(ClassMethod $classMethod): string
return Strings::replace($content, self::REPLACE_COLON_WITH_SPACE_REGEX, '$1: ');
}

/**
* Clean class and trait from empty "use x;" for traits causing invalid code
*/
protected function pStmt_Class(Class_ $class): string
{
$shouldReindex = false;

foreach ($class->stmts as $key => $stmt) {
// remove empty ones
if ($stmt instanceof TraitUse && $stmt->traits === []) {
unset($class->stmts[$key]);
$shouldReindex = true;
}
}

if ($shouldReindex) {
$class->stmts = array_values($class->stmts);
}

return parent::pStmt_Class($class);
}

/**
* It remove all spaces extra to parent
*/
Expand Down

0 comments on commit a8f322a

Please sign in to comment.