diff --git a/stubs/CoreGenericFunctions.phpstub b/stubs/CoreGenericFunctions.phpstub index 11765e5bea2..eaf02d61a4b 100644 --- a/stubs/CoreGenericFunctions.phpstub +++ b/stubs/CoreGenericFunctions.phpstub @@ -1460,19 +1460,19 @@ function ldap_escape(string $value, string $ignore = "", int $flags = 0) : strin /** * @psalm-pure * + * @param int<1, 2147483647> $depth * @return mixed * @psalm-flow ($json) -> return */ function json_decode(string $json, ?bool $associative = null, int $depth = 512, int $flags = 0) {} /** + * The conditional return type below relies on the fact that JSON_THROW_ON_ERROR is + * the highest-valued of JSON constants * @psalm-pure * - * @return ( - * $flags is 4194304 - * ? non-empty-string - * : ($flags is 4194560 ? non-empty-string : non-empty-string|false) - * ) + * @param int<1, 2147483647> $depth + * @return ($flags is int<4194304, 8388607> ? non-empty-string : non-empty-string|false) * * @psalm-flow ($value) -> return * @psalm-ignore-falsable-return diff --git a/tests/CoreStubsTest.php b/tests/CoreStubsTest.php index 2ef09a1d860..fe2ee5ea9b7 100644 --- a/tests/CoreStubsTest.php +++ b/tests/CoreStubsTest.php @@ -2,11 +2,13 @@ namespace Psalm\Tests; +use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait; use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait; class CoreStubsTest extends TestCase { use ValidCodeAnalysisTestTrait; + use InvalidCodeAnalysisTestTrait; public function providerValidCodeParse(): iterable { @@ -131,5 +133,39 @@ function foo(string $foo): string '$a===' => 'non-empty-string', ], ]; + yield 'json_encode returns a non-empty-string with JSON_THROW_ON_ERROR' => [ + 'code' => ' [ + '$a===' => 'non-empty-string', + '$b===' => 'non-empty-string', + '$c===' => 'non-empty-string', + '$d===' => 'non-empty-string', + '$e===' => 'false|non-empty-string', + '$f===' => 'false|non-empty-string', + ], + ]; + } + + public function providerInvalidCodeParse(): iterable + { + yield 'json_decode invalid depth' => [ + 'code' => ' 'InvalidArgument', + ]; + yield 'json_encode invalid depth' => [ + 'code' => ' 'InvalidArgument', + ]; } }