Skip to content

Commit 2855aa2

Browse files
committed
chore: Use <> to specify special characters
1 parent 94f660d commit 2855aa2

File tree

3 files changed

+11
-21
lines changed

3 files changed

+11
-21
lines changed

src/Endpoint.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ class Endpoint implements EndpointInterface
7474

7575
/**
7676
* The registered REST route with namespace and route
77-
*
78-
* @var string|null
7977
*/
8078
protected ?string $fullRestRoute = null;
8179

@@ -299,9 +297,6 @@ public function middleware(Middleware $middleware): Endpoint
299297

300298
/**
301299
* Specifies a set of plugins that are needed by the endpoint
302-
*
303-
* @param string|array $plugins
304-
* @return Endpoint
305300
*/
306301
public function depends(string|array $plugins): Endpoint
307302
{
@@ -310,13 +305,12 @@ public function depends(string|array $plugins): Endpoint
310305
}
311306

312307
$this->plugins += $plugins;
308+
313309
return $this;
314310
}
315311

316312
/**
317313
* Retrieves the registered REST route: namespace + route
318-
*
319-
* @return string|null
320314
*/
321315
public function getFullRestRoute(): ?string
322316
{
@@ -325,8 +319,6 @@ public function getFullRestRoute(): ?string
325319

326320
/**
327321
* Retrieves the HTTP method of the endpoint
328-
*
329-
* @return string
330322
*/
331323
public function getHttpMethod(): string
332324
{
@@ -335,8 +327,6 @@ public function getHttpMethod(): string
335327

336328
/**
337329
* Retrieves the required endpoint plugins
338-
*
339-
* @return array
340330
*/
341331
public function getRequiredPlugins(): array
342332
{
@@ -453,7 +443,7 @@ protected function replaceSpecialValue(WP_REST_Request $request, string $value):
453443
// Checks if value matches a special value.
454444
// If so, replaces with request variable.
455445
$newValue = \trim($value);
456-
if (! \str_starts_with($newValue, '{') && ! \str_ends_with($newValue, '}')) {
446+
if (! \str_starts_with($newValue, '<') || ! \str_ends_with($newValue, '>')) {
457447
return $value;
458448
}
459449

tests/Integration/Routers/PostsRouter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
})
3333
->schema('Posts/Update')
3434
->returns('Posts/Get')
35-
->hasCap('edit_post', '{ID}');
35+
->hasCap('edit_post', '<ID>');
3636

3737
// Deletes a post
3838
$router->delete('(?P<ID>[\d]+)', function (string $ID) {
@@ -43,6 +43,6 @@
4343

4444
return esc_html__('Post deleted with success');
4545
})
46-
->hasCap('delete_post', '{ID}');
46+
->hasCap('delete_post', '<ID>');
4747

4848
return $router;

tests/Unit/EndpointTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@
118118
$mockedRequest = Mockery::mock(\WP_REST_Request::class);
119119
$expectedParams = [];
120120
foreach ($args as $arg) {
121-
if (! is_string($arg) || ! str_starts_with($arg, '{')) {
121+
if (! is_string($arg) || ! str_starts_with($arg, '<')) {
122122
$expectedParams[] = $arg;
123123

124124
continue;
125125
}
126126

127127
$paramName = substr($arg, 1, -1);
128-
$isArgumentMissing = $arg === '{argument-missing}';
128+
$isArgumentMissing = $arg === '<argument-missing>';
129129
$mockedRequest
130130
->shouldReceive('has_param')
131131
->once()
@@ -152,7 +152,7 @@
152152
expect($permissionHandlers[0]($mockedRequest))->toBeTrue();
153153
})->with([
154154
'create_users', ['edit_plugins', 'delete_plugins', 98],
155-
['create_users', '{post_id}', '{another_var}', false],
155+
['create_users', '<post_id>', '{another_var}', false],
156156
['edit_posts', '{argument-missing}'], '{custom-cap}',
157157
])->group('endpoint', 'hasCap');
158158

@@ -166,14 +166,14 @@
166166
$mockedRequest = Mockery::mock(\WP_REST_Request::class);
167167
$expectedParams = [];
168168
foreach ($args as $arg) {
169-
if (! str_starts_with($arg, '{')) {
169+
if (! str_starts_with($arg, '<')) {
170170
$expectedParams[] = $arg;
171171

172172
continue;
173173
}
174174

175175
$paramName = substr($arg, 1, -1);
176-
$isArgumentMissing = $arg === '{argument-missing}';
176+
$isArgumentMissing = $arg === '<argument-missing>';
177177
$mockedRequest
178178
->shouldReceive('has_param')
179179
->once()
@@ -205,8 +205,8 @@
205205
->toHaveProperty('data', ['status' => 403]);
206206
})->with([
207207
'create_users', ['edit_plugins', 'delete_plugins'],
208-
'{custom_capability}', ['create_users', '{post_id}', '{another_var}'],
209-
['create_users', '{post_id}', '{argument-missing}'],
208+
'{custom_capability}', ['create_users', '<post_id>', '{another_var}'],
209+
['create_users', '<post_id>', '{argument-missing}'],
210210
])->group('endpoint', 'hasCap');
211211

212212
test('Missing capability', function () {

0 commit comments

Comments
 (0)