Skip to content

Commit 665e9da

Browse files
TomasVotrubaondrejmirtes
authored andcommitted
add test case for invalid json string
1 parent b2b979b commit 665e9da

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class NodeScopeResolverTest extends TypeInferenceTestCase
1414

1515
public function dataFileAsserts(): iterable
1616
{
17-
<<<<<<< HEAD
1817
require_once __DIR__ . '/data/implode.php';
1918
yield from $this->gatherAssertTypes(__DIR__ . '/data/implode.php');
20-
=======
19+
2120
yield from $this->gatherAssertTypes(__DIR__ . '/data/json-decode/narrow_type.php');
2221
yield from $this->gatherAssertTypes(__DIR__ . '/data/json-decode/narrow_type_with_force_array.php');
23-
>>>>>>> add test case for narrow typed json_decode
22+
yield from $this->gatherAssertTypes(__DIR__ . '/data/json-decode/invalid_type.php');
23+
yield from $this->gatherAssertTypes(__DIR__ . '/data/json-decode/json_object_as_array.php');
2424

2525
require_once __DIR__ . '/data/bug2574.php';
2626

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Analyser\JsonDecode;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
$value = json_decode('{"key"}');
8+
assertType('null', $value);
9+
10+
$value = json_decode('{"key"}', true);
11+
assertType('null', $value);
12+
13+
$value = json_decode('{"key"}', null);
14+
assertType('null', $value);
15+
16+
$value = json_decode('{"key"}', false);
17+
assertType('null', $value);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Analyser\JsonDecode;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
// @see https://3v4l.org/YFlHF
8+
function ($mixed) {
9+
$value = json_decode($mixed, null, 512, JSON_OBJECT_AS_ARRAY);
10+
assertType('mixed~stdClass', $value);
11+
};
12+
13+
function ($mixed) {
14+
$value = json_decode($mixed, null);
15+
assertType('mixed', $value);
16+
};

tests/PHPStan/Analyser/data/json-decode/narrow_type_with_force_array.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,3 @@ function ($mixed) {
2626
$value = json_decode($mixed, true);
2727
assertType('mixed~stdClass', $value);
2828
};
29-
30-
// @see https://3v4l.org/YFlHF
31-
function ($mixed) {
32-
$value = json_decode($mixed, null, 512, JSON_OBJECT_AS_ARRAY);
33-
assertType('mixed~stdClass', $value);
34-
};

0 commit comments

Comments
 (0)