mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-22 00:12:29 +02:00
add support for false/true
This commit is contained in:
parent
cf5129c10b
commit
036d7b9940
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -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',
|
||||
|
@ -14,7 +14,7 @@ final class TypeAnalyzer
|
||||
public static function isPhpReservedType(string $type): bool
|
||||
{
|
||||
return in_array(
|
||||
$type,
|
||||
strtolower($type),
|
||||
[
|
||||
'string',
|
||||
'bool',
|
||||
|
Loading…
x
Reference in New Issue
Block a user