Skip to content

Commit 057715b

Browse files
committed
zero has no value
1 parent 4c62ddb commit 057715b

File tree

3 files changed

+15
-27
lines changed

3 files changed

+15
-27
lines changed

rules-tests/DowngradePhp80/Rector/MethodCall/DowngradeReflectionGetAttributesRector/Fixture/class_is_nullable.php

Whitespace-only changes.

rules/DowngradePhp73/Rector/ConstFetch/DowngradePhp73JsonConstRector.php

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
use PhpParser\Node\Name\FullyQualified as NameFullyQualified;
1919
use PhpParser\Node\Stmt\Expression;
2020
use PhpParser\Node\Stmt\If_;
21-
use PhpParser\Node\VariadicPlaceholder;
2221
use Rector\DowngradePhp72\NodeManipulator\JsonConstCleaner;
2322
use Rector\Enum\JsonConstant;
2423
use Rector\NodeAnalyzer\DefineFuncCallAnalyzer;
2524
use Rector\NodeTypeResolver\Node\AttributeKey;
25+
use Rector\PhpParser\NodeTraverser\SimpleNodeTraverser;
2626
use Rector\Rector\AbstractRector;
2727
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2828
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -42,7 +42,7 @@ final class DowngradePhp73JsonConstRector extends AbstractRector
4242
/**
4343
* @var array<string>
4444
*/
45-
private const REFACTOR_FUNCS = ['json_decode', 'json_encode'];
45+
private const JSON_FUNCTIONS = ['json_decode', 'json_encode'];
4646

4747
public function __construct(
4848
private readonly JsonConstCleaner $jsonConstCleaner,
@@ -103,9 +103,11 @@ public function refactor(Node $node): null|Expr|array
103103
}
104104

105105
if ($node instanceof Expression) {
106-
return $this->refactorStmt($node);
106+
return $this->refactorExpression($node);
107107
}
108108

109+
dump($node::class);
110+
109111
return $this->jsonConstCleaner->clean($node, [JsonConstant::THROW_ON_ERROR]);
110112
}
111113

@@ -115,15 +117,12 @@ private function markConstantKnownInInnerStmts(If_ $if): void
115117
return;
116118
}
117119

118-
$this->traverseNodesWithCallable($if, static function (Node $node): null {
119-
$node->setAttribute(self::PHP73_JSON_CONSTANT_IS_KNOWN, true);
120-
return null;
121-
});
120+
SimpleNodeTraverser::decorateWithAttributeValue($if, self::PHP73_JSON_CONSTANT_IS_KNOWN, true);
122121
}
123122

124-
private function resolveFuncCall(Expression $Expression): ?FuncCall
123+
private function resolveFuncCall(Expression $expression): ?FuncCall
125124
{
126-
$expr = $Expression->expr;
125+
$expr = $expression->expr;
127126
if ($expr instanceof Assign) {
128127
if ($expr->expr instanceof FuncCall) {
129128
return $expr->expr;
@@ -149,16 +148,13 @@ private function resolveFuncCall(Expression $Expression): ?FuncCall
149148
*
150149
* @return null|array<Expression|If_>
151150
*/
152-
private function refactorStmt(Expression $expression): ?array
151+
private function refactorExpression(Expression $expression): ?array
153152
{
154153
if ($expression->getAttribute(AttributeKey::IS_IN_TRY_BLOCK) === true) {
155154
return null;
156155
}
157156

158-
// retrieve a `FuncCall`, if any, from the statement
159157
$funcCall = $this->resolveFuncCall($expression);
160-
161-
// Nothing to do if no `FuncCall` found
162158
if (! $funcCall instanceof FuncCall) {
163159
return null;
164160
}
@@ -168,12 +164,12 @@ private function refactorStmt(Expression $expression): ?array
168164
}
169165

170166
// Nothing to do if not a refactored function
171-
if (! $this->isNames($funcCall, self::REFACTOR_FUNCS)) {
167+
if (! $this->isNames($funcCall, self::JSON_FUNCTIONS)) {
172168
return null;
173169
}
174170

175171
// Nothing to do if the flag `JSON_THROW_ON_ERROR` is not set in args
176-
if (! $this->hasConstFetchInArgs($funcCall->args, 'JSON_THROW_ON_ERROR')) {
172+
if (! $this->hasConstFetchInArgs($funcCall->getArgs(), 'JSON_THROW_ON_ERROR')) {
177173
return null;
178174
}
179175

@@ -200,18 +196,12 @@ private function refactorStmt(Expression $expression): ?array
200196

201197
/**
202198
* Search if a given constant is set within a list of `Arg`
203-
* @param array<Arg|VariadicPlaceholder> $args
199+
* @param Arg[] $args
204200
*/
205201
private function hasConstFetchInArgs(array $args, string $constName): bool
206202
{
207203
foreach ($args as $arg) {
208-
// Only `Arg` instances are handled.
209-
if (! $arg instanceof Arg) {
210-
return false;
211-
}
212-
213204
$value = $arg->value;
214-
215205
if ($value instanceof ConstFetch && $this->getName($value) === $constName) {
216206
return true;
217207
}
@@ -229,8 +219,6 @@ private function hasConstFetchInArgs(array $args, string $constName): bool
229219
*/
230220
private function hasConstFetchInBitwiseOr(BitwiseOr $bitwiseOr, string $constName): bool
231221
{
232-
$found = false;
233-
234222
foreach ([$bitwiseOr->left, $bitwiseOr->right] as $subNode) {
235223
$found = match (true) {
236224
$subNode instanceof BitwiseOr => (
@@ -243,10 +231,10 @@ private function hasConstFetchInBitwiseOr(BitwiseOr $bitwiseOr, string $constNam
243231
};
244232

245233
if ($found) {
246-
break;
234+
return true;
247235
}
248236
}
249237

250-
return $found;
238+
return false;
251239
}
252240
}

tests/Issues/DowngradeNullJson/Fixture/fixture.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Fixture
2222
{
2323
public function run(?int $flags = null)
2424
{
25-
$flags = $flags ?? JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_PRESERVE_ZERO_FRACTION;
25+
$flags = $flags ?? JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_PRESERVE_ZERO_FRACTION | 0;
2626

2727
json_encode([], $flags);
2828
}

0 commit comments

Comments
 (0)