Skip to content

Commit 828b269

Browse files
committed
json_validate() stub
1 parent f5bacaf commit 828b269

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed

conf/config.neon

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ parameters:
165165
- ../stubs/arrayFunctions.stub
166166
- ../stubs/core.stub
167167
- ../stubs/typeCheckingFunctions.stub
168+
- ../stubs/json.stub
168169
earlyTerminatingMethodCalls: []
169170
earlyTerminatingFunctionCalls: []
170171
memoryLimitFile: %tmpDir%/.memory_limit

stubs/json.stub

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
/**
4+
* @param positive-int $depth
5+
* @param int-mask<JSON_INVALID_UTF8_IGNORE> $flags
6+
* @phpstan-assert-if-true =non-empty-string $json
7+
*/
8+
function json_validate(string $json, int $depth = 512, int $flags = 0): bool
9+
{
10+
}

tests/PHPStan/Analyser/NodeScopeResolverTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,10 @@ public function dataFileAsserts(): iterable
946946
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7096.php');
947947
}
948948

949+
if (PHP_VERSION_ID >= 80300) {
950+
yield from $this->gatherAssertTypes(__DIR__ . '/data/json-validate.php');
951+
}
952+
949953
if (PHP_VERSION_ID >= 80100) {
950954
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7167.php');
951955
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6864.php');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace JsonValidate;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
10+
public function doFoo(string $s): void
11+
{
12+
if (json_validate($s)) {
13+
assertType('non-empty-string', $s);
14+
} else {
15+
assertType('string', $s);
16+
}
17+
}
18+
19+
}

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -1388,4 +1388,22 @@ public function testFlockParams(): void
13881388
]);
13891389
}
13901390

1391+
public function testJsonValidate(): void
1392+
{
1393+
if (PHP_VERSION_ID < 80300) {
1394+
$this->markTestSkipped('Test requires PHP 8.3');
1395+
}
1396+
1397+
$this->analyse([__DIR__ . '/data/json_validate.php'], [
1398+
[
1399+
'Parameter #2 $depth of function json_validate expects int<1, max>, 0 given.',
1400+
6,
1401+
],
1402+
[
1403+
'Parameter #3 $flags of function json_validate expects 0|1048576, 2 given.',
1404+
7,
1405+
],
1406+
]);
1407+
}
1408+
13911409
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace JsonValidateParams;
4+
5+
function doFoo() {
6+
$x = json_validate('{ "test": { "foo": "bar" } }', 0, 0); // invalid depth
7+
$x = json_validate('{ "test": { "foo": "bar" } }', 100, JSON_BIGINT_AS_STRING); // invalid flags
8+
9+
$x = json_validate('{ "test": { "foo": "bar" } }');
10+
$x = json_validate('{ "test": { "foo": "bar" } }', 100, 0);
11+
$x = json_validate('{ "test": { "foo": "bar" } }', 100, JSON_INVALID_UTF8_IGNORE);
12+
13+
}

0 commit comments

Comments
 (0)