From 92986093cb86cbdde5f68259b42f60c34f00e4ef Mon Sep 17 00:00:00 2001 From: Kent Rasmussen Date: Mon, 15 Jan 2024 13:22:18 +0900 Subject: [PATCH 1/5] refactor: Add RecaptchaServiceInterface Signed-off-by: Kent Rasmussen --- src/ReCaptcha.php | 71 +++++++++++-------------------- src/RecaptchaServiceInterface.php | 60 ++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 46 deletions(-) create mode 100644 src/RecaptchaServiceInterface.php diff --git a/src/ReCaptcha.php b/src/ReCaptcha.php index cf1a3db..ab01386 100644 --- a/src/ReCaptcha.php +++ b/src/ReCaptcha.php @@ -19,11 +19,11 @@ use const E_USER_WARNING; /** - * Render and verify ReCaptchas + * Render and verify v2 ReCaptchas * * @final This class should not be extended and will be marked final in version 4.0 */ -class ReCaptcha implements Stringable +class ReCaptcha implements RecaptchaServiceInterface, Stringable { /** * URI to the API @@ -185,13 +185,10 @@ public function getIp() } /** - * Set a single parameter - * - * @param string $key - * @param mixed $value - * @return self + * @inheritDoc + * @return $this */ - public function setParam($key, $value) + public function setParam($key, $value): self { $this->params[$key] = $value; @@ -227,11 +224,9 @@ public function setParams($params) } /** - * Get the parameter array - * - * @return array + * @inheritDoc */ - public function getParams() + public function getParams(): array { return $this->params; } @@ -252,13 +247,10 @@ public function getParam($key) } /** - * Set a single option - * - * @param string $key - * @param mixed $value - * @return self + * @inheritDoc + * @return $this */ - public function setOption($key, $value) + public function setOption($key, $value): self { $this->options[$key] = $value; @@ -269,7 +261,7 @@ public function setOption($key, $value) * Set options * * @param iterable $options - * @return self + * @return $this * @throws Exception */ public function setOptions($options) @@ -290,11 +282,9 @@ public function setOptions($options) } /** - * Get the options array - * - * @return array + * @inheritDoc */ - public function getOptions() + public function getOptions(): array { return $this->options; } @@ -315,22 +305,18 @@ public function getOption($key) } /** - * Get the site key - * - * @return string + * @inheritDoc */ - public function getSiteKey() + public function getSiteKey(): string { return $this->siteKey; } /** - * Set the site key - * - * @param string $siteKey - * @return self + * @inheritDoc + * @return $this */ - public function setSiteKey($siteKey) + public function setSiteKey($siteKey): self { $this->siteKey = $siteKey; @@ -338,22 +324,18 @@ public function setSiteKey($siteKey) } /** - * Get the secret key - * - * @return string + * @inheritDoc */ - public function getSecretKey() + public function getSecretKey(): string { return $this->secretKey; } /** - * Set the secret key - * - * @param string $secretKey - * @return self + * @inheritDoc + * @return $this */ - public function setSecretKey($secretKey) + public function setSecretKey($secretKey): self { $this->secretKey = $secretKey; @@ -478,15 +460,12 @@ protected function post($responseField) } /** - * Verify the user input + * {@inheritDoc} * * This method calls up the post method and returns a * \Laminas\ReCaptcha\Response object. - * - * @param string $responseField - * @return Response */ - public function verify($responseField) + public function verify(string $responseField): Response { $response = $this->post($responseField); return new Response(null, null, $response); diff --git a/src/RecaptchaServiceInterface.php b/src/RecaptchaServiceInterface.php new file mode 100644 index 0000000..9143152 --- /dev/null +++ b/src/RecaptchaServiceInterface.php @@ -0,0 +1,60 @@ + + */ + public function getOptions(): array; + + /** + * Set a single option + */ + public function setOption(string $key, mixed $value): self; + + /** + * Get the parameter array + * + * @return array + */ + public function getParams(): array; + + /** + * Set a single parameter + */ + public function setParam(string $key, mixed $value): self; + + /** + * Get the site key + */ + public function getSiteKey(): string; + + /** + * Set the site key + */ + public function setSiteKey(string $siteKey): self; + + /** + * Get the secret key + */ + public function getSecretKey(): string; + + /** + * Set the secret key + */ + public function setSecretKey(string $secretKey): self; + + /** + * Verify the user input + */ + public function verify(string $responseField): Response; +} From 6240b478c35979e141bf2121d2130698c4b28742 Mon Sep 17 00:00:00 2001 From: Kent Rasmussen Date: Thu, 15 Feb 2024 09:55:17 +0900 Subject: [PATCH 2/5] fix: psalm type errors Signed-off-by: Kent Rasmussen --- src/ReCaptcha.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ReCaptcha.php b/src/ReCaptcha.php index ab01386..272054c 100644 --- a/src/ReCaptcha.php +++ b/src/ReCaptcha.php @@ -188,7 +188,7 @@ public function getIp() * @inheritDoc * @return $this */ - public function setParam($key, $value): self + public function setParam(string $key, mixed $value): self { $this->params[$key] = $value; @@ -250,7 +250,7 @@ public function getParam($key) * @inheritDoc * @return $this */ - public function setOption($key, $value): self + public function setOption(string $key, mixed $value): self { $this->options[$key] = $value; @@ -316,7 +316,7 @@ public function getSiteKey(): string * @inheritDoc * @return $this */ - public function setSiteKey($siteKey): self + public function setSiteKey(string $siteKey): self { $this->siteKey = $siteKey; @@ -335,7 +335,7 @@ public function getSecretKey(): string * @inheritDoc * @return $this */ - public function setSecretKey($secretKey): self + public function setSecretKey(string $secretKey): self { $this->secretKey = $secretKey; From 1f4ee6e691446eec608bae07cff7bd80ceeb9cee Mon Sep 17 00:00:00 2001 From: Kent Rasmussen Date: Fri, 16 Feb 2024 09:12:06 +0900 Subject: [PATCH 3/5] fix: remove types for BC Signed-off-by: Kent Rasmussen --- src/ReCaptcha.php | 23 ++++++++---------- src/RecaptchaServiceInterface.php | 39 ++++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/ReCaptcha.php b/src/ReCaptcha.php index 272054c..dd4ab7f 100644 --- a/src/ReCaptcha.php +++ b/src/ReCaptcha.php @@ -188,7 +188,7 @@ public function getIp() * @inheritDoc * @return $this */ - public function setParam(string $key, mixed $value): self + public function setParam($key, $value) { $this->params[$key] = $value; @@ -226,7 +226,7 @@ public function setParams($params) /** * @inheritDoc */ - public function getParams(): array + public function getParams() { return $this->params; } @@ -248,9 +248,8 @@ public function getParam($key) /** * @inheritDoc - * @return $this */ - public function setOption(string $key, mixed $value): self + public function setOption($key, $value) { $this->options[$key] = $value; @@ -284,7 +283,7 @@ public function setOptions($options) /** * @inheritDoc */ - public function getOptions(): array + public function getOptions() { return $this->options; } @@ -307,16 +306,15 @@ public function getOption($key) /** * @inheritDoc */ - public function getSiteKey(): string + public function getSiteKey() { return $this->siteKey; } /** * @inheritDoc - * @return $this */ - public function setSiteKey(string $siteKey): self + public function setSiteKey($siteKey) { $this->siteKey = $siteKey; @@ -326,16 +324,15 @@ public function setSiteKey(string $siteKey): self /** * @inheritDoc */ - public function getSecretKey(): string + public function getSecretKey() { return $this->secretKey; } /** * @inheritDoc - * @return $this */ - public function setSecretKey(string $secretKey): self + public function setSecretKey($secretKey) { $this->secretKey = $secretKey; @@ -465,9 +462,9 @@ protected function post($responseField) * This method calls up the post method and returns a * \Laminas\ReCaptcha\Response object. */ - public function verify(string $responseField): Response + public function verify($responseField) { $response = $this->post($responseField); - return new Response(null, null, $response); + return new Response(null, [], $response); } } diff --git a/src/RecaptchaServiceInterface.php b/src/RecaptchaServiceInterface.php index 9143152..0c1da44 100644 --- a/src/RecaptchaServiceInterface.php +++ b/src/RecaptchaServiceInterface.php @@ -14,47 +14,68 @@ interface RecaptchaServiceInterface * * @return array */ - public function getOptions(): array; + public function getOptions(); /** * Set a single option + * + * @param string $key + * @param mixed $value + * @return $this */ - public function setOption(string $key, mixed $value): self; + public function setOption($key, $value); /** * Get the parameter array * * @return array */ - public function getParams(): array; + public function getParams(); /** * Set a single parameter + * + * @param string $key + * @param mixed $value + * @return $this */ - public function setParam(string $key, mixed $value): self; + public function setParam($key, $value); /** * Get the site key + * + * @return string */ - public function getSiteKey(): string; + public function getSiteKey(); /** * Set the site key + * + * @param string $siteKey + * @return $this */ - public function setSiteKey(string $siteKey): self; + public function setSiteKey($siteKey); /** * Get the secret key + * + * @return string */ - public function getSecretKey(): string; + public function getSecretKey(); /** * Set the secret key + * + * @param string $secretKey + * @return $this */ - public function setSecretKey(string $secretKey): self; + public function setSecretKey($secretKey); /** * Verify the user input + * + * @param string $responseField + * @return Response */ - public function verify(string $responseField): Response; + public function verify($responseField); } From 2ec9d374ecfa812d07c0542d61e748a42ee07ea6 Mon Sep 17 00:00:00 2001 From: Kent Rasmussen Date: Fri, 16 Feb 2024 09:16:21 +0900 Subject: [PATCH 4/5] fix: missed psalm type error Signed-off-by: Kent Rasmussen --- src/ReCaptcha.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ReCaptcha.php b/src/ReCaptcha.php index dd4ab7f..18b82e1 100644 --- a/src/ReCaptcha.php +++ b/src/ReCaptcha.php @@ -186,7 +186,6 @@ public function getIp() /** * @inheritDoc - * @return $this */ public function setParam($key, $value) { From 81d60b20418f20aee752c1cd62ec0443131aa424 Mon Sep 17 00:00:00 2001 From: Kent Rasmussen Date: Thu, 21 Mar 2024 10:38:17 +0900 Subject: [PATCH 5/5] fix: ReCaptchaServiceInterface casing --- src/ReCaptcha.php | 2 +- ...aptchaServiceInterface.php => ReCaptchaServiceInterface.php} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/{RecaptchaServiceInterface.php => ReCaptchaServiceInterface.php} (97%) diff --git a/src/ReCaptcha.php b/src/ReCaptcha.php index 18b82e1..225a334 100644 --- a/src/ReCaptcha.php +++ b/src/ReCaptcha.php @@ -23,7 +23,7 @@ * * @final This class should not be extended and will be marked final in version 4.0 */ -class ReCaptcha implements RecaptchaServiceInterface, Stringable +class ReCaptcha implements ReCaptchaServiceInterface, Stringable { /** * URI to the API diff --git a/src/RecaptchaServiceInterface.php b/src/ReCaptchaServiceInterface.php similarity index 97% rename from src/RecaptchaServiceInterface.php rename to src/ReCaptchaServiceInterface.php index 0c1da44..f80dd72 100644 --- a/src/RecaptchaServiceInterface.php +++ b/src/ReCaptchaServiceInterface.php @@ -7,7 +7,7 @@ /** * An interface for interacting with a recaptcha service provider */ -interface RecaptchaServiceInterface +interface ReCaptchaServiceInterface { /** * Get the options array