Skip to content

Commit a6c5041

Browse files
authored
refactor: use readonly classes with promoted constructors for DTOs (#618)
* refactor: use readonly classes with promoted constructors for DTOs * test: drop reflection-based readonly class checks per review
1 parent 5183304 commit a6c5041

File tree

2 files changed

+25
-78
lines changed

2 files changed

+25
-78
lines changed

src/ReCaptcha/RequestParameters.php

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,8 @@
4242
/**
4343
* Stores and formats the parameters for the request to the reCAPTCHA service.
4444
*/
45-
class RequestParameters
45+
readonly class RequestParameters
4646
{
47-
/**
48-
* The shared key between your site and reCAPTCHA.
49-
*/
50-
private string $secret;
51-
52-
/**
53-
* The user response token provided by reCAPTCHA, verifying the user on your site.
54-
*/
55-
private string $response;
56-
57-
/**
58-
* Remote user's IP address.
59-
*/
60-
private ?string $remoteIp;
61-
62-
/**
63-
* Client version.
64-
*/
65-
private ?string $version;
66-
6747
/**
6848
* Initialise parameters.
6949
*
@@ -72,13 +52,12 @@ class RequestParameters
7252
* @param null|string $remoteIp user's IP address
7353
* @param null|string $version version of this client library
7454
*/
75-
public function __construct(string $secret, string $response, ?string $remoteIp = null, ?string $version = null)
76-
{
77-
$this->secret = $secret;
78-
$this->response = $response;
79-
$this->remoteIp = $remoteIp;
80-
$this->version = $version;
81-
}
55+
public function __construct(
56+
private string $secret,
57+
private string $response,
58+
private ?string $remoteIp = null,
59+
private ?string $version = null,
60+
) {}
8261

8362
/**
8463
* Array representation.

src/ReCaptcha/Response.php

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -42,60 +42,28 @@
4242
/**
4343
* The response returned from the service.
4444
*/
45-
class Response
45+
readonly class Response
4646
{
47-
/**
48-
* Success or failure.
49-
*/
50-
private bool $success = false;
51-
52-
/**
53-
* Error code strings.
54-
*
55-
* @var array<string>
56-
*/
57-
private array $errorCodes = [];
58-
59-
/**
60-
* The hostname of the site where the reCAPTCHA was solved.
61-
*/
62-
private string $hostname;
63-
64-
/**
65-
* Timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ).
66-
*/
67-
private string $challengeTs;
68-
69-
/**
70-
* APK package name.
71-
*/
72-
private string $apkPackageName;
73-
74-
/**
75-
* Score assigned to the request.
76-
*/
77-
private ?float $score;
78-
79-
/**
80-
* Action as specified by the page.
81-
*/
82-
private string $action;
83-
8447
/**
8548
* Constructor.
8649
*
87-
* @param array<string> $errorCodes
88-
*/
89-
public function __construct(bool $success, array $errorCodes = [], string $hostname = '', string $challengeTs = '', string $apkPackageName = '', ?float $score = null, string $action = '')
90-
{
91-
$this->success = $success;
92-
$this->hostname = $hostname;
93-
$this->challengeTs = $challengeTs;
94-
$this->apkPackageName = $apkPackageName;
95-
$this->score = $score;
96-
$this->action = $action;
97-
$this->errorCodes = $errorCodes;
98-
}
50+
* @param bool $success success or failure
51+
* @param array<string> $errorCodes error code strings
52+
* @param string $hostname the hostname of the site where the reCAPTCHA was solved
53+
* @param string $challengeTs timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
54+
* @param string $apkPackageName APK package name
55+
* @param ?float $score score assigned to the request
56+
* @param string $action action as specified by the page
57+
*/
58+
public function __construct(
59+
private bool $success,
60+
private array $errorCodes = [],
61+
private string $hostname = '',
62+
private string $challengeTs = '',
63+
private string $apkPackageName = '',
64+
private ?float $score = null,
65+
private string $action = '',
66+
) {}
9967

10068
/**
10169
* Build the response from the expected JSON returned by the service.

0 commit comments

Comments
 (0)