Skip to content

Commit

Permalink
Add dynamic return type for stripslashes (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanDelMar authored Sep 26, 2024
1 parent fa9e550 commit 19596c3
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'sanitize_post' => ['T', '@phpstan-template' => 'T of array|object', 'post' => 'T'],
'sanitize_term' => ['T', '@phpstan-template' => 'T of array|object', 'term' => 'T'],
'stripslashes_deep' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
'stripslashes_from_strings_only' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
'urldecode_deep' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
'urlencode_deep' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
'validate_file' => ["(\$file is '' ? 0 : (\$allowed_files is empty ? 0|1|2 : 0|1|2|3))"],
Expand Down
75 changes: 75 additions & 0 deletions tests/Faker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

/**
* @phpstan-type Types array{
* bool: bool,
* int: int,
* float: float,
* string: string,
* array: array<mixed>,
* resource: resource,
* object: object,
* numeric-string: numeric-string,
* null: null,
* mixed: mixed,
* true: true,
* false: false,
* callable: callable,
* iterable: iterable<mixed>,
* array-key: array-key,
* positive-int: positive-int,
* negative-int: negative-int,
* non-positive-int: non-positive-int,
* non-negative-int: non-negative-int,
* non-zero-int: non-zero-int,
* }
*/
class Faker
{
/**
* @var Types $types
* @phpstan-ignore-next-line
*/
private static $types;

/**
* @template T of string
* @param T $type
* @return Types[T]
*/
public static function fake(string $type): mixed
{
return self::$types[$type];
}

/**
* @template T of string
* @template K of string
* @param T $valueType
* @param K $keyType
* @return array<Types[K], Types[T]>
*/
public static function fakeArray(string $valueType, string $keyType = 'array-key'): mixed
{
return [$_GET[$keyType], $_GET[$valueType]];
}

/**
* @template T of non-empty-array<key-of<Types>>
* @param T $types
* @return Types[value-of<T>]
*/
public static function or(array $types): mixed
{
foreach ($types as $type) {
if ($_GET['thing'] === $type) {
return self::fake($type);
}
}
return self::fake($types[0]);
}
}
1 change: 1 addition & 0 deletions tests/TypeInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/paginate_links.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/rest_ensure_response.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/size_format.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/stripslashes.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/term_exists.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/validate_file.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_debug_backtrace_summary.php');
Expand Down
27 changes: 27 additions & 0 deletions tests/data/stripslashes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

use function PHPStan\Testing\assertType;
use function stripslashes_deep;
use function stripslashes_from_strings_only;

assertType('null', stripslashes_deep(null));
assertType('bool', stripslashes_deep(Faker::fake('bool')));
assertType('int', stripslashes_deep(Faker::fake('int')));
assertType('float', stripslashes_deep(Faker::fake('float')));
assertType('string', stripslashes_deep(Faker::fake('string')));
assertType('array', stripslashes_deep(Faker::fake('array')));
assertType('resource', stripslashes_deep(Faker::fake('resource')));
assertType('object', stripslashes_deep(Faker::fake('object')));

assertType('null', stripslashes_from_strings_only(null));
assertType('bool', stripslashes_from_strings_only(Faker::fake('bool')));
assertType('int', stripslashes_from_strings_only(Faker::fake('int')));
assertType('float', stripslashes_from_strings_only(Faker::fake('float')));
assertType('string', stripslashes_from_strings_only(Faker::fake('string')));
assertType('array', stripslashes_from_strings_only(Faker::fake('array')));
assertType('resource', stripslashes_from_strings_only(Faker::fake('resource')));
assertType('object', stripslashes_from_strings_only(Faker::fake('object')));
3 changes: 3 additions & 0 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -106242,6 +106242,9 @@ function stripslashes_deep($value)
*
* @param mixed $value The array or string to be stripped.
* @return mixed The stripped value.
* @phpstan-template T
* @phpstan-param T $value
* @phpstan-return T
*/
function stripslashes_from_strings_only($value)
{
Expand Down

0 comments on commit 19596c3

Please sign in to comment.