rector/rules/RemovingStatic/NodeFactory/SelfContainerFactory.php
Tomas Votruba cdc3b7adef Updated Rector to commit f451b0b8e1e6761ec7f50809745d44d01caba66d
f451b0b8e1 [PHP 8.0] Bump to promoted properties (#4)
2021-05-10 23:39:21 +00:00

36 lines
1.4 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\RemovingStatic\NodeFactory;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Name;
use PHPStan\Type\ObjectType;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\StaticTypeMapper\StaticTypeMapper;
final class SelfContainerFactory
{
/**
* @var \Rector\StaticTypeMapper\StaticTypeMapper
*/
private $staticTypeMapper;
public function __construct(\Rector\StaticTypeMapper\StaticTypeMapper $staticTypeMapper)
{
$this->staticTypeMapper = $staticTypeMapper;
}
public function createGetTypeMethodCall(\PHPStan\Type\ObjectType $objectType) : \PhpParser\Node\Expr\MethodCall
{
$staticPropertyFetch = new \PhpParser\Node\Expr\StaticPropertyFetch(new \PhpParser\Node\Name('self'), 'container');
$getMethodCall = new \PhpParser\Node\Expr\MethodCall($staticPropertyFetch, 'get');
$className = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($objectType);
if (!$className instanceof \PhpParser\Node\Name) {
throw new \Rector\Core\Exception\ShouldNotHappenException();
}
$getMethodCall->args[] = new \PhpParser\Node\Arg(new \PhpParser\Node\Expr\ClassConstFetch($className, 'class'));
return $getMethodCall;
}
}