2020-01-28 16:39:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-02-10 09:23:01 +00:00
|
|
|
namespace Rector\StaticTypeMapper\Mapper;
|
2020-01-28 16:39:05 +00:00
|
|
|
|
|
|
|
use Nette\Utils\Strings;
|
|
|
|
use PHPStan\Type\ArrayType;
|
|
|
|
use PHPStan\Type\BooleanType;
|
|
|
|
use PHPStan\Type\CallableType;
|
|
|
|
use PHPStan\Type\FloatType;
|
|
|
|
use PHPStan\Type\IntegerType;
|
2020-10-28 20:42:17 +00:00
|
|
|
use PHPStan\Type\IterableType;
|
2020-01-28 16:39:05 +00:00
|
|
|
use PHPStan\Type\MixedType;
|
|
|
|
use PHPStan\Type\NullType;
|
|
|
|
use PHPStan\Type\ObjectWithoutClassType;
|
|
|
|
use PHPStan\Type\ResourceType;
|
|
|
|
use PHPStan\Type\StringType;
|
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use PHPStan\Type\VoidType;
|
2021-01-18 19:23:10 +00:00
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\FalseBooleanType;
|
2020-01-28 16:39:05 +00:00
|
|
|
|
|
|
|
final class ScalarStringToTypeMapper
|
|
|
|
{
|
2020-02-10 09:23:01 +00:00
|
|
|
/**
|
|
|
|
* @var string[][]
|
|
|
|
*/
|
|
|
|
private const SCALAR_NAME_BY_TYPE = [
|
|
|
|
StringType::class => ['string'],
|
|
|
|
FloatType::class => ['float', 'real', 'double'],
|
|
|
|
IntegerType::class => ['int', 'integer'],
|
2021-01-18 19:23:10 +00:00
|
|
|
FalseBooleanType::class => ['false'],
|
|
|
|
BooleanType::class => ['true', 'bool', 'boolean'],
|
2020-02-10 09:23:01 +00:00
|
|
|
NullType::class => ['null'],
|
|
|
|
VoidType::class => ['void'],
|
|
|
|
ResourceType::class => ['resource'],
|
|
|
|
CallableType::class => ['callback', 'callable'],
|
|
|
|
ObjectWithoutClassType::class => ['object'],
|
|
|
|
];
|
|
|
|
|
2020-01-28 16:39:05 +00:00
|
|
|
public function mapScalarStringToType(string $scalarName): Type
|
|
|
|
{
|
|
|
|
$loweredScalarName = Strings::lower($scalarName);
|
|
|
|
|
2020-02-10 09:23:01 +00:00
|
|
|
foreach (self::SCALAR_NAME_BY_TYPE as $objectType => $scalarNames) {
|
2020-01-28 16:39:05 +00:00
|
|
|
if (! in_array($loweredScalarName, $scalarNames, true)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new $objectType();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($loweredScalarName === 'array') {
|
|
|
|
return new ArrayType(new MixedType(), new MixedType());
|
|
|
|
}
|
|
|
|
|
2020-10-28 20:42:17 +00:00
|
|
|
if ($loweredScalarName === 'iterable') {
|
|
|
|
return new IterableType(new MixedType(), new MixedType());
|
|
|
|
}
|
|
|
|
|
2020-01-28 16:39:05 +00:00
|
|
|
if ($loweredScalarName === 'mixed') {
|
|
|
|
return new MixedType(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new MixedType();
|
|
|
|
}
|
|
|
|
}
|