Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ public function getNodeTypes(): array
/**
* @param ConstFetch|BitwiseOr|If_ $node
*/
public function refactor(Node $node): Expr|If_|null
public function refactor(Node $node): Expr|null
{
if ($node instanceof If_) {
return $this->refactorIf($node);
$this->markConstantKnownInInnerStmts($node);
return null;
}

// skip as known
Expand All @@ -84,20 +85,18 @@ public function refactor(Node $node): Expr|If_|null
]);
}

private function refactorIf(If_ $if): ?If_
private function markConstantKnownInInnerStmts(If_ $if): void
{
if (! $this->defineFuncCallAnalyzer->isDefinedWithConstants($if->cond, [
JsonConstant::INVALID_UTF8_IGNORE,
JsonConstant::INVALID_UTF8_SUBSTITUTE,
])) {
return null;
return;
}

$this->traverseNodesWithCallable($if, static function (Node $node): null {
$node->setAttribute(self::PHP72_JSON_CONSTANT_IS_KNOWN, true);
return null;
});

return $if;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ public function getNodeTypes(): array

/**
* @param ConstFetch|BitwiseOr|If_|TryCatch|Expression $node
* @return null|Expr|If_|array<Expression|If_>
* @return null|Expr|array<Expression|If_>
*/
public function refactor(Node $node): null|Expr|If_|array
public function refactor(Node $node): null|Expr|array
{
if ($node instanceof If_) {
return $this->refactorIf($node);
$this->markConstantKnownInInnerStmts($node);
return null;
}

// skip as known
Expand Down Expand Up @@ -146,18 +147,16 @@ function (Node $subNode): ?int {
return $this->jsonConstCleaner->clean($node, [JsonConstant::THROW_ON_ERROR]);
}

private function refactorIf(If_ $if): ?If_
private function markConstantKnownInInnerStmts(If_ $if): void
{
if (! $this->defineFuncCallAnalyzer->isDefinedWithConstants($if->cond, [JsonConstant::THROW_ON_ERROR])) {
return null;
return;
}

$this->traverseNodesWithCallable($if, static function (Node $node): null {
$node->setAttribute(self::PHP73_JSON_CONSTANT_IS_KNOWN, true);
return null;
});

return $if;
}

private function resolveFuncCall(Expression $Expression): ?FuncCall
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ public function getNodeTypes(): array
public function refactor(Node $node): Node|null
{
if ($node instanceof Instanceof_) {
return $this->refactorInstanceof($node);
$this->markSkipInstanceof($node);
return null;
}

if ($node instanceof Ternary) {
return $this->refactorTernary($node);
$this->markSkipTernary($node);
return null;
}

if ($node->getAttribute(self::SKIP_NODE) === true) {
Expand All @@ -105,36 +107,34 @@ public function refactor(Node $node): Node|null
);
}

private function refactorInstanceof(Instanceof_ $instanceof): ?Instanceof_
private function markSkipInstanceof(Instanceof_ $instanceof): void
{
if (! $this->isName($instanceof->class, 'ReflectionNamedType')) {
return null;
return;
}

if (! $instanceof->expr instanceof MethodCall) {
return null;
return;
}

// checked typed → safe
$instanceof->expr->setAttribute(self::SKIP_NODE, true);
return $instanceof;
}

private function refactorTernary(Ternary $ternary): ?Ternary
private function markSkipTernary(Ternary $ternary): void
{
if (! $ternary->if instanceof Expr) {
return null;
return;
}

if (! $ternary->cond instanceof FuncCall) {
return null;
return;
}

if (! $this->isName($ternary->cond, 'method_exists')) {
return null;
return;
}

$ternary->if->setAttribute(self::SKIP_NODE, true);
return $ternary;
}
}
Loading