Skip to content

Commit ac7227d

Browse files
committed
[depre] Deprecate DisallowedShortTernaryRuleFixerRector as risky and not practical
1 parent 9970314 commit ac7227d

10 files changed

Lines changed: 10 additions & 223 deletions

File tree

config/set/strict-booleans.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
use Rector\Config\RectorConfig;
66
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
7-
use Rector\Strict\Rector\Ternary\DisallowedShortTernaryRuleFixerRector;
87

98
return static function (RectorConfig $rectorConfig): void {
10-
$rectorConfig->rules([DisallowedEmptyRuleFixerRector::class, DisallowedShortTernaryRuleFixerRector::class]);
9+
$rectorConfig->rules([DisallowedEmptyRuleFixerRector::class]);
1110
};

rules-tests/Strict/Rector/Ternary/DisallowedShortTernaryRuleFixerRector/DisallowedShortTernaryRuleFixerRectorTest.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

rules-tests/Strict/Rector/Ternary/DisallowedShortTernaryRuleFixerRector/Fixture/reset_trait.php.inc

Lines changed: 0 additions & 31 deletions
This file was deleted.

rules-tests/Strict/Rector/Ternary/DisallowedShortTernaryRuleFixerRector/Fixture/short_ternary_array.php.inc

Lines changed: 0 additions & 31 deletions
This file was deleted.

rules-tests/Strict/Rector/Ternary/DisallowedShortTernaryRuleFixerRector/Fixture/short_ternary_string.php.inc

Lines changed: 0 additions & 31 deletions
This file was deleted.

rules-tests/Strict/Rector/Ternary/DisallowedShortTernaryRuleFixerRector/Fixture/skip_docblock.php.inc

Lines changed: 0 additions & 16 deletions
This file was deleted.

rules-tests/Strict/Rector/Ternary/DisallowedShortTernaryRuleFixerRector/config/configured_rule.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

rules/Strict/Rector/Ternary/DisallowedShortTernaryRuleFixerRector.php

Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,22 @@
55
namespace Rector\Strict\Rector\Ternary;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\Expr;
9-
use PhpParser\Node\Expr\FuncCall;
108
use PhpParser\Node\Expr\Ternary;
11-
use PHPStan\Analyser\Scope;
12-
use Rector\PHPStan\ScopeFetcher;
13-
use Rector\Strict\NodeFactory\ExactCompareFactory;
14-
use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector;
9+
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
10+
use Rector\Exception\ShouldNotHappenException;
11+
use Rector\Rector\AbstractRector;
1512
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
1613
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1714

1815
/**
1916
* Fixer Rector for PHPStan rule:
2017
* https://github.com/phpstan/phpstan-strict-rules/blob/master/src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php
2118
*
22-
* @see \Rector\Tests\Strict\Rector\Ternary\DisallowedShortTernaryRuleFixerRector\DisallowedShortTernaryRuleFixerRectorTest
19+
* @deprecated as risky and not practical
2320
*/
24-
final class DisallowedShortTernaryRuleFixerRector extends AbstractFalsyScalarRuleFixerRector
21+
final class DisallowedShortTernaryRuleFixerRector extends AbstractRector implements DeprecatedInterface
2522
{
26-
private bool $hasChanged = false;
27-
28-
public function __construct(
29-
private readonly ExactCompareFactory $exactCompareFactory,
30-
) {
31-
}
23+
public const TREAT_AS_NON_EMPTY = 'treat_as_non_empty';
3224

3325
public function getRuleDefinition(): RuleDefinition
3426
{
@@ -78,65 +70,6 @@ public function getNodeTypes(): array
7870
*/
7971
public function refactor(Node $node): ?Ternary
8072
{
81-
$this->hasChanged = false;
82-
83-
// skip non-short ternary
84-
if ($node->if instanceof Expr) {
85-
return null;
86-
}
87-
88-
$scope = ScopeFetcher::fetch($node);
89-
90-
// special case for reset() function
91-
if ($node->cond instanceof FuncCall && $this->isName($node->cond, 'reset')) {
92-
$this->refactorResetFuncCall($node, $node->cond, $scope);
93-
94-
if (! $this->hasChanged) {
95-
return null;
96-
}
97-
98-
return $node;
99-
}
100-
101-
$exprType = $scope->getNativeType($node->cond);
102-
$compareExpr = $this->exactCompareFactory->createNotIdenticalFalsyCompare(
103-
$exprType,
104-
$node->cond,
105-
$this->treatAsNonEmpty
106-
);
107-
if (! $compareExpr instanceof Expr) {
108-
return null;
109-
}
110-
111-
$node->if = $node->cond;
112-
$node->cond = $compareExpr;
113-
114-
return $node;
115-
}
116-
117-
private function refactorResetFuncCall(Ternary $ternary, FuncCall $resetFuncCall, Scope $scope): void
118-
{
119-
$ternary->if = $ternary->cond;
120-
121-
if ($resetFuncCall->isFirstClassCallable()) {
122-
return;
123-
}
124-
125-
$firstArgValue = $resetFuncCall->getArgs()[0]
126-
->value;
127-
$firstArgType = $scope->getNativeType($firstArgValue);
128-
129-
$falsyCompareExpr = $this->exactCompareFactory->createNotIdenticalFalsyCompare(
130-
$firstArgType,
131-
$firstArgValue,
132-
$this->treatAsNonEmpty
133-
);
134-
135-
if (! $falsyCompareExpr instanceof Expr) {
136-
return;
137-
}
138-
139-
$ternary->cond = $falsyCompareExpr;
140-
$this->hasChanged = true;
73+
throw new ShouldNotHappenException('This rule is deprecated as risky and not practical');
14174
}
14275
}

src/Configuration/RectorConfigBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ public function withPreparedSets(
760760
bool $naming = false,
761761
bool $instanceOf = false,
762762
bool $earlyReturn = false,
763+
// @deprecated
763764
bool $strictBooleans = false,
764765
bool $carbon = false,
765766
bool $rectorPreset = false,
@@ -780,7 +781,6 @@ public function withPreparedSets(
780781
SetList::NAMING => $naming,
781782
SetList::INSTANCEOF => $instanceOf,
782783
SetList::EARLY_RETURN => $earlyReturn,
783-
SetList::STRICT_BOOLEANS => $strictBooleans,
784784
SetList::CARBON => $carbon,
785785
SetList::RECTOR_PRESET => $rectorPreset,
786786
PHPUnitSetList::PHPUNIT_CODE_QUALITY => $phpunitCodeQuality,

src/Set/ValueObject/SetList.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ final class SetList
3131
public const DEAD_CODE = __DIR__ . '/../../../config/set/dead-code.php';
3232

3333
/**
34+
* @deprecated As too strict and not practical. Use code quality and coding style sets instead.
3435
* @var string
3536
*/
3637
public const STRICT_BOOLEANS = __DIR__ . '/../../../config/set/strict-booleans.php';

0 commit comments

Comments
 (0)