Skip to content

Commit 54912ed

Browse files
committed
make use of native attribute
1 parent 5a4fd67 commit 54912ed

File tree

1 file changed

+14
-41
lines changed

1 file changed

+14
-41
lines changed

rules/DowngradePhp73/Rector/ConstFetch/DowngradePhp73JsonConstRector.php

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,19 @@
1010
use PhpParser\Node\Expr\Assign;
1111
use PhpParser\Node\Expr\BinaryOp\BitwiseOr;
1212
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
13-
use PhpParser\Node\Expr\Closure;
1413
use PhpParser\Node\Expr\ConstFetch;
1514
use PhpParser\Node\Expr\FuncCall;
1615
use PhpParser\Node\Expr\New_;
1716
use PhpParser\Node\Expr\Throw_;
1817
use PhpParser\Node\Name;
1918
use PhpParser\Node\Name\FullyQualified as NameFullyQualified;
20-
use PhpParser\Node\Stmt\Class_;
2119
use PhpParser\Node\Stmt\Expression;
22-
use PhpParser\Node\Stmt\Function_;
2320
use PhpParser\Node\Stmt\If_;
24-
use PhpParser\Node\Stmt\TryCatch;
2521
use PhpParser\Node\VariadicPlaceholder;
26-
use PhpParser\NodeVisitor;
2722
use Rector\DowngradePhp72\NodeManipulator\JsonConstCleaner;
2823
use Rector\Enum\JsonConstant;
2924
use Rector\NodeAnalyzer\DefineFuncCallAnalyzer;
25+
use Rector\NodeTypeResolver\Node\AttributeKey;
3026
use Rector\Rector\AbstractRector;
3127
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
3228
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -66,22 +62,17 @@ public function getRuleDefinition(): RuleDefinition
6662
[
6763
new CodeSample(
6864
<<<'CODE_SAMPLE'
69-
json_encode($content, JSON_THROW_ON_ERROR);
65+
$json = json_encode($content, JSON_THROW_ON_ERROR);
66+
67+
$content = json_decode($json, null, 512, JSON_THROW_ON_ERROR);
7068
CODE_SAMPLE
7169
,
7270
<<<'CODE_SAMPLE'
73-
json_encode($content, 0);
71+
$json json_encode($content, 0);
7472
if (json_last_error() !== JSON_ERROR_NONE) {
7573
throw new \Exception(json_last_error_msg());
7674
}
77-
CODE_SAMPLE
78-
),
79-
new CodeSample(
80-
<<<'CODE_SAMPLE'
81-
$content = json_decode($json, null, 512, JSON_THROW_ON_ERROR);
82-
CODE_SAMPLE
83-
,
84-
<<<'CODE_SAMPLE'
75+
8576
$content = json_decode($json, null, 512, 0);
8677
if (json_last_error() !== JSON_ERROR_NONE) {
8778
throw new \Exception(json_last_error_msg());
@@ -97,11 +88,11 @@ public function getRuleDefinition(): RuleDefinition
9788
*/
9889
public function getNodeTypes(): array
9990
{
100-
return [ConstFetch::class, BitwiseOr::class, If_::class, TryCatch::class, Expression::class];
91+
return [ConstFetch::class, BitwiseOr::class, If_::class, Expression::class];
10192
}
10293

10394
/**
104-
* @param ConstFetch|BitwiseOr|If_|TryCatch|Expression $node
95+
* @param ConstFetch|BitwiseOr|If_|Expression $node
10596
* @return null|Expr|array<Expression|If_>
10697
*/
10798
public function refactor(Node $node): null|Expr|array
@@ -116,29 +107,10 @@ public function refactor(Node $node): null|Expr|array
116107
return null;
117108
}
118109

119-
if ($node instanceof TryCatch) {
120-
$this->traverseNodesWithCallable(
121-
$node->stmts,
122-
function (Node $subNode): ?int {
123-
if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) {
124-
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
125-
}
126-
127-
if (! $subNode instanceof Expression) {
128-
return null;
129-
}
130-
131-
$funcCall = $this->resolveFuncCall($subNode);
132-
if ($funcCall instanceof FuncCall) {
133-
$subNode->setAttribute(self::IS_EXPRESSION_INSIDE_TRY_CATCH, true);
134-
}
135-
136-
return null;
137-
}
138-
);
139-
140-
return null;
141-
}
110+
// if ($node instanceof TryCatch) {
111+
// SimpleNodeTraverser::decorateWithAttributeValue($node->stmts, self::IS_EXPRESSION_INSIDE_TRY_CATCH, true);
112+
// return null;
113+
// }
142114

143115
if ($node instanceof Expression) {
144116
return $this->refactorStmt($node);
@@ -184,11 +156,12 @@ private function resolveFuncCall(Expression $Expression): ?FuncCall
184156
* only when the flags are directly set in the function call.
185157
* If the flags are set from a variable, that would require a much more
186158
* complex analysis to be 100% accurate, beyond Rector actual capabilities.
159+
*
187160
* @return null|array<Expression|If_>
188161
*/
189162
private function refactorStmt(Expression $Expression): ?array
190163
{
191-
if ($Expression->getAttribute(self::IS_EXPRESSION_INSIDE_TRY_CATCH) === true) {
164+
if ($Expression->getAttribute(AttributeKey::IS_IN_TRY_BLOCK) === true) {
192165
return null;
193166
}
194167

0 commit comments

Comments
 (0)