Skip to content

Commit

Permalink
[TypeCoverage] Add EmptyOnNullableObjectToInstanceOfRector (#3230)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Dec 20, 2022
1 parent 394fa70 commit c80cd6f
Show file tree
Hide file tree
Showing 16 changed files with 345 additions and 196 deletions.
27 changes: 25 additions & 2 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 414 Rules Overview
# 415 Rules Overview

<br>

Expand Down Expand Up @@ -64,7 +64,7 @@

- [Transform](#transform) (34)

- [TypeDeclaration](#typedeclaration) (38)
- [TypeDeclaration](#typedeclaration) (39)

- [Visibility](#visibility) (3)

Expand Down Expand Up @@ -9170,6 +9170,29 @@ Add array shape exact types based on constant keys of array

<br>

### EmptyOnNullableObjectToInstanceOfRector

Change `empty()` on nullable object to instanceof check

- class: [`Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector`](../rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php)

```diff
class SomeClass
{
public function run(?AnotherObject $anotherObject)
{
- if (empty($anotherObject)) {
+ if (! $anotherObject instanceof AnotherObject) {
return false;
}

return true;
}
}
```

<br>

### FalseReturnClassMethodToNullableRector

Change class method that returns false as invalid state, to nullable
Expand Down
2 changes: 2 additions & 0 deletions config/set/type-declaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector;
use Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector;
use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;
use Rector\TypeDeclaration\Rector\FunctionLike\AddParamTypeSplFixedArrayRector;
use Rector\TypeDeclaration\Rector\FunctionLike\AddReturnTypeDeclarationFromYieldsRector;
use Rector\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector;
Expand Down Expand Up @@ -66,5 +67,6 @@
ReturnTypeFromStrictConstantReturnRector::class,
ReturnTypeFromStrictTypedCallRector::class,
ReturnNeverTypeRector::class,
EmptyOnNullableObjectToInstanceOfRector::class,
]);
};

This file was deleted.

This file was deleted.

This file was deleted.

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

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class EmptyOnNullableObjectToInstanceOfRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<string>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Fixture;

use Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\VariableTypeResolver\Source\AnotherType;

final class EmptyOnExactType
{
public function run()
{
$exactType = $this->getExactType();
if (empty($exactType)) {
return;
}
}

private function getExactType(): ?AnotherType
{
if (mt_rand(0, 1)) {
return new AnotherType();
}

return null;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Fixture;

use Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\VariableTypeResolver\Source\AnotherType;

final class EmptyOnExactType
{
public function run()
{
$exactType = $this->getExactType();
if (!$exactType instanceof \Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\VariableTypeResolver\Source\AnotherType) {
return;
}
}

private function getExactType(): ?AnotherType
{
if (mt_rand(0, 1)) {
return new AnotherType();
}

return null;
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Source\AnotherObject;

final class NegatedEmpty
{
public function run(?AnotherObject $anotherObject)
{
if (! empty($anotherObject)) {
return false;
}

return true;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Source\AnotherObject;

final class NegatedEmpty
{
public function run(?AnotherObject $anotherObject)
{
if ($anotherObject instanceof \Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Source\AnotherObject) {
return false;
}

return true;
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Source\AnotherObject;

final class SomeClass
{
public function run(?AnotherObject $anotherObject)
{
if (empty($anotherObject)) {
return false;
}

return true;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Source\AnotherObject;

final class SomeClass
{
public function run(?AnotherObject $anotherObject)
{
if (!$anotherObject instanceof \Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Source\AnotherObject) {
return false;
}

return true;
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Source;

final class AnotherObject
{

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

declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(EmptyOnNullableObjectToInstanceOfRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?array
return new Variable($variableName);
});

if ($countInCond === null) {
if (! $countInCond instanceof FuncCall) {
return null;
}

Expand Down
Loading

0 comments on commit c80cd6f

Please sign in to comment.