From 14ae80f1cb0ff05773b0fa56527c54da6ff117b5 Mon Sep 17 00:00:00 2001 From: IanDelMar <42134098+IanDelMar@users.noreply.github.com> Date: Thu, 26 Sep 2024 03:16:44 +0200 Subject: [PATCH] Add conditional return type for validate_file (#229) --- functionMap.php | 1 + tests/TypeInferenceTest.php | 1 + tests/data/validate_file.php | 23 +++++++++++++++++++++++ wordpress-stubs.php | 1 + 4 files changed, 26 insertions(+) create mode 100644 tests/data/validate_file.php diff --git a/functionMap.php b/functionMap.php index 43a7e3b..ce391a6 100644 --- a/functionMap.php +++ b/functionMap.php @@ -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], diff --git a/tests/TypeInferenceTest.php b/tests/TypeInferenceTest.php index 843101c..a468797 100644 --- a/tests/TypeInferenceTest.php +++ b/tests/TypeInferenceTest.php @@ -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'); diff --git a/tests/data/validate_file.php b/tests/data/validate_file.php new file mode 100644 index 0000000..c2cf4df --- /dev/null +++ b/tests/data/validate_file.php @@ -0,0 +1,23 @@ + $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)); diff --git a/wordpress-stubs.php b/wordpress-stubs.php index fc661d8..042efda 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -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()) {