Skip to content

Commit

Permalink
Add BinaryOpAnalyzer locally
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 7, 2022
1 parent 86682e4 commit 81f3299
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rules/Php80/Rector/Identical/StrEndsWithRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use Rector\Core\NodeAnalyzer\BinaryOpAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Nette\NodeAnalyzer\BinaryOpAnalyzer;
use Rector\Nette\ValueObject\FuncCallAndExpr;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down
39 changes: 39 additions & 0 deletions src/NodeAnalyzer/BinaryOpAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Rector\Core\NodeAnalyzer;

use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\FuncCall;
use Rector\Core\ValueObject\FuncCallAndExpr;
use Rector\NodeNameResolver\NodeNameResolver;

final class BinaryOpAnalyzer
{
public function __construct(
private readonly NodeNameResolver $nodeNameResolver
) {
}

public function matchFuncCallAndOtherExpr(BinaryOp $binaryOp, string $funcCallName): ?FuncCallAndExpr
{
if ($binaryOp->left instanceof FuncCall) {
if (! $this->nodeNameResolver->isName($binaryOp->left, $funcCallName)) {
return null;
}

return new FuncCallAndExpr($binaryOp->left, $binaryOp->right);
}

if ($binaryOp->right instanceof FuncCall) {
if (! $this->nodeNameResolver->isName($binaryOp->right, $funcCallName)) {
return null;
}

return new FuncCallAndExpr($binaryOp->right, $binaryOp->left);
}

return null;
}
}
27 changes: 27 additions & 0 deletions src/ValueObject/FuncCallAndExpr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Rector\Core\ValueObject;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;

final class FuncCallAndExpr
{
public function __construct(
private readonly FuncCall $funcCall,
private readonly Expr $expr
) {
}

public function getFuncCall(): FuncCall
{
return $this->funcCall;
}

public function getExpr(): Expr
{
return $this->expr;
}
}

0 comments on commit 81f3299

Please sign in to comment.