Skip to content

[deprecation] Deprecate JsonThrowOnErrorRector as it can cause BC breaks - #8327

Merged
TomasVotruba merged 1 commit into
mainfrom
deprecate-json-throw-on-error-rector
Aug 9, 2026
Merged

[deprecation] Deprecate JsonThrowOnErrorRector as it can cause BC breaks#8327
TomasVotruba merged 1 commit into
mainfrom
deprecate-json-throw-on-error-rector

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Deprecates JsonThrowOnErrorRector, following the same pattern as #8324.

The rule adds JSON_THROW_ON_ERROR to every json_encode() / json_decode() call:

-json_encode($content);
-json_decode($json);
+json_encode($content, JSON_THROW_ON_ERROR);
+json_decode($json, null, 512, JSON_THROW_ON_ERROR);

This is a BC break, not a cleanup. Before the change a malformed payload returns false or null; after it, the same payload throws JsonException. Code that relied on the silent return value now fatals at runtime:

// before: $data is null, the if-branch handles it
// after: JsonException is thrown, the if-branch is never reached
$data = json_decode($json);
if ($data === null) {
    return $this->fallback();
}

Every call site has to be reviewed by hand to decide whether an exception is wanted and where it gets caught. Rector cannot make that call, so the rule is deprecated instead. For consistent error reporting on any PHP version, a wrapper tool like nette/utils Json::decode() is the better fit.

The rule was in no set, so no user config is silently affected.

Changes:

  • JsonThrowOnErrorRector implements DeprecatedInterface, refactor() throws ShouldNotHappenException
  • removed its test + fixtures
  • removed tests/Issues/ScopeNotAvailable/JsonThrowCaseSensitiveConstFetchTest.php, a regression test that only existed to cover this rule

Adding JSON_THROW_ON_ERROR turns silent false/null results into a thrown JsonException. Every call site has to be reviewed by hand, so the change needs manual care instead. Use a wrapper tool like nette/utils Json for clear error reporting on any PHP version.
@TomasVotruba
TomasVotruba merged commit 052375d into main Aug 9, 2026
64 checks passed
@TomasVotruba
TomasVotruba deleted the deprecate-json-throw-on-error-rector branch August 9, 2026 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant