Skip to content

Commit 7e36ecf

Browse files
cleanup (#354)
* cleanup * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <[email protected]>
1 parent 26caf7c commit 7e36ecf

File tree

2 files changed

+14
-31
lines changed

2 files changed

+14
-31
lines changed

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,10 @@ composer require rector/rector --dev
1919
To add a set to your config, use `Rector\Set\ValueObject\DowngradeLevelSetList` class and pick target set:
2020

2121
```php
22-
use Rector\Set\ValueObject\DowngradeLevelSetList;
2322
use Rector\Config\RectorConfig;
2423

25-
return static function (RectorConfig $rectorConfig): void {
26-
$rectorConfig->sets([
27-
DowngradeLevelSetList::DOWN_TO_PHP_72
28-
]);
29-
};
24+
return RectorConfig::configure()
25+
->withDowngradeSets(php72: true);
3026
```
3127

3228
Then run Rector to downgrade your code to PHP 7.2!

rules/DowngradePhp73/Rector/ConstFetch/DowngradePhp73JsonConstRector.php

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,8 @@ public function refactor(Node $node): null|Expr|array
102102
return null;
103103
}
104104

105-
// if ($node instanceof TryCatch) {
106-
// SimpleNodeTraverser::decorateWithAttributeValue($node->stmts, self::IS_EXPRESSION_INSIDE_TRY_CATCH, true);
107-
// return null;
108-
// }
109-
110105
if ($node instanceof Expression) {
111-
return $this->refactorStmt($node);
106+
return $this->refactorExpression($node);
112107
}
113108

114109
return $this->jsonConstCleaner->clean($node, [JsonConstant::THROW_ON_ERROR]);
@@ -154,14 +149,14 @@ private function resolveFuncCall(Expression $Expression): ?FuncCall
154149
*
155150
* @return null|array<Expression|If_>
156151
*/
157-
private function refactorStmt(Expression $Expression): ?array
152+
private function refactorExpression(Expression $expression): ?array
158153
{
159-
if ($Expression->getAttribute(AttributeKey::IS_IN_TRY_BLOCK) === true) {
154+
if ($expression->getAttribute(AttributeKey::IS_IN_TRY_BLOCK) === true) {
160155
return null;
161156
}
162157

163158
// retrieve a `FuncCall`, if any, from the statement
164-
$funcCall = $this->resolveFuncCall($Expression);
159+
$funcCall = $this->resolveFuncCall($expression);
165160

166161
// Nothing to do if no `FuncCall` found
167162
if (! $funcCall instanceof FuncCall) {
@@ -178,7 +173,7 @@ private function refactorStmt(Expression $Expression): ?array
178173
return null;
179174
}
180175

181-
$nodes = [$Expression];
176+
$nodes = [$expression];
182177
$nodes[] = new If_(
183178
new NotIdentical(
184179
new FuncCall(new Name('json_last_error')),
@@ -228,26 +223,18 @@ private function hasConstFetchInArgs(array $args, string $constName): bool
228223
/**
229224
* Search if a given constant is set within a `BitwiseOr`
230225
*/
231-
private function hasConstFetchInBitwiseOr(BitwiseOr $bitwiseOr, string $constName): bool
226+
private function hasConstFetchInBitwiseOr(BitwiseOr $bitwiseOr, string $constantName): bool
232227
{
233-
$found = false;
234-
235228
foreach ([$bitwiseOr->left, $bitwiseOr->right] as $subNode) {
236-
$found = match (true) {
237-
$subNode instanceof BitwiseOr => (
238-
$this->hasConstFetchInBitwiseOr($subNode, $constName)
239-
),
240-
$subNode instanceof ConstFetch => (
241-
$this->getName($subNode) === $constName
242-
),
243-
default => false
244-
};
229+
if ($subNode instanceof BitwiseOr && $this->hasConstFetchInBitwiseOr($subNode, $constantName)) {
230+
return true;
231+
}
245232

246-
if ($found) {
247-
break;
233+
if ($subNode instanceof ConstFetch && $this->isName($subNode, $constantName)) {
234+
return true;
248235
}
249236
}
250237

251-
return $found;
238+
return false;
252239
}
253240
}

0 commit comments

Comments
 (0)