Skip to content

Commit bc0ce70

Browse files
committed
misc
1 parent cab5fdc commit bc0ce70

File tree

4 files changed

+19
-24
lines changed

4 files changed

+19
-24
lines changed

phpstan.neon

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ includes:
55
parameters:
66
level: 8
77
errorFormat: symplify
8-
# reportUnmatchedIgnoredErrors: false
8+
reportUnmatchedIgnoredErrors: false
99

1010
# see https://phpstan.org/writing-php-code/phpdoc-types#global-type-aliases
1111
typeAliases:
@@ -113,3 +113,6 @@ parameters:
113113
- rules/DowngradePhp74/Rector/MethodCall/DowngradeReflectionGetTypeRector.php
114114
- rules/DowngradePhp80/Rector/MethodCall/DowngradeReflectionGetAttributesRector.php
115115
- rules/DowngradePhp73/Rector/String_/DowngradeFlexibleHeredocSyntaxRector.php
116+
117+
# local attribute key
118+
- '#Parameter \#2 \$attributeKey of static method Rector\\PhpParser\\NodeTraverser\\SimpleNodeTraverser\:\:decorateWithAttributeValue\(\) expects#'

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

Whitespace-only changes.

rules/DowngradePhp73/Rector/ConstFetch/DowngradePhp73JsonConstRector.php

Lines changed: 14 additions & 22 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,
@@ -63,12 +63,12 @@ public function getRuleDefinition(): RuleDefinition
6363
CODE_SAMPLE
6464
,
6565
<<<'CODE_SAMPLE'
66-
$json json_encode($content, 0);
66+
$json = json_encode($content);
6767
if (json_last_error() !== JSON_ERROR_NONE) {
6868
throw new \Exception(json_last_error_msg());
6969
}
7070
71-
$content = json_decode($json, null, 512, 0);
71+
$content = json_decode($json, null, 512);
7272
if (json_last_error() !== JSON_ERROR_NONE) {
7373
throw new \Exception(json_last_error_msg());
7474
}
@@ -115,15 +115,12 @@ private function markConstantKnownInInnerStmts(If_ $if): void
115115
return;
116116
}
117117

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

124-
private function resolveFuncCall(Expression $Expression): ?FuncCall
121+
private function resolveFuncCall(Expression $expression): ?FuncCall
125122
{
126-
$expr = $Expression->expr;
123+
$expr = $expression->expr;
127124
if ($expr instanceof Assign) {
128125
if ($expr->expr instanceof FuncCall) {
129126
return $expr->expr;
@@ -155,21 +152,22 @@ private function refactorExpression(Expression $expression): ?array
155152
return null;
156153
}
157154

158-
// retrieve a `FuncCall`, if any, from the statement
159155
$funcCall = $this->resolveFuncCall($expression);
160-
161-
// Nothing to do if no `FuncCall` found
162156
if (! $funcCall instanceof FuncCall) {
163157
return null;
164158
}
165159

160+
if ($funcCall->isFirstClassCallable()) {
161+
return null;
162+
}
163+
166164
// Nothing to do if not a refactored function
167-
if (! in_array($this->getName($funcCall), self::REFACTOR_FUNCS, true)) {
165+
if (! $this->isNames($funcCall, self::JSON_FUNCTIONS)) {
168166
return null;
169167
}
170168

171169
// Nothing to do if the flag `JSON_THROW_ON_ERROR` is not set in args
172-
if (! $this->hasConstFetchInArgs($funcCall->args, 'JSON_THROW_ON_ERROR')) {
170+
if (! $this->hasConstFetchInArgs($funcCall->getArgs(), 'JSON_THROW_ON_ERROR')) {
173171
return null;
174172
}
175173

@@ -196,18 +194,12 @@ private function refactorExpression(Expression $expression): ?array
196194

197195
/**
198196
* Search if a given constant is set within a list of `Arg`
199-
* @param array<Arg|VariadicPlaceholder> $args
197+
* @param Arg[] $args
200198
*/
201199
private function hasConstFetchInArgs(array $args, string $constName): bool
202200
{
203201
foreach ($args as $arg) {
204-
// Only `Arg` instances are handled.
205-
if (! $arg instanceof Arg) {
206-
return false;
207-
}
208-
209202
$value = $arg->value;
210-
211203
if ($value instanceof ConstFetch && $this->getName($value) === $constName) {
212204
return true;
213205
}

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)