fix nullable for scalar to null

This commit is contained in:
Tomas Votruba 2019-11-05 13:22:53 +01:00
parent 04e0de2429
commit 46f479e818
3 changed files with 22 additions and 5 deletions

View File

@ -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();
}
}

View File

@ -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';
}
}
}

View File

@ -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