rector/rules/Defluent/ValueObject/AssignAndRootExpr.php

82 lines
2.0 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Rector\Defluent\ValueObject;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
2020-07-06 01:57:19 +02:00
use PhpParser\Node\Expr\Variable;
2020-06-30 11:01:20 +02:00
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Defluent\Contract\ValueObject\FirstCallFactoryAwareInterface;
use Rector\Defluent\Contract\ValueObject\RootExprAwareInterface;
final class AssignAndRootExpr extends AbstractRootExpr implements RootExprAwareInterface, FirstCallFactoryAwareInterface
{
2020-06-30 11:01:20 +02:00
/**
* @var Variable|null
2020-06-30 11:01:20 +02:00
*/
private $silentVariable;
2020-08-12 20:24:52 +02:00
public function __construct(
Expr $assignExpr,
Expr $rootExpr,
?Variable $silentVariable = null,
bool $isFirstCallFactory = false
) {
$this->assignExpr = $assignExpr;
$this->rootExpr = $rootExpr;
2020-06-30 11:01:20 +02:00
$this->silentVariable = $silentVariable;
2020-08-12 20:24:52 +02:00
$this->isFirstCallFactory = $isFirstCallFactory;
}
public function getAssignExpr(): Expr
{
return $this->assignExpr;
}
public function getRootExpr(): Expr
{
return $this->rootExpr;
}
2020-06-30 11:01:20 +02:00
2020-07-06 01:57:19 +02:00
public function getSilentVariable(): ?Variable
2020-06-30 11:01:20 +02:00
{
return $this->silentVariable;
}
public function getReturnSilentVariable(): Return_
{
if (! $this->silentVariable instanceof Variable) {
2020-06-30 11:01:20 +02:00
throw new ShouldNotHappenException();
}
return new Return_($this->silentVariable);
}
public function getCallerExpr(): Expr
{
if ($this->silentVariable !== null) {
return $this->silentVariable;
}
return $this->assignExpr;
}
2020-08-12 20:24:52 +02:00
public function isFirstCallFactory(): bool
{
return $this->isFirstCallFactory;
}
public function getFactoryAssignVariable(): Expr
{
$firstAssign = $this->getFirstAssign();
if (! $firstAssign instanceof Assign) {
2020-08-12 20:24:52 +02:00
return $this->getCallerExpr();
}
return $firstAssign->var;
}
}