mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-22 18:54:39 +01:00
60 lines
1.8 KiB
PHP
60 lines
1.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Rector\Laravel\NodeFactory;
|
|
|
|
use PhpParser\Node\Expr;
|
|
use PhpParser\Node\Expr\Assign;
|
|
use PhpParser\Node\Expr\Variable;
|
|
use PhpParser\Node\Stmt\Expression;
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
|
use Rector\AttributeAwarePhpDoc\Ast\Type\FullyQualifiedIdentifierTypeNode;
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
|
use Rector\Laravel\ValueObject\ServiceNameTypeAndVariableName;
|
|
|
|
final class AppAssignFactory
|
|
{
|
|
/**
|
|
* @var PhpDocInfoFactory
|
|
*/
|
|
private $phpDocInfoFactory;
|
|
|
|
public function __construct(PhpDocInfoFactory $phpDocInfoFactory)
|
|
{
|
|
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
|
}
|
|
|
|
public function createAssignExpression(
|
|
ServiceNameTypeAndVariableName $serviceNameTypeAndVariableName,
|
|
Expr $expr
|
|
): Expression {
|
|
$variable = new Variable($serviceNameTypeAndVariableName->getVariableName());
|
|
$assign = new Assign($variable, $expr);
|
|
$expression = new Expression($assign);
|
|
|
|
$this->decorateWithVarAnnotation($expression, $serviceNameTypeAndVariableName);
|
|
|
|
return $expression;
|
|
}
|
|
|
|
private function decorateWithVarAnnotation(
|
|
Expression $expression,
|
|
ServiceNameTypeAndVariableName $serviceNameTypeAndVariableName
|
|
): void {
|
|
$phpDocInfo = $this->phpDocInfoFactory->createEmpty($expression);
|
|
|
|
$fullyQualifiedIdentifierTypeNode = new FullyQualifiedIdentifierTypeNode(
|
|
$serviceNameTypeAndVariableName->getType()
|
|
);
|
|
|
|
$varTagValueNode = new VarTagValueNode(
|
|
$fullyQualifiedIdentifierTypeNode,
|
|
'$' . $serviceNameTypeAndVariableName->getVariableName(),
|
|
''
|
|
);
|
|
|
|
$phpDocInfo->addTagValueNode($varTagValueNode);
|
|
$phpDocInfo->makeSingleLined();
|
|
}
|
|
}
|