mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-19 14:27:14 +01:00
[SOLID] Add AddFalseDefaultToBoolPropertyRector
This commit is contained in:
parent
cae1c0a727
commit
653e5353bb
@ -4,3 +4,4 @@ services:
|
||||
Rector\SOLID\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector: null
|
||||
Rector\SOLID\Rector\ClassMethod\ChangeReadOnlyVariableWithDefaultValueToConstantRector: null
|
||||
Rector\SOLID\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector: null
|
||||
Rector\SOLID\Rector\Property\AddFalseDefaultToBoolPropertyRector: null
|
||||
|
@ -1,4 +1,4 @@
|
||||
# All 509 Rectors Overview
|
||||
# All 510 Rectors Overview
|
||||
|
||||
- [Projects](#projects)
|
||||
- [General](#general)
|
||||
@ -9096,6 +9096,26 @@ Remove final from Doctrine entities
|
||||
|
||||
## SOLID
|
||||
|
||||
### `AddFalseDefaultToBoolPropertyRector`
|
||||
|
||||
- class: [`Rector\SOLID\Rector\Property\AddFalseDefaultToBoolPropertyRector`](/../master/rules/solid/src/Rector/Property/AddFalseDefaultToBoolPropertyRector.php)
|
||||
- [test fixtures](/../master/rules/solid/tests/Rector/Property/AddFalseDefaultToBoolPropertyRector/Fixture)
|
||||
|
||||
Add false default to bool properties, to prevent null compare errors
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
- private $isDisabled;
|
||||
+ private $isDisabled = false;
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### `ChangeIfElseValueAssignToEarlyReturnRector`
|
||||
|
||||
- class: [`Rector\SOLID\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector`](/../master/rules/solid/src/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php)
|
||||
|
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\SOLID\Rector\Property;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PHPStan\Type\BooleanType;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
/**
|
||||
* @see \Rector\SOLID\Tests\Rector\Property\AddFalseDefaultToBoolPropertyRector\AddFalseDefaultToBoolPropertyRectorTest
|
||||
*/
|
||||
final class AddFalseDefaultToBoolPropertyRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Add false default to bool properties, to prevent null compare errors', [
|
||||
new CodeSample(
|
||||
<<<'PHP'
|
||||
class SomeClass
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $isDisabled;
|
||||
}
|
||||
PHP
|
||||
,
|
||||
<<<'PHP'
|
||||
class SomeClass
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $isDisabled = false;
|
||||
}
|
||||
PHP
|
||||
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Property::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Property $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (count($node->props) !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$onlyProperty = $node->props[0];
|
||||
if ($onlyProperty->default !== null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var PhpDocInfo|null $phpDocInfo */
|
||||
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
|
||||
if (! $phpDocInfo->getVarType() instanceof BooleanType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$onlyProperty->default = $this->createFalse();
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\SOLID\Tests\Rector\Property\AddFalseDefaultToBoolPropertyRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Rector\SOLID\Rector\Property\AddFalseDefaultToBoolPropertyRector;
|
||||
|
||||
final class AddFalseDefaultToBoolPropertyRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(string $file): void
|
||||
{
|
||||
$this->doTestFile($file);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return AddFalseDefaultToBoolPropertyRector::class;
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\SOLID\Tests\Rector\Property\AddFalseDefaultToBoolPropertyRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $isDisabled;
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\SOLID\Tests\Rector\Property\AddFalseDefaultToBoolPropertyRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $isDisabled = false;
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\SOLID\Tests\Rector\Property\AddFalseDefaultToBoolPropertyRector\Fixture;
|
||||
|
||||
class SkipDefault
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $isDisabled = true;
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\SOLID\Tests\Rector\Property\AddFalseDefaultToBoolPropertyRector\Fixture;
|
||||
|
||||
class SkipString
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $isDisabled;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user