2020-06-28 18:22:34 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-10-14 06:24:29 +02:00
|
|
|
namespace Rector\Defluent\ValueObject;
|
2020-06-28 18:22:34 +02:00
|
|
|
|
|
|
|
use PhpParser\Node\Expr;
|
2021-01-20 18:41:35 +07:00
|
|
|
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;
|
2020-10-14 06:24:29 +02:00
|
|
|
use Rector\Defluent\Contract\ValueObject\FirstCallFactoryAwareInterface;
|
|
|
|
use Rector\Defluent\Contract\ValueObject\RootExprAwareInterface;
|
2020-06-28 18:22:34 +02:00
|
|
|
|
2020-12-21 03:12:42 +01:00
|
|
|
final class AssignAndRootExpr extends AbstractRootExpr implements RootExprAwareInterface, FirstCallFactoryAwareInterface
|
2020-06-28 18:22:34 +02:00
|
|
|
{
|
2020-06-30 11:01:20 +02:00
|
|
|
/**
|
2020-07-31 14:23:43 +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
|
|
|
|
) {
|
2020-06-28 18:22:34 +02:00
|
|
|
$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;
|
2020-06-28 18:22:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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_
|
|
|
|
{
|
2021-01-20 18:41:35 +07:00
|
|
|
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();
|
2021-01-20 18:41:35 +07:00
|
|
|
if (! $firstAssign instanceof Assign) {
|
2020-08-12 20:24:52 +02:00
|
|
|
return $this->getCallerExpr();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $firstAssign->var;
|
|
|
|
}
|
2020-06-28 18:22:34 +02:00
|
|
|
}
|