Skip to content

Commit 513dd16

Browse files
committed
Modernize to PHP 8.4
1 parent c775e26 commit 513dd16

File tree

9 files changed

+112
-222
lines changed

9 files changed

+112
-222
lines changed

src/ReCaptcha/ReCaptcha.php

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This is a PHP library that handles calling reCAPTCHA.
45
*
@@ -43,7 +44,7 @@ class ReCaptcha
4344
* Version of this client library.
4445
* @const string
4546
*/
46-
public const VERSION = 'php_1.3.0';
47+
public const VERSION = 'php_2.0.0';
4748

4849
/**
4950
* URL for reCAPTCHA siteverify API
@@ -115,28 +116,28 @@ class ReCaptcha
115116
* Shared secret for the site.
116117
* @var string
117118
*/
118-
private $secret;
119+
private string $secret;
119120

120121
/**
121122
* Method used to communicate with service. Defaults to POST request.
122123
* @var RequestMethod
123124
*/
124-
private $requestMethod;
125+
private ?RequestMethod $requestMethod;
125126

126-
private $hostname;
127-
private $apkPackageName;
128-
private $action;
129-
private $threshold;
130-
private $timeoutSeconds;
127+
private string $hostname;
128+
private string $apkPackageName;
129+
private string $action;
130+
private float $threshold;
131+
private int $timeoutSeconds;
131132

132133
/**
133134
* Create a configured instance to use the reCAPTCHA service.
134135
*
135136
* @param string $secret The shared key between your site and reCAPTCHA.
136-
* @param RequestMethod $requestMethod method used to send the request. Defaults to POST.
137+
* @param ?RequestMethod $requestMethod method used to send the request. Defaults to POST.
137138
* @throws \RuntimeException if $secret is invalid
138139
*/
139-
public function __construct($secret, ?RequestMethod $requestMethod = null)
140+
public function __construct(string $secret, ?RequestMethod $requestMethod = null)
140141
{
141142
if (empty($secret)) {
142143
throw new \RuntimeException('No secret provided');
@@ -156,20 +157,21 @@ public function __construct($secret, ?RequestMethod $requestMethod = null)
156157
*
157158
* @param string $response The user response token provided by reCAPTCHA, verifying the user on your site.
158159
* @param string $remoteIp The end user's IP address.
160+
*
159161
* @return Response Response from the service.
160162
*/
161-
public function verify(string $response, ?string $remoteIp = null)
163+
public function verify(string $response, ?string $remoteIp = null): Response
162164
{
163165
// Discard empty solution submissions
164166
if (empty($response)) {
165-
$recaptchaResponse = new Response(false, array(self::E_MISSING_INPUT_RESPONSE));
167+
$recaptchaResponse = new Response(false, [self::E_MISSING_INPUT_RESPONSE]);
166168
return $recaptchaResponse;
167169
}
168170

169171
$params = new RequestParameters($this->secret, $response, $remoteIp, self::VERSION);
170172
$rawResponse = $this->requestMethod->submit($params);
171173
$initialResponse = Response::fromJson($rawResponse);
172-
$validationErrors = array();
174+
$validationErrors = [];
173175

174176
if (isset($this->hostname) && strcasecmp($this->hostname, $initialResponse->getHostname()) !== 0) {
175177
$validationErrors[] = self::E_HOSTNAME_MISMATCH;
@@ -215,23 +217,23 @@ public function verify(string $response, ?string $remoteIp = null)
215217
* This should be without a protocol or trailing slash, e.g. www.google.com
216218
*
217219
* @param string $hostname Expected hostname
218-
* @return ReCaptcha Current instance for fluent interface
219220
*/
220-
public function setExpectedHostname(string $hostname)
221+
public function setExpectedHostname(string $hostname): static
221222
{
222223
$this->hostname = $hostname;
224+
223225
return $this;
224226
}
225227

226228
/**
227229
* Provide an APK package name to match against in verify()
228230
*
229231
* @param string $apkPackageName Expected APK package name
230-
* @return ReCaptcha Current instance for fluent interface
231232
*/
232-
public function setExpectedApkPackageName(string $apkPackageName)
233+
public function setExpectedApkPackageName(string $apkPackageName): static
233234
{
234235
$this->apkPackageName = $apkPackageName;
236+
235237
return $this;
236238
}
237239

@@ -240,11 +242,11 @@ public function setExpectedApkPackageName(string $apkPackageName)
240242
* This should be set per page.
241243
*
242244
* @param string $action Expected action
243-
* @return ReCaptcha Current instance for fluent interface
244245
*/
245-
public function setExpectedAction(string $action)
246+
public function setExpectedAction(string $action): static
246247
{
247248
$this->action = $action;
249+
248250
return $this;
249251
}
250252

@@ -253,23 +255,23 @@ public function setExpectedAction(string $action)
253255
* Threshold should be a float between 0 and 1 which will be tested as response >= threshold.
254256
*
255257
* @param float $threshold Expected threshold
256-
* @return ReCaptcha Current instance for fluent interface
257258
*/
258-
public function setScoreThreshold(float $threshold)
259+
public function setScoreThreshold(float $threshold): static
259260
{
260261
$this->threshold = floatval($threshold);
262+
261263
return $this;
262264
}
263265

264266
/**
265267
* Provide a timeout in seconds to test against the challenge timestamp in verify()
266268
*
267269
* @param int $timeoutSeconds Expected hostname
268-
* @return ReCaptcha Current instance for fluent interface
269270
*/
270-
public function setChallengeTimeout(int $timeoutSeconds)
271+
public function setChallengeTimeout(int $timeoutSeconds): static
271272
{
272273
$this->timeoutSeconds = $timeoutSeconds;
274+
273275
return $this;
274276
}
275277
}

src/ReCaptcha/RequestMethod.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This is a PHP library that handles calling reCAPTCHA.
45
*
@@ -45,5 +46,5 @@ interface RequestMethod
4546
* @param RequestParameters $params Request parameters
4647
* @return string Body of the reCAPTCHA response
4748
*/
48-
public function submit(RequestParameters $params);
49+
public function submit(RequestParameters $params): string;
4950
}

src/ReCaptcha/RequestMethod/Curl.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This is a PHP library that handles calling reCAPTCHA.
45
*
@@ -41,8 +42,8 @@ class Curl
4142
{
4243
/**
4344
* @see http://php.net/curl_init
44-
* @param string $url
45-
* @return resource cURL handle
45+
*
46+
* @return resource cURL handle *
4647
*/
4748
public function init(?string $url = null)
4849
{
@@ -52,20 +53,17 @@ public function init(?string $url = null)
5253
/**
5354
* @see http://php.net/curl_setopt_array
5455
* @param resource $ch
55-
* @param array $options
56-
* @return bool
5756
*/
58-
public function setoptArray($ch, array $options)
57+
public function setoptArray($ch, array $options): bool
5958
{
6059
return curl_setopt_array($ch, $options);
6160
}
6261

6362
/**
6463
* @see http://php.net/curl_exec
6564
* @param resource $ch
66-
* @return mixed
6765
*/
68-
public function exec($ch)
66+
public function exec($ch): mixed
6967
{
7068
return curl_exec($ch);
7169
}
@@ -74,7 +72,7 @@ public function exec($ch)
7472
* @see http://php.net/curl_close
7573
* @param resource $ch
7674
*/
77-
public function close($ch)
75+
public function close($ch): void
7876
{
7977
curl_close($ch);
8078
}

src/ReCaptcha/RequestMethod/CurlPost.php

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This is a PHP library that handles calling reCAPTCHA.
45
*
@@ -45,51 +46,44 @@
4546
*/
4647
class CurlPost implements RequestMethod
4748
{
48-
/**
49-
* Curl connection to the reCAPTCHA service
50-
* @var Curl
51-
*/
52-
private $curl;
53-
54-
/**
55-
* URL for reCAPTCHA siteverify API
56-
* @var string
57-
*/
58-
private $siteVerifyUrl;
59-
6049
/**
6150
* Only needed if you want to override the defaults
6251
*
63-
* @param Curl $curl Curl resource
64-
* @param string $siteVerifyUrl URL for reCAPTCHA siteverify API
52+
* @param ?Curl $curl Curl connection to the reCAPTCHA service
53+
* @param ?string $siteVerifyUrl URL for reCAPTCHA siteverify API
6554
*/
66-
public function __construct(?Curl $curl = null, ?string $siteVerifyUrl = null)
55+
public function __construct(private ?Curl $curl = null, private ?string $siteVerifyUrl = null)
6756
{
68-
$this->curl = (is_null($curl)) ? new Curl() : $curl;
69-
$this->siteVerifyUrl = (is_null($siteVerifyUrl)) ? ReCaptcha::SITE_VERIFY_URL : $siteVerifyUrl;
57+
if (is_null($curl)) {
58+
$this->curl = new Curl();
59+
}
60+
if (is_null($siteVerifyUrl)) {
61+
$this->siteVerifyUrl = ReCaptcha::SITE_VERIFY_URL;
62+
}
7063
}
7164

7265
/**
7366
* Submit the cURL request with the specified parameters.
7467
*
7568
* @param RequestParameters $params Request parameters
69+
*
7670
* @return string Body of the reCAPTCHA response
7771
*/
78-
public function submit(RequestParameters $params)
72+
public function submit(RequestParameters $params): string
7973
{
8074
$handle = $this->curl->init($this->siteVerifyUrl);
8175

82-
$options = array(
76+
$options = [
8377
CURLOPT_POST => true,
8478
CURLOPT_POSTFIELDS => $params->toQueryString(),
85-
CURLOPT_HTTPHEADER => array(
79+
CURLOPT_HTTPHEADER => [
8680
'Content-Type: application/x-www-form-urlencoded'
87-
),
81+
],
8882
CURLINFO_HEADER_OUT => false,
8983
CURLOPT_HEADER => false,
9084
CURLOPT_RETURNTRANSFER => true,
9185
CURLOPT_SSL_VERIFYPEER => true
92-
);
86+
];
9387
$this->curl->setoptArray($handle, $options);
9488

9589
$response = $this->curl->exec($handle);

src/ReCaptcha/RequestMethod/Post.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This is a PHP library that handles calling reCAPTCHA.
45
*
@@ -43,43 +44,40 @@
4344
*/
4445
class Post implements RequestMethod
4546
{
46-
/**
47-
* URL for reCAPTCHA siteverify API
48-
* @var string
49-
*/
50-
private $siteVerifyUrl;
51-
5247
/**
5348
* Only needed if you want to override the defaults
5449
*
5550
* @param string $siteVerifyUrl URL for reCAPTCHA siteverify API
5651
*/
57-
public function __construct(?string $siteVerifyUrl = null)
52+
public function __construct(private ?string $siteVerifyUrl = null)
5853
{
59-
$this->siteVerifyUrl = (is_null($siteVerifyUrl)) ? ReCaptcha::SITE_VERIFY_URL : $siteVerifyUrl;
54+
if (is_null($siteVerifyUrl)) {
55+
$this->siteVerifyUrl = ReCaptcha::SITE_VERIFY_URL;
56+
}
6057
}
6158

6259
/**
6360
* Submit the POST request with the specified parameters.
6461
*
6562
* @param RequestParameters $params Request parameters
63+
*
6664
* @return string Body of the reCAPTCHA response
6765
*/
68-
public function submit(RequestParameters $params)
66+
public function submit(RequestParameters $params): string
6967
{
70-
$options = array(
71-
'http' => array(
68+
$options = [
69+
'http' => [
7270
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
7371
'method' => 'POST',
7472
'content' => $params->toQueryString(),
7573
// Force the peer to validate (not needed in 5.6.0+, but still works)
7674
'verify_peer' => true,
77-
),
78-
);
75+
],
76+
];
7977
$context = stream_context_create($options);
8078
$response = file_get_contents($this->siteVerifyUrl, false, $context);
8179

82-
if ($response !== false) {
80+
if ($response) {
8381
return $response;
8482
}
8583

0 commit comments

Comments
 (0)