Skip to content

Commit 19596c3

Browse files
authored
Add dynamic return type for stripslashes (#232)
1 parent fa9e550 commit 19596c3

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

functionMap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
'sanitize_post' => ['T', '@phpstan-template' => 'T of array|object', 'post' => 'T'],
5050
'sanitize_term' => ['T', '@phpstan-template' => 'T of array|object', 'term' => 'T'],
5151
'stripslashes_deep' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
52+
'stripslashes_from_strings_only' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
5253
'urldecode_deep' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
5354
'urlencode_deep' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
5455
'validate_file' => ["(\$file is '' ? 0 : (\$allowed_files is empty ? 0|1|2 : 0|1|2|3))"],

tests/Faker.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
/**
8+
* @phpstan-type Types array{
9+
* bool: bool,
10+
* int: int,
11+
* float: float,
12+
* string: string,
13+
* array: array<mixed>,
14+
* resource: resource,
15+
* object: object,
16+
* numeric-string: numeric-string,
17+
* null: null,
18+
* mixed: mixed,
19+
* true: true,
20+
* false: false,
21+
* callable: callable,
22+
* iterable: iterable<mixed>,
23+
* array-key: array-key,
24+
* positive-int: positive-int,
25+
* negative-int: negative-int,
26+
* non-positive-int: non-positive-int,
27+
* non-negative-int: non-negative-int,
28+
* non-zero-int: non-zero-int,
29+
* }
30+
*/
31+
class Faker
32+
{
33+
/**
34+
* @var Types $types
35+
* @phpstan-ignore-next-line
36+
*/
37+
private static $types;
38+
39+
/**
40+
* @template T of string
41+
* @param T $type
42+
* @return Types[T]
43+
*/
44+
public static function fake(string $type): mixed
45+
{
46+
return self::$types[$type];
47+
}
48+
49+
/**
50+
* @template T of string
51+
* @template K of string
52+
* @param T $valueType
53+
* @param K $keyType
54+
* @return array<Types[K], Types[T]>
55+
*/
56+
public static function fakeArray(string $valueType, string $keyType = 'array-key'): mixed
57+
{
58+
return [$_GET[$keyType], $_GET[$valueType]];
59+
}
60+
61+
/**
62+
* @template T of non-empty-array<key-of<Types>>
63+
* @param T $types
64+
* @return Types[value-of<T>]
65+
*/
66+
public static function or(array $types): mixed
67+
{
68+
foreach ($types as $type) {
69+
if ($_GET['thing'] === $type) {
70+
return self::fake($type);
71+
}
72+
}
73+
return self::fake($types[0]);
74+
}
75+
}

tests/TypeInferenceTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function dataFileAsserts(): iterable
4444
yield from $this->gatherAssertTypes(__DIR__ . '/data/paginate_links.php');
4545
yield from $this->gatherAssertTypes(__DIR__ . '/data/rest_ensure_response.php');
4646
yield from $this->gatherAssertTypes(__DIR__ . '/data/size_format.php');
47+
yield from $this->gatherAssertTypes(__DIR__ . '/data/stripslashes.php');
4748
yield from $this->gatherAssertTypes(__DIR__ . '/data/term_exists.php');
4849
yield from $this->gatherAssertTypes(__DIR__ . '/data/validate_file.php');
4950
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_debug_backtrace_summary.php');

tests/data/stripslashes.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
use function PHPStan\Testing\assertType;
8+
use function stripslashes_deep;
9+
use function stripslashes_from_strings_only;
10+
11+
assertType('null', stripslashes_deep(null));
12+
assertType('bool', stripslashes_deep(Faker::fake('bool')));
13+
assertType('int', stripslashes_deep(Faker::fake('int')));
14+
assertType('float', stripslashes_deep(Faker::fake('float')));
15+
assertType('string', stripslashes_deep(Faker::fake('string')));
16+
assertType('array', stripslashes_deep(Faker::fake('array')));
17+
assertType('resource', stripslashes_deep(Faker::fake('resource')));
18+
assertType('object', stripslashes_deep(Faker::fake('object')));
19+
20+
assertType('null', stripslashes_from_strings_only(null));
21+
assertType('bool', stripslashes_from_strings_only(Faker::fake('bool')));
22+
assertType('int', stripslashes_from_strings_only(Faker::fake('int')));
23+
assertType('float', stripslashes_from_strings_only(Faker::fake('float')));
24+
assertType('string', stripslashes_from_strings_only(Faker::fake('string')));
25+
assertType('array', stripslashes_from_strings_only(Faker::fake('array')));
26+
assertType('resource', stripslashes_from_strings_only(Faker::fake('resource')));
27+
assertType('object', stripslashes_from_strings_only(Faker::fake('object')));

wordpress-stubs.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106242,6 +106242,9 @@ function stripslashes_deep($value)
106242106242
*
106243106243
* @param mixed $value The array or string to be stripped.
106244106244
* @return mixed The stripped value.
106245+
* @phpstan-template T
106246+
* @phpstan-param T $value
106247+
* @phpstan-return T
106245106248
*/
106246106249
function stripslashes_from_strings_only($value)
106247106250
{

0 commit comments

Comments
 (0)