Skip to content

Commit bd37407

Browse files
rename isJson->jsonValidate
1 parent db6a5d9 commit bd37407

9 files changed

+25
-20
lines changed

_ide_helper.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Illuminate\Support {
1414
/**
15-
* @method static bool isJson($value)
15+
* @method static bool jsonValidate(string $json, int $depth = 512, int $flags = 0)
1616
*
1717
* @mixin \Illuminate\Support\Str
1818
*/
@@ -21,7 +21,7 @@ final class Str
2121
}
2222

2323
/**
24-
* @method bool isJson()
24+
* @method bool jsonValidate(int $depth = 512, int $flags = 0)
2525
*
2626
* @mixin \Illuminate\Support\Stringable
2727
*/

app/Commands/CommitCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function ($attempts) use ($cachedDiff): string {
8484
->driver($this->option('generator'))
8585
->generate($this->getPrompt($cachedDiff));
8686
$message = $this->tryFixMessage($originalMessage);
87-
if (! str($message)->isJson()) {
87+
if (! str($message)->jsonValidate()) {
8888
throw new TaskException(sprintf(
8989
'The generated commit message(%s) is an invalid JSON.',
9090
var_export($originalMessage, true)

app/Commands/ConfigCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private function argToValue(string $arg)
227227
return str_contains($arg, '.') ? (float) $arg : (int) $arg;
228228
}
229229

230-
if (str($arg)->isJson()) {
230+
if (str($arg)->jsonValidate()) {
231231
return json_decode($arg, true, 512, JSON_THROW_ON_ERROR);
232232
}
233233

app/ConfigManager.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public static function readFrom(...$files): array
227227
}
228228

229229
if ($ext->is('json')) {
230-
if (! str($contents = file_get_contents($file))->isJson()) {
230+
if (! str($contents = file_get_contents($file))->jsonValidate()) {
231231
throw InvalidJsonFileException::make($file);
232232
}
233233

app/Macros/StrMacro.php

+14-9
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,25 @@ final class StrMacro
2121
* @psalm-suppress UnusedFunctionCall
2222
* @noinspection BadExceptionsProcessingInspection
2323
*/
24-
public static function isJson(): \Closure
24+
public static function jsonValidate(): \Closure
2525
{
26-
return static function ($value): bool {
27-
if (! \is_string($value)) {
28-
return false;
26+
return static function (string $json, int $depth = 512, int $flags = 0): bool {
27+
if (0 !== $flags && \defined('JSON_INVALID_UTF8_IGNORE') && JSON_INVALID_UTF8_IGNORE !== $flags) {
28+
throw new \ValueError('json_validate(): Argument #3 ($flags) must be a valid flag (allowed flags: JSON_INVALID_UTF8_IGNORE)');
2929
}
3030

31-
try {
32-
json_decode($value, true, 512, JSON_THROW_ON_ERROR);
33-
} catch (\JsonException $jsonException) {
34-
return false;
31+
if ($depth <= 0) {
32+
throw new \ValueError('json_validate(): Argument #2 ($depth) must be greater than 0');
3533
}
3634

37-
return true;
35+
// see https://www.php.net/manual/en/function.json-decode.php
36+
if ($depth > ($jsonMaxDepth = 0x7FFFFFFF)) {
37+
throw new \ValueError(sprintf('json_validate(): Argument #2 ($depth) must be less than %d', $jsonMaxDepth));
38+
}
39+
40+
json_decode($json, null, $depth, $flags);
41+
42+
return JSON_ERROR_NONE === json_last_error();
3843
};
3944
}
4045
}

app/Macros/StringableMacro.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ final class StringableMacro
2222
/**
2323
* @psalm-suppress InaccessibleProperty
2424
*/
25-
public function isJson(): \Closure
25+
public function jsonValidate(): \Closure
2626
{
27-
return function (): bool {
28-
return Str::isJson($this->value);
27+
return function (int $depth = 512, int $flags = 0): bool {
28+
return Str::jsonValidate($this->value, $depth, $flags);
2929
};
3030
}
3131
}

app/Support/OpenAI.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ static function (PendingRequest $pendingRequest) use (&$rowData, $writer): Pendi
264264
// $contents = $response->getBody()->getContents();
265265
//
266266
// // $parameters['stream'] === true && $writer === null
267-
// if ($contents && ! \str($contents)->isJson()) {
267+
// if ($contents && ! \str($contents)->jsonValidate()) {
268268
// $data = \str($contents)
269269
// ->explode("\n\n")
270270
// ->reverse()

phpstan.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ parameters:
3131
# noVariableVariables: false
3232
ignoreErrors:
3333
- "#^Call to an undefined method #"
34-
- "#^Call to an undefined static method Illuminate\\\\Support\\\\Str\\:\\:isJson\\(\\)\\.$#"
34+
- "#^Call to an undefined static method Illuminate\\\\Support\\\\Str\\:\\:jsonValidate\\(\\)\\.$#"
3535
- "#^Undefined variable\\: \\$this$#"

tests/Unit/Macros/StrMacroTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
use Illuminate\Support\Str;
1414

1515
it('will return false', function (): void {
16-
expect(Str::isJson(null))->toBeFalse();
16+
expect(Str::jsonValidate(''))->toBeFalse();
1717
})->group(__DIR__, __FILE__);

0 commit comments

Comments
 (0)