Skip to content

Commit

Permalink
Add conditional return type for validate_file (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanDelMar authored Sep 26, 2024
1 parent 7d957a9 commit 14ae80f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
'stripslashes_deep' => ['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))"],
'wp_die' => ['($args is array{exit: false} ? void : never))'],
'wp_dropdown_languages' => ["(\$args is array{id: null|''} ? void : (\$args is array{name: null|''} ? void : string))"],
'wp_clear_scheduled_hook' => ['(0|positive-int|($wp_error is false ? false : \WP_Error))', 'args' => $cronArgsType],
Expand Down
1 change: 1 addition & 0 deletions tests/TypeInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function dataFileAsserts(): iterable
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/term_exists.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/validate_file.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_debug_backtrace_summary.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_die.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_dropdown_languages.php');
Expand Down
23 changes: 23 additions & 0 deletions tests/data/validate_file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

use function validate_file;
use function PHPStan\Testing\assertType;

/** @var array<string> $allowedFiles */
$allowedFiles = $_GET['allowedFiles'];

assertType('0|1|2', validate_file((string)$_GET['file']));
assertType('0|1|2', validate_file((string)$_GET['file'], []));
assertType('0|1|2|3', validate_file((string)$_GET['file'], $allowedFiles));

assertType('0', validate_file(''));
assertType('0', validate_file('', []));
assertType('0', validate_file('', $allowedFiles));

assertType('0|1|2', validate_file('file2'));
assertType('0|1|2', validate_file('file2', []));
assertType('0|1|2|3', validate_file('file2', $allowedFiles));
1 change: 1 addition & 0 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -109741,6 +109741,7 @@ function iis7_supports_permalinks()
* @param string $file File path.
* @param string[] $allowed_files Optional. Array of allowed files. Default empty array.
* @return int 0 means nothing is wrong, greater than 0 means something was wrong.
* @phpstan-return ($file is '' ? 0 : ($allowed_files is empty ? 0|1|2 : 0|1|2|3))
*/
function validate_file($file, $allowed_files = array())
{
Expand Down

0 comments on commit 14ae80f

Please sign in to comment.