diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-16 12:51:24 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-16 12:51:24 +0900 |
| commit | fce44d4587ba11b080fe0266c483d0be684ffd0d (patch) | |
| tree | 753e87ac7ed2e57d635a60c10a4ce01b7e0f0f8e /crates/shirabe/src/json/json_file.rs | |
| parent | b12c2b15f0487d54d359a581e99ced68713914d0 (diff) | |
| download | php-shirabe-fce44d4587ba11b080fe0266c483d0be684ffd0d.tar.gz php-shirabe-fce44d4587ba11b080fe0266c483d0be684ffd0d.tar.zst php-shirabe-fce44d4587ba11b080fe0266c483d0be684ffd0d.zip | |
refactor(php-shim): split json_decode into assoc and obj variants
The assoc flag was always a literal at every call site, so the boolean
carried no information the function name could not. json_decode_assoc
and json_decode_obj make the resulting PhpMixed shape visible at the
call site.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/json/json_file.rs')
| -rw-r--r-- | crates/shirabe/src/json/json_file.rs | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/crates/shirabe/src/json/json_file.rs b/crates/shirabe/src/json/json_file.rs index 78ed76d7..e31cd70d 100644 --- a/crates/shirabe/src/json/json_file.rs +++ b/crates/shirabe/src/json/json_file.rs @@ -14,8 +14,8 @@ use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ InvalidArgumentException, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, PhpMixed, RuntimeException, UnexpectedValueException, dirname, file_exists, file_get_contents, - file_put_contents, is_dir, is_file, json_decode, json_encode_ex, mkdir, php_regex, realpath, - str_repeat, strlen, strpos, usleep, + file_put_contents, is_dir, is_file, json_decode_assoc, json_decode_obj, json_encode_ex, mkdir, + php_regex, realpath, str_repeat, strlen, strpos, usleep, }; use shirabe_seld_json_lint::{ParsingException, ParsingExceptionDetails}; @@ -309,7 +309,7 @@ impl JsonFile { .into()); } let content = file_get_contents(&self.path).unwrap_or_default(); - let data = json_decode(&content, false)?; + let data = json_decode_obj(&content)?; if matches!(data, PhpMixed::Null) && content != "null" { Self::validate_syntax(&content, Some(&self.path))?; @@ -362,7 +362,7 @@ impl JsonFile { }; if schema == Self::STRICT_SCHEMA && is_composer_schema_file { - schema_data = json_decode(Self::COMPOSER_SCHEMA_JSON, false)?; + schema_data = json_decode_obj(Self::COMPOSER_SCHEMA_JSON)?; if let PhpMixed::Object(map) = &mut schema_data { map.insert("additionalProperties".to_string(), PhpMixed::Bool(false)); map.insert( @@ -476,11 +476,12 @@ impl JsonFile { None => return Ok(PhpMixed::Null), Some(j) => j, }; - let mut data = json_decode(json, true)?; + let mut data = json_decode_assoc(json)?; // PHP: `null === $data && JSON_ERROR_NONE !== json_last_error()`, i.e. the decode produced - // null because of an error rather than because the input was the literal `null`. json_decode - // here swallows the error into PhpMixed::Null, so detect the failure by comparing the source - // against `null`, mirroring validateSchema's own `'null' !== $content` check. + // null because of an error rather than because the input was the literal `null`. + // json_decode_assoc swallows the error into PhpMixed::Null, so detect the failure by + // comparing the source against `null`, mirroring validateSchema's own + // `'null' !== $content` check. if matches!(data, PhpMixed::Null) && json != "null" { // attempt resolving simple conflicts in lock files so that one can run `composer update --lock` and get a valid lock file if let Some(file) = file @@ -498,7 +499,7 @@ impl JsonFile { &mut count, ); if count == 1 { - data = json_decode(&replaced, true)?; + data = json_decode_assoc(&replaced)?; if !matches!(data, PhpMixed::Null) { return Ok(data); } |
