From 8f07bb8983f09c30a87aae7a6bef655d31f7fb86 Mon Sep 17 00:00:00 2001 From: Fulvio Notarstefano Date: Mon, 17 Jun 2024 11:48:00 +0200 Subject: [PATCH 1/5] Ignore throws in function mocks --- php/WP_Mock/API/function-mocks.php | 60 +++++++++++++++--------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/php/WP_Mock/API/function-mocks.php b/php/WP_Mock/API/function-mocks.php index 72ebec0..b6c51b2 100644 --- a/php/WP_Mock/API/function-mocks.php +++ b/php/WP_Mock/API/function-mocks.php @@ -149,11 +149,11 @@ function esc_textarea() if (! function_exists('__')) { /** - * @return string|mixed - * @throws ExpectationFailedException|Exception + * @return string */ - function __() + function __() : string { + /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } @@ -161,32 +161,32 @@ function __() if (! function_exists('_e')) { /** * @return void - * @throws ExpectationFailedException|Exception */ - function _e(): void + function _e() : void { + /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ Handler::handlePredefinedEchoFunction(__FUNCTION__, func_get_args()); } } if (! function_exists('_x')) { /** - * @return string|mixed - * @throws ExpectationFailedException|Exception + * @return string */ - function _x() + function _x() : string { + /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } if (! function_exists('esc_html__')) { /** - * @return string|mixed - * @throws ExpectationFailedException|Exception + * @return string */ - function esc_html__() + function esc_html__() : string { + /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } @@ -194,32 +194,32 @@ function esc_html__() if (! function_exists('esc_html_e')) { /** * @return void - * @throws ExpectationFailedException|Exception */ - function esc_html_e(): void + function esc_html_e() : void { + /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ Handler::handlePredefinedEchoFunction(__FUNCTION__, func_get_args()); } } if (! function_exists('esc_html_x')) { /** - * @return string|mixed - * @throws ExpectationFailedException|Exception + * @return string */ - function esc_html_x() + function esc_html_x() : string { + /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } if (! function_exists('esc_attr__')) { /** - * @return string|mixed - * @throws ExpectationFailedException|Exception + * @return string */ - function esc_attr__() + function esc_attr__() : string { + /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } @@ -227,42 +227,44 @@ function esc_attr__() if (! function_exists('esc_attr_e')) { /** * @return void - * @throws ExpectationFailedException|Exception */ - function esc_attr_e(): void + function esc_attr_e() : void { + /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ Handler::handlePredefinedEchoFunction(__FUNCTION__, func_get_args()); } } if (! function_exists('esc_attr_x')) { /** - * @return string|mixed - * @throws ExpectationFailedException + * @return string */ - function esc_attr_x() + function esc_attr_x() : string { + /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } if (! function_exists('_n')) { /** - * @return string|mixed singular or plural string based on number - * @throws ExpectationFailedException if too few arguments passed + * @return string */ - function _n() + function _n() : string { $args = func_get_args(); if (count($args) >= 3) { /** @phpstan-ignore-next-line */ if (isset($args[0]) && 1 >= intval($args[2])) { - return $args[0]; + /** @phpstan-ignore-next-line */ + return (string) $args[0]; } else { - return $args[1]; + /** @phpstan-ignore-next-line */ + return (string) $args[1]; } } else { + /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ throw new ExpectationFailedException(sprintf('Too few arguments to function %s', __FUNCTION__)); } } From 3627f10a4d2fe874115bd94034359f4de4adbeab Mon Sep 17 00:00:00 2001 From: Fulvio Notarstefano Date: Tue, 25 Jun 2024 01:29:54 +0900 Subject: [PATCH 2/5] Tweak exclusions --- php/WP_Mock/API/function-mocks.php | 36 +++++++++++++++--------------- php/WP_Mock/Functions/Handler.php | 3 ++- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/php/WP_Mock/API/function-mocks.php b/php/WP_Mock/API/function-mocks.php index b6c51b2..96fc63d 100644 --- a/php/WP_Mock/API/function-mocks.php +++ b/php/WP_Mock/API/function-mocks.php @@ -83,66 +83,66 @@ function apply_filters($tag, $value) if (! function_exists('esc_html')) { /** - * @return string|mixed - * @throws ExpectationFailedException|Exception + * @return string */ - function esc_html() + function esc_html() : string { + /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } if (! function_exists('esc_attr')) { /** - * @return string|mixed - * @throws ExpectationFailedException|Exception + * @return string */ - function esc_attr() + function esc_attr() : string { + /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } if (! function_exists('esc_url')) { /** - * @return string|mixed - * @throws ExpectationFailedException|Exception + * @return string */ - function esc_url() + function esc_url() : string { + /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } if (! function_exists('esc_url_raw')) { /** - * @return string|mixed - * @throws ExpectationFailedException|Exception + * @return string */ - function esc_url_raw() + function esc_url_raw() : string { + /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } if (! function_exists('esc_js')) { /** - * @return string|mixed - * @throws ExpectationFailedException|Exception + * @return string */ - function esc_js() + function esc_js() : string { + /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } if (! function_exists('esc_textarea')) { /** - * @return string|mixed - * @throws ExpectationFailedException|Exception + * @return string */ - function esc_textarea() + function esc_textarea() : string { + /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } diff --git a/php/WP_Mock/Functions/Handler.php b/php/WP_Mock/Functions/Handler.php index bc75987..914c3ac 100644 --- a/php/WP_Mock/Functions/Handler.php +++ b/php/WP_Mock/Functions/Handler.php @@ -104,7 +104,7 @@ public static function handlePredefinedReturnFunction(string $functionName, arra * @param string $functionName function name * @param array $args function arguments * @return void - * @throws Exception|ExpectationFailedException + * @throws ExpectationFailedException */ public static function handlePredefinedEchoFunction(string $functionName, array $args = []): void { @@ -115,6 +115,7 @@ public static function handlePredefinedEchoFunction(string $functionName, array } catch (Exception $exception) { ob_end_clean(); + /** @var ExpectationFailedException $exception */ throw $exception; } From 10dcee68e9af683d9f03a4997f6fa5518ddd6ef4 Mon Sep 17 00:00:00 2001 From: Fulvio Notarstefano Date: Tue, 25 Jun 2024 01:34:57 +0900 Subject: [PATCH 3/5] Fix failing test --- tests/Integration/WP_MockTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/WP_MockTest.php b/tests/Integration/WP_MockTest.php index 04461fb..8b23319 100644 --- a/tests/Integration/WP_MockTest.php +++ b/tests/Integration/WP_MockTest.php @@ -175,7 +175,7 @@ public function testMockingOverridesDefaults(): void */ public function testBotchedMocksStillOverridesDefault(): void { - WP_Mock::userFunction('esc_html'); + WP_Mock::userFunction('esc_html')->andReturn(''); /** @phpstan-ignore-next-line function "exists" */ $this->assertEmpty(esc_html('Input')); From 93465a80c6b2346f868ef2784fabf31f6b51646d Mon Sep 17 00:00:00 2001 From: Fulvio Notarstefano Date: Tue, 25 Jun 2024 01:35:47 +0900 Subject: [PATCH 4/5] Fix failing analysis --- tests/Integration/WP_MockTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Integration/WP_MockTest.php b/tests/Integration/WP_MockTest.php index 8b23319..3af7bb3 100644 --- a/tests/Integration/WP_MockTest.php +++ b/tests/Integration/WP_MockTest.php @@ -130,7 +130,6 @@ public function providerCommonFunctionsDefaultFunctionality(): array * @preserveGlobalState disabled * * @return void - * @throws Exception */ public function testDefaultFailsInStrictMode(): void { From d96aa5f8acfaa0f0a51a9547022331c6eba389b9 Mon Sep 17 00:00:00 2001 From: Fulvio Notarstefano Date: Tue, 25 Jun 2024 01:39:47 +0900 Subject: [PATCH 5/5] Update comment --- php/WP_Mock/API/function-mocks.php | 32 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/php/WP_Mock/API/function-mocks.php b/php/WP_Mock/API/function-mocks.php index 96fc63d..1e74915 100644 --- a/php/WP_Mock/API/function-mocks.php +++ b/php/WP_Mock/API/function-mocks.php @@ -87,7 +87,7 @@ function apply_filters($tag, $value) */ function esc_html() : string { - /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ + /** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } @@ -98,7 +98,7 @@ function esc_html() : string */ function esc_attr() : string { - /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ + /** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } @@ -109,7 +109,7 @@ function esc_attr() : string */ function esc_url() : string { - /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ + /** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } @@ -120,7 +120,7 @@ function esc_url() : string */ function esc_url_raw() : string { - /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ + /** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } @@ -131,7 +131,7 @@ function esc_url_raw() : string */ function esc_js() : string { - /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ + /** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } @@ -142,7 +142,7 @@ function esc_js() : string */ function esc_textarea() : string { - /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ + /** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } @@ -153,7 +153,7 @@ function esc_textarea() : string */ function __() : string { - /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ + /** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } @@ -164,7 +164,7 @@ function __() : string */ function _e() : void { - /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ + /** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */ Handler::handlePredefinedEchoFunction(__FUNCTION__, func_get_args()); } } @@ -175,7 +175,7 @@ function _e() : void */ function _x() : string { - /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ + /** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } @@ -186,7 +186,7 @@ function _x() : string */ function esc_html__() : string { - /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ + /** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } @@ -197,7 +197,7 @@ function esc_html__() : string */ function esc_html_e() : void { - /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ + /** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */ Handler::handlePredefinedEchoFunction(__FUNCTION__, func_get_args()); } } @@ -208,7 +208,7 @@ function esc_html_e() : void */ function esc_html_x() : string { - /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ + /** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } @@ -219,7 +219,7 @@ function esc_html_x() : string */ function esc_attr__() : string { - /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ + /** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } @@ -230,7 +230,7 @@ function esc_attr__() : string */ function esc_attr_e() : void { - /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ + /** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */ Handler::handlePredefinedEchoFunction(__FUNCTION__, func_get_args()); } } @@ -241,7 +241,7 @@ function esc_attr_e() : void */ function esc_attr_x() : string { - /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ + /** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */ return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args()); } } @@ -264,7 +264,7 @@ function _n() : string return (string) $args[1]; } } else { - /** @phpstan-ignore-next-line to prevent flagging the function as throwable in codebases requiring WP_Mock */ + /** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */ throw new ExpectationFailedException(sprintf('Too few arguments to function %s', __FUNCTION__)); } }