Skip to content

Commit

Permalink
make rules inherit from abstract falsy
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Oct 5, 2021
1 parent 78cf16e commit 9eb5fe3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
15 changes: 10 additions & 5 deletions rules/Strict/Rector/If_/BooleanInIfConditionRuleFixerRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
namespace Rector\Strict\Rector\If_;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\If_;
use PHPStan\Analyser\Scope;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Strict\NodeFactory\ExactCompareFactory;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
Expand All @@ -20,7 +21,7 @@
*
* @see \Rector\Tests\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector\BooleanInIfConditionRuleFixerRectorTest
*/
final class BooleanInIfConditionRuleFixerRector extends AbstractRector
final class BooleanInIfConditionRuleFixerRector extends AbstractFalsyScalarRuleFixerRector
{
public function __construct(
private ExactCompareFactory $exactCompareFactory
Expand All @@ -34,7 +35,7 @@ public function getRuleDefinition(): RuleDefinition
'PHPStan\Rules\BooleansInConditions\BooleanInIfConditionRule'
);
return new RuleDefinition($errorMessage, [
new CodeSample(
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
final class NegatedString
{
Expand Down Expand Up @@ -62,6 +63,10 @@ public function run(string $name)
}
}
CODE_SAMPLE
,
[
self::TREAT_AS_NON_EMPTY => false,
]
),
]);
}
Expand Down Expand Up @@ -98,7 +103,7 @@ public function refactor(Node $node): ?If_
$elseifCondExprType,
$elseif->cond
);
if ($notIdentical === null) {
if (! $notIdentical instanceof Expr) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Ternary;
use PHPStan\Analyser\Scope;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Strict\NodeFactory\ExactCompareFactory;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
Expand All @@ -20,7 +20,7 @@
*
* @see \Rector\Tests\Strict\Rector\Ternary\BooleanInTernaryOperatorRuleFixerRector\BooleanInTernaryOperatorRuleFixerRectorTest
*/
final class BooleanInTernaryOperatorRuleFixerRector extends AbstractRector
final class BooleanInTernaryOperatorRuleFixerRector extends AbstractFalsyScalarRuleFixerRector
{
public function __construct(
private ExactCompareFactory $exactCompareFactory
Expand All @@ -34,7 +34,7 @@ public function getRuleDefinition(): RuleDefinition
'PHPStan\Rules\BooleansInConditions\BooleanInTernaryOperatorRule'
);
return new RuleDefinition($errorMessage, [
new CodeSample(
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
final class ArrayCompare
{
Expand All @@ -54,6 +54,10 @@ public function run(array $data)
}
}
CODE_SAMPLE
,
[
self::TREAT_AS_NON_EMPTY => false,
]
),
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
namespace Rector\Strict\Rector\Ternary;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Ternary;
use PHPStan\Analyser\Scope;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Strict\NodeFactory\ExactCompareFactory;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
Expand All @@ -20,7 +21,7 @@
*
* @see \Rector\Tests\Strict\Rector\Ternary\DisallowedShortTernaryRuleFixerRector\DisallowedShortTernaryRuleFixerRectorTest
*/
final class DisallowedShortTernaryRuleFixerRector extends AbstractRector
final class DisallowedShortTernaryRuleFixerRector extends AbstractFalsyScalarRuleFixerRector
{
public function __construct(
private ExactCompareFactory $exactCompareFactory,
Expand All @@ -34,7 +35,7 @@ public function getRuleDefinition(): RuleDefinition
'PHPStan\Rules\DisallowedConstructs\DisallowedShortTernaryRule'
);
return new RuleDefinition($errorMessage, [
new CodeSample(
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
final class ShortTernaryArray
{
Expand All @@ -54,6 +55,10 @@ public function run(array $array)
}
}
CODE_SAMPLE
,
[
self::TREAT_AS_NON_EMPTY => false,
]
),
]);
}
Expand Down Expand Up @@ -88,13 +93,13 @@ public function refactor(Node $node): ?Ternary
}

$exprType = $scope->getType($node->cond);
$falsyIdentical = $this->exactCompareFactory->createNotIdenticalFalsyCompare($exprType, $node->cond);
if ($falsyIdentical === null) {
$compareExpr = $this->exactCompareFactory->createNotIdenticalFalsyCompare($exprType, $node->cond);
if (! $compareExpr instanceof Expr) {
return null;
}

$node->if = $node->cond;
$node->cond = $falsyIdentical;
$node->cond = $compareExpr;

return $node;
}
Expand All @@ -111,7 +116,7 @@ private function refactorResetFuncCall(Ternary $ternary, FuncCall $resetFuncCall
$firstArgValue
);

if ($falsyCompareExpr === null) {
if (! $falsyCompareExpr instanceof Expr) {
return;
}

Expand Down

0 comments on commit 9eb5fe3

Please sign in to comment.