Skip to content

Commit

Permalink
json_validate() stub
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 28, 2023
1 parent f5bacaf commit 828b269
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ parameters:
- ../stubs/arrayFunctions.stub
- ../stubs/core.stub
- ../stubs/typeCheckingFunctions.stub
- ../stubs/json.stub
earlyTerminatingMethodCalls: []
earlyTerminatingFunctionCalls: []
memoryLimitFile: %tmpDir%/.memory_limit
Expand Down
10 changes: 10 additions & 0 deletions stubs/json.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/**
* @param positive-int $depth
* @param int-mask<JSON_INVALID_UTF8_IGNORE> $flags
* @phpstan-assert-if-true =non-empty-string $json
*/
function json_validate(string $json, int $depth = 512, int $flags = 0): bool
{
}
4 changes: 4 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,10 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7096.php');
}

if (PHP_VERSION_ID >= 80300) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/json-validate.php');
}

if (PHP_VERSION_ID >= 80100) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7167.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6864.php');
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Analyser/data/json-validate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace JsonValidate;

use function PHPStan\Testing\assertType;

class Foo
{

public function doFoo(string $s): void
{
if (json_validate($s)) {
assertType('non-empty-string', $s);
} else {
assertType('string', $s);
}
}

}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1388,4 +1388,22 @@ public function testFlockParams(): void
]);
}

public function testJsonValidate(): void
{
if (PHP_VERSION_ID < 80300) {
$this->markTestSkipped('Test requires PHP 8.3');
}

$this->analyse([__DIR__ . '/data/json_validate.php'], [
[
'Parameter #2 $depth of function json_validate expects int<1, max>, 0 given.',
6,
],
[
'Parameter #3 $flags of function json_validate expects 0|1048576, 2 given.',
7,
],
]);
}

}
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Functions/data/json_validate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace JsonValidateParams;

function doFoo() {
$x = json_validate('{ "test": { "foo": "bar" } }', 0, 0); // invalid depth
$x = json_validate('{ "test": { "foo": "bar" } }', 100, JSON_BIGINT_AS_STRING); // invalid flags

$x = json_validate('{ "test": { "foo": "bar" } }');
$x = json_validate('{ "test": { "foo": "bar" } }', 100, 0);
$x = json_validate('{ "test": { "foo": "bar" } }', 100, JSON_INVALID_UTF8_IGNORE);

}

0 comments on commit 828b269

Please sign in to comment.