mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-04 02:10:51 +00:00
7d36c3b0b9
a80cf5292d
revert to working scoper 0.14
70 lines
2.0 KiB
PHP
70 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\CodingStyle\Rector\Property;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Stmt\Property;
|
|
use PHPStan\Type\BooleanType;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @see \Rector\Tests\CodingStyle\Rector\Property\AddFalseDefaultToBoolPropertyRector\AddFalseDefaultToBoolPropertyRectorTest
|
|
*/
|
|
final class AddFalseDefaultToBoolPropertyRector extends \Rector\Core\Rector\AbstractRector
|
|
{
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Add false default to bool properties, to prevent null compare errors', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
/**
|
|
* @var bool
|
|
*/
|
|
private $isDisabled;
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
/**
|
|
* @var bool
|
|
*/
|
|
private $isDisabled = false;
|
|
}
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Stmt\Property::class];
|
|
}
|
|
/**
|
|
* @param Property $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
if (\count($node->props) !== 1) {
|
|
return null;
|
|
}
|
|
$onlyProperty = $node->props[0];
|
|
if ($onlyProperty->default !== null) {
|
|
return null;
|
|
}
|
|
if (!$this->isBoolDocType($node)) {
|
|
return null;
|
|
}
|
|
$onlyProperty->default = $this->nodeFactory->createFalse();
|
|
return $node;
|
|
}
|
|
private function isBoolDocType(\PhpParser\Node\Stmt\Property $property) : bool
|
|
{
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
|
|
return $phpDocInfo->getVarType() instanceof \PHPStan\Type\BooleanType;
|
|
}
|
|
}
|