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

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,19 @@
namespace Rector\CodingStyle\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use Rector\CodingStyle\ValueObject\VariableAndExprAssign;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\CodingStyle\Rector\ClassMethod\BinaryOpStandaloneAssignsToDirectRector\BinaryOpStandaloneAssignsToDirectRectorTest
* @deprecated This rule is deprecated, as it targets a very specific shape of 3 statements only and its purpose is unclear.
*/
final class BinaryOpStandaloneAssignsToDirectRector extends AbstractRector implements MinPhpVersionInterface
final class BinaryOpStandaloneAssignsToDirectRector extends AbstractRector implements DeprecatedInterface
{
public function getRuleDefinition(): RuleDefinition
{
Expand Down Expand Up @@ -65,112 +57,9 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($node->stmts === null) {
return null;
}

if (count($node->stmts) !== 3) {
return null;
}

$firstStmt = $node->stmts[0];
$secondStmt = $node->stmts[1];
$thirdStmt = $node->stmts[2];

if (! $thirdStmt instanceof Return_) {
return null;
}

$firstVariableAndExprAssign = $this->matchToVariableAssignExpr($firstStmt);
if (! $firstVariableAndExprAssign instanceof VariableAndExprAssign) {
return null;
}

$secondVariableAndExprAssign = $this->matchToVariableAssignExpr($secondStmt);
if (! $secondVariableAndExprAssign instanceof VariableAndExprAssign) {
return null;
}

if (! $thirdStmt->expr instanceof BinaryOp) {
return null;
}

$binaryOp = $thirdStmt->expr;

if (! $this->nodeComparator->areNodesEqual($binaryOp->left, $firstVariableAndExprAssign->getVariable())) {
return null;
}

if (! $this->nodeComparator->areNodesEqual($binaryOp->right, $secondVariableAndExprAssign->getVariable())) {
return null;
}

$resolveParamByRefVariables = $this->resolveParamByRefVariables($node);
if ($this->isNames($binaryOp->left, $resolveParamByRefVariables)) {
return null;
}

if ($this->isNames($binaryOp->right, $resolveParamByRefVariables)) {
return null;
}

$binaryOp->left = $firstVariableAndExprAssign->getExpr();
$binaryOp->right = $secondVariableAndExprAssign->getExpr();

$node->stmts = [$thirdStmt];
return $node;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::VARIADIC_PARAM;
}

/**
* @return string[]
*/
private function resolveParamByRefVariables(ClassMethod|Function_|Closure $node): array
{
$paramByRefVariables = [];
foreach ($node->params as $param) {
if (! $param->var instanceof Variable) {
continue;
}

if (! $param->byRef) {
continue;
}

$paramByRefVariables[] = $this->getName($param);
}

return $paramByRefVariables;
}

private function matchToVariableAssignExpr(Stmt $stmt): ?VariableAndExprAssign
{
if (! $stmt instanceof Expression) {
return null;
}

if (! $stmt->expr instanceof Assign) {
return null;
}

$assign = $stmt->expr;
if (! $assign->var instanceof Variable) {
return null;
}

// skip complex cases
if ($assign->expr instanceof CallLike && ! $assign->expr->isFirstClassCallable() && $assign->expr->getArgs() !== []) {
return null;
}

if ($assign->expr instanceof BinaryOp) {
return null;
}

return new VariableAndExprAssign($assign->var, $assign->expr);
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as it covers a very specific shape of code with unclear purpose',
self::class
));
}
}
Loading
Loading