add support for false/true

This commit is contained in:
Tomas Votruba 2018-12-06 21:24:36 +01:00
parent cf5129c10b
commit 036d7b9940
4 changed files with 72 additions and 5 deletions

View File

@ -136,10 +136,8 @@ abstract class AbstractTypeInfo
foreach ($types as $i => $type) {
// convert: ?Type => Type, null
if (Strings::startsWith($type, '?')) {
$type = ltrim($type, '?');
$this->isNullable = true;
}
$type = $this->normalizeNullable($type);
$type = $this->normalizeCasing($type);
if ($type === 'null') {
unset($types[$i]);
@ -153,6 +151,11 @@ abstract class AbstractTypeInfo
continue;
}
if (in_array($type, ['true', 'false'], true)) {
$types[$i] = 'bool';
continue;
}
if ($type === '$this') {
$types[$i] = 'self';
continue;
@ -244,4 +247,26 @@ abstract class AbstractTypeInfo
return $types === $arraySubtypeGroup;
}
private function normalizeNullable(string $type): string
{
if (Strings::startsWith($type, '?')) {
$type = ltrim($type, '?');
$this->isNullable = true;
}
return $type;
}
private function normalizeCasing(string $type): string
{
if (TypeAnalyzer::isPhpReservedType($type)) {
return strtolower($type);
}
if (strtolower($type) === ['$this']) {
return strtolower($type);
}
return $type;
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Rector\Php\Tests\Rector\ClassMethod\ParamAndReturnScalarTypehintsRector\Integration;
class BoolClass
{
/**
* @param true $ojoj
* @param FALSE $ojoj2
* @param true|false $ojoj3
* @param null|true|false $ojoj4
* @param int|null|true|false $ojoj5
* @return false
*/
function someFunction($ojoj, $ojoj2, $ojoj3, $ojoj4, $ojoj5)
{
}
}
?>
-----
<?php
namespace Rector\Php\Tests\Rector\ClassMethod\ParamAndReturnScalarTypehintsRector\Integration;
class BoolClass
{
/**
* @param true $ojoj
* @param FALSE $ojoj2
* @param true|false $ojoj3
* @param null|true|false $ojoj4
* @param int|null|true|false $ojoj5
* @return false
*/
function someFunction(bool $ojoj, bool $ojoj2, bool $ojoj3, ?bool $ojoj4, $ojoj5): bool
{
}
}
?>

View File

@ -14,6 +14,7 @@ final class ParamAndReturnScalarTypehintsRectorTest extends AbstractRectorTestCa
{
$integrationFiles = [
__DIR__ . '/Fixture/this.php.inc',
__DIR__ . '/Fixture/false.php.inc',
__DIR__ . '/Fixture/undesired.php.inc',
__DIR__ . '/Fixture/aliased.php.inc',
__DIR__ . '/Fixture/external_scope.php.inc',

View File

@ -14,7 +14,7 @@ final class TypeAnalyzer
public static function isPhpReservedType(string $type): bool
{
return in_array(
$type,
strtolower($type),
[
'string',
'bool',