Skip to content

Commit

Permalink
NullableCompareToNullRector: Don't trust phpdocs (#5225)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Nov 6, 2023
1 parent 8615d40 commit 7607b33
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
<?php

/** @var stdClass|null $value */
$value = null;
if ($value) {
}
namespace Rector\Tests\CodingStyle\Rector\If_\NullableCompareToNullRector\Fixture;

function doFoo(?stdClass $value) {
if ($value) {
}

if (!$value) {
if (!$value) {
}
}

?>
-----
<?php

/** @var stdClass|null $value */
$value = null;
if ($value !== null) {
}
namespace Rector\Tests\CodingStyle\Rector\If_\NullableCompareToNullRector\Fixture;

function doFoo(?stdClass $value) {
if ($value !== null) {
}

if ($value === null) {
if ($value === null) {
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\If_\NullableCompareToNullRector\Fixture;

class SkipMixed
{
public function run(mixed $mixed)
{
if (! $mixed) {
return 'truethy mixed';
}

if ($mixed) {
return 'falsey mixed';
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

/** @var stdClass|null $value */
$value = null;
if ($value) {
}

if (!$value) {
}

?>
30 changes: 9 additions & 21 deletions rules/CodingStyle/Rector/If_/NullableCompareToNullRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\StringType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -84,43 +85,30 @@ public function refactor(Node $node): ?Node

private function isNullableNonScalarType(Expr $expr): bool
{
$staticType = $this->getType($expr);
if ($staticType instanceof MixedType) {
return false;
}
$nativeType = $this->nodeTypeResolver->getNativeType($expr);

if (! $staticType instanceof UnionType) {
// is non-nullable?
if (!TypeCombinator::containsNull($nativeType)) {
return false;
}

// is non-nullable?
if ($staticType->isSuperTypeOf(new NullType())->no()) {
if (! $nativeType instanceof UnionType) {
return false;
}

// is array?
foreach ($staticType->getTypes() as $subType) {
foreach ($nativeType->getTypes() as $subType) {
if ($subType->isArray()->yes()) {
return false;
}
}

// is string?
if ($staticType->isSuperTypeOf(new StringType())->yes()) {
return false;
}

// is number?
if ($staticType->isSuperTypeOf(new IntegerType())->yes()) {
return false;
}
$nativeType = TypeCombinator::removeNull($nativeType);

// is bool?
if ($staticType->isSuperTypeOf(new BooleanType())->yes()) {
if ($nativeType->isScalar()->yes()) {
return false;
}

return ! $staticType->isSuperTypeOf(new FloatType())
->yes();
return true;
}
}

0 comments on commit 7607b33

Please sign in to comment.