rector/vendor/rector/rector-nette/src/Kdyby/BlueprintFactory/VariableWithTypesFactory.php
Tomas Votruba 40f2bcdb1e Updated Rector to commit 29b79786e2
29b79786e2 add getStringValue() to value resolver (#1130)
2021-11-02 15:11:40 +00:00

58 lines
2.2 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\Nette\Kdyby\BlueprintFactory;
use PhpParser\Node\Arg;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Naming\Naming\VariableNaming;
use Rector\Nette\Kdyby\ValueObject\VariableWithType;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\StaticTypeMapper\StaticTypeMapper;
final class VariableWithTypesFactory
{
/**
* @var \Rector\NodeTypeResolver\NodeTypeResolver
*/
private $nodeTypeResolver;
/**
* @var \Rector\StaticTypeMapper\StaticTypeMapper
*/
private $staticTypeMapper;
/**
* @var \Rector\Naming\Naming\VariableNaming
*/
private $variableNaming;
public function __construct(\Rector\NodeTypeResolver\NodeTypeResolver $nodeTypeResolver, \Rector\StaticTypeMapper\StaticTypeMapper $staticTypeMapper, \Rector\Naming\Naming\VariableNaming $variableNaming)
{
$this->nodeTypeResolver = $nodeTypeResolver;
$this->staticTypeMapper = $staticTypeMapper;
$this->variableNaming = $variableNaming;
}
/**
* @param Arg[] $args
* @return VariableWithType[]
*/
public function createVariablesWithTypesFromArgs(array $args) : array
{
$variablesWithTypes = [];
foreach ($args as $arg) {
$staticType = $this->nodeTypeResolver->getType($arg->value);
$variableName = $this->variableNaming->resolveFromNodeAndType($arg, $staticType);
if ($variableName === null) {
throw new \Rector\Core\Exception\ShouldNotHappenException();
}
// compensate for static
if ($staticType instanceof \PHPStan\Type\StaticType) {
$staticType = new \PHPStan\Type\ObjectType($staticType->getClassName());
}
$phpParserTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($staticType, \Rector\PHPStanStaticTypeMapper\Enum\TypeKind::PROPERTY());
$variablesWithTypes[] = new \Rector\Nette\Kdyby\ValueObject\VariableWithType($variableName, $staticType, $phpParserTypeNode);
}
return $variablesWithTypes;
}
}