mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 19:53:14 +01:00
fix nullable for scalar to null
This commit is contained in:
parent
04e0de2429
commit
46f479e818
@ -10,6 +10,7 @@ use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
||||
use PhpParser\Node\Expr\BooleanNot;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PHPStan\Type\ArrayType;
|
||||
use PHPStan\Type\BooleanType;
|
||||
use PHPStan\Type\FloatType;
|
||||
use PHPStan\Type\IntegerType;
|
||||
use PHPStan\Type\MixedType;
|
||||
@ -33,7 +34,7 @@ final class NullableCompareToNullRector extends AbstractRector
|
||||
new CodeSample(
|
||||
<<<'PHP'
|
||||
/** @var stdClass|null $value */
|
||||
if ($value) {
|
||||
if ($value) {
|
||||
}
|
||||
|
||||
if (!$value) {
|
||||
@ -113,6 +114,12 @@ PHP
|
||||
if ($staticType->isSuperTypeOf(new IntegerType())->yes()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// is bool?
|
||||
if ($staticType->isSuperTypeOf(new BooleanType())->yes()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ! $staticType->isSuperTypeOf(new FloatType())->yes();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\If_\NullableCompareToNullRector\Fixture;
|
||||
|
||||
class KeepNullableBool
|
||||
{
|
||||
public function run(?bool $boolOrNull)
|
||||
{
|
||||
if (! $boolOrNull) {
|
||||
return 'no item nor null';
|
||||
}
|
||||
}
|
||||
}
|
@ -20,10 +20,7 @@ final class NullableCompareToNullRectorTest extends AbstractRectorTestCase
|
||||
|
||||
public function provideDataForTest(): Iterator
|
||||
{
|
||||
yield [__DIR__ . '/Fixture/fixture.php.inc'];
|
||||
yield [__DIR__ . '/Fixture/fixture2.php.inc'];
|
||||
yield [__DIR__ . '/Fixture/fixture3.php.inc'];
|
||||
yield [__DIR__ . '/Fixture/nullable_scalars.php.inc'];
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
|
Loading…
x
Reference in New Issue
Block a user