Skip to content

Commit 0b455a9

Browse files
apply code suggestions
Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com>
1 parent 6947685 commit 0b455a9

File tree

6 files changed

+18
-29
lines changed

6 files changed

+18
-29
lines changed

system/Commands/Generators/Views/formrequest.tpl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function rules(): array
3131
// /**
3232
// * Determines if the current user is authorized to make this request.
3333
// */
34-
// public function authorize(): bool
34+
// public function isAuthorized(): bool
3535
// {
3636
// return true;
3737
// }

system/HTTP/FormRequest.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace CodeIgniter\HTTP;
1515

16-
use CodeIgniter\Exceptions\RuntimeException;
1716
use ReflectionNamedType;
1817
use ReflectionParameter;
1918

@@ -36,22 +35,12 @@ abstract class FormRequest
3635

3736
/**
3837
* When called by the framework, the current IncomingRequest is injected
39-
* explicitly. When instantiated manually (e.g. in tests or commands),
40-
* the constructor falls back to service('request').
41-
*
42-
* @throws RuntimeException if used outside of an HTTP request context.
38+
* explicitly. When instantiated manually (e.g. in tests), the constructor
39+
* falls back to service('request').
4340
*/
4441
final public function __construct(?IncomingRequest $request = null)
4542
{
46-
$resolved = $request ?? service('request');
47-
48-
if (! $resolved instanceof IncomingRequest) {
49-
throw new RuntimeException(
50-
static::class . ' requires an IncomingRequest instance, got ' . $resolved::class . '.',
51-
);
52-
}
53-
54-
$this->request = $resolved;
43+
$this->request = $request ?? service('request');
5544
}
5645

5746
/**
@@ -88,12 +77,12 @@ public function messages(): array
8877
*
8978
* Override in subclasses to add authorization logic:
9079
*
91-
* public function authorize(): bool
80+
* public function isAuthorized(): bool
9281
* {
9382
* return auth()->user()->can('create-posts');
9483
* }
9584
*/
96-
public function authorize(): bool
85+
public function isAuthorized(): bool
9786
{
9887
return true;
9988
}
@@ -164,7 +153,7 @@ protected function failedValidation(array $errors): ResponseInterface
164153
}
165154

166155
/**
167-
* Called when the authorize() check returns false. Override to customize.
156+
* Called when the isAuthorized() check returns false. Override to customize.
168157
*/
169158
protected function failedAuthorization(): ResponseInterface
170159
{
@@ -261,7 +250,7 @@ protected function validationData(): array
261250
*/
262251
final public function resolveRequest(): ?ResponseInterface
263252
{
264-
if (! $this->authorize()) {
253+
if (! $this->isAuthorized()) {
265254
return $this->failedAuthorization();
266255
}
267256

tests/_support/HTTP/Requests/UnauthorizedFormRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function rules(): array
2525
return ['title' => 'required'];
2626
}
2727

28-
public function authorize(): bool
28+
public function isAuthorized(): bool
2929
{
3030
return false;
3131
}

tests/system/HTTP/FormRequestTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function testDefaultAuthorizeReturnsTrue(): void
119119
{
120120
$formRequest = $this->makeFormRequest($this->makeRequest());
121121

122-
$this->assertTrue($formRequest->authorize());
122+
$this->assertTrue($formRequest->isAuthorized());
123123
}
124124

125125
public function testValidatedReturnsEmptyArrayBeforeResolution(): void
@@ -338,7 +338,7 @@ public function rules(): array
338338
return [];
339339
}
340340

341-
public function authorize(): bool
341+
public function isAuthorized(): bool
342342
{
343343
return false;
344344
}
@@ -364,7 +364,7 @@ public function rules(): array
364364
return ['title' => 'required'];
365365
}
366366

367-
public function authorize(): bool
367+
public function isAuthorized(): bool
368368
{
369369
self::$order[] = 'authorize';
370370

@@ -381,7 +381,7 @@ protected function prepareForValidation(array $data): array
381381

382382
$formRequest->resolveRequest();
383383

384-
// authorize() must fire before prepareForValidation(); validation never runs.
384+
// isAuthorized() must fire before prepareForValidation(); validation never runs.
385385
$this->assertSame(['authorize'], $formRequest::$order);
386386
}
387387

@@ -443,7 +443,7 @@ public function rules(): array
443443
return [];
444444
}
445445

446-
public function authorize(): bool
446+
public function isAuthorized(): bool
447447
{
448448
return false;
449449
}

user_guide_src/source/incoming/form_requests.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,14 @@ identical to the ``$errors`` argument of :ref:`saving-validation-rules-to-config
131131
Authorization
132132
*************
133133

134-
Override ``authorize()`` to control whether the current user is allowed to make
134+
Override ``isAuthorized()`` to control whether the current user is allowed to make
135135
this request. Return ``false`` to reject the request with a 403 Forbidden
136136
response before validation even runs.
137137

138138
.. literalinclude:: form_requests/005.php
139139
:lines: 2-
140140

141-
.. note:: The ``authorize()`` check runs before ``prepareForValidation()`` and
141+
.. note:: The ``isAuthorized()`` check runs before ``prepareForValidation()`` and
142142
before validation itself. An unauthorized request never reaches the
143143
validation stage.
144144

@@ -223,7 +223,7 @@ whose type extends ``FormRequest``:
223223

224224
#. A new instance is created with the current request injected via the
225225
constructor.
226-
#. ``authorize()`` is called. If it returns ``false``, ``failedAuthorization()``
226+
#. ``isAuthorized()`` is called. If it returns ``false``, ``failedAuthorization()``
227227
is called, and its response is returned to the client.
228228
#. ``validationData()`` collects the data to validate.
229229
#. ``prepareForValidation()`` receives that data and may modify it before the

user_guide_src/source/incoming/form_requests/005.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function rules(): array
1414
];
1515
}
1616

17-
public function authorize(): bool
17+
public function isAuthorized(): bool
1818
{
1919
// Only authenticated users may submit posts.
2020
return auth()->loggedIn();

0 commit comments

Comments
 (0)