Skip to content

Commit f24f714

Browse files
committed
code improved
1 parent 84ea3fa commit f24f714

File tree

2 files changed

+116
-77
lines changed

2 files changed

+116
-77
lines changed

src/Annotation/Route.php

Lines changed: 87 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@
1818
use Sunrise\Http\Router\Exception\InvalidDescriptorArgumentException;
1919
use Sunrise\Http\Router\RouteDescriptorInterface;
2020

21-
/**
22-
* Import functions
23-
*/
24-
use function is_array;
25-
use function is_int;
26-
use function is_string;
27-
use function is_subclass_of;
28-
2921
/**
3022
* Annotation for a route description
3123
*
@@ -218,9 +210,10 @@ private function extractNameFromParams(array $params) : string
218210
{
219211
$name = $params['name'] ?? '';
220212

221-
if ('' === $name || !is_string($name)) {
222-
throw new InvalidDescriptorArgumentException('@Route.name must contain a non-empty string.');
223-
}
213+
InvalidDescriptorArgumentException::assertIsNotEmptyString(
214+
$name,
215+
'@Route.name must contain a non-empty string.'
216+
);
224217

225218
return $name;
226219
}
@@ -236,10 +229,16 @@ private function extractHostFromParams(array $params) : ?string
236229
{
237230
$host = $params['host'] ?? null;
238231

239-
if (isset($host) && ('' === $host || !is_string($host))) {
240-
throw new InvalidDescriptorArgumentException('@Route.host must contain a non-empty string.');
232+
// isn't required parameter...
233+
if (null === $host) {
234+
return null;
241235
}
242236

237+
InvalidDescriptorArgumentException::assertIsNotEmptyString(
238+
$host,
239+
'@Route.host must contain a non-empty string.'
240+
);
241+
243242
return $host;
244243
}
245244

@@ -254,9 +253,10 @@ private function extractPathFromParams(array $params) : string
254253
{
255254
$path = $params['path'] ?? '';
256255

257-
if ('' === $path || !is_string($path)) {
258-
throw new InvalidDescriptorArgumentException('@Route.path must contain a non-empty string.');
259-
}
256+
InvalidDescriptorArgumentException::assertIsNotEmptyString(
257+
$path,
258+
'@Route.path must contain a non-empty string.'
259+
);
260260

261261
return $path;
262262
}
@@ -272,14 +272,16 @@ private function extractMethodsFromParams(array $params) : array
272272
{
273273
$methods = $params['methods'] ?? [];
274274

275-
if ([] === $methods || !is_array($methods)) {
276-
throw new InvalidDescriptorArgumentException('@Route.methods must contain a non-empty array.');
277-
}
275+
InvalidDescriptorArgumentException::assertIsNotEmptyArray(
276+
$methods,
277+
'@Route.methods must contain a non-empty array.'
278+
);
278279

279280
foreach ($methods as $value) {
280-
if (!is_string($value)) {
281-
throw new InvalidDescriptorArgumentException('@Route.methods must contain strings.');
282-
}
281+
InvalidDescriptorArgumentException::assertIsNotEmptyString(
282+
$value,
283+
'@Route.methods must contain non-empty strings.'
284+
);
283285
}
284286

285287
return $methods;
@@ -294,18 +296,24 @@ private function extractMethodsFromParams(array $params) : array
294296
*/
295297
private function extractMiddlewaresFromParams(array $params) : array
296298
{
297-
$middlewares = $params['middlewares'] ?? [];
299+
$middlewares = $params['middlewares'] ?? null;
298300

299-
if (!is_array($middlewares)) {
300-
throw new InvalidDescriptorArgumentException('@Route.middlewares must contain an array.');
301+
// isn't required parameter...
302+
if (null === $middlewares) {
303+
return [];
301304
}
302305

306+
InvalidDescriptorArgumentException::assertIsArray(
307+
$middlewares,
308+
'@Route.middlewares must contain an array.'
309+
);
310+
303311
foreach ($middlewares as $value) {
304-
if (!is_string($value) || !is_subclass_of($value, MiddlewareInterface::class)) {
305-
throw new InvalidDescriptorArgumentException(
306-
'@Route.middlewares must contain the class names of existing middlewares.'
307-
);
308-
}
312+
InvalidDescriptorArgumentException::assertIsSubclassOf(
313+
$value,
314+
MiddlewareInterface::class,
315+
'@Route.middlewares must contain the class names of existing middlewares.'
316+
);
309317
}
310318

311319
return $middlewares;
@@ -320,12 +328,18 @@ private function extractMiddlewaresFromParams(array $params) : array
320328
*/
321329
private function extractAttributesFromParams(array $params) : array
322330
{
323-
$attributes = $params['attributes'] ?? [];
331+
$attributes = $params['attributes'] ?? null;
324332

325-
if (!is_array($attributes)) {
326-
throw new InvalidDescriptorArgumentException('@Route.attributes must contain an array.');
333+
// isn't required parameter...
334+
if (null === $attributes) {
335+
return [];
327336
}
328337

338+
InvalidDescriptorArgumentException::assertIsArray(
339+
$attributes,
340+
'@Route.attributes must contain an array.'
341+
);
342+
329343
return $attributes;
330344
}
331345

@@ -338,12 +352,18 @@ private function extractAttributesFromParams(array $params) : array
338352
*/
339353
private function extractSummaryFromParams(array $params) : string
340354
{
341-
$summary = $params['summary'] ?? '';
355+
$summary = $params['summary'] ?? null;
342356

343-
if (!is_string($summary)) {
344-
throw new InvalidDescriptorArgumentException('@Route.summary must contain a string.');
357+
// isn't required parameter...
358+
if (null === $summary) {
359+
return '';
345360
}
346361

362+
InvalidDescriptorArgumentException::assertIsString(
363+
$summary,
364+
'@Route.summary must contain a string.'
365+
);
366+
347367
return $summary;
348368
}
349369

@@ -356,12 +376,18 @@ private function extractSummaryFromParams(array $params) : string
356376
*/
357377
private function extractDescriptionFromParams(array $params) : string
358378
{
359-
$description = $params['description'] ?? '';
379+
$description = $params['description'] ?? null;
360380

361-
if (!is_string($description)) {
362-
throw new InvalidDescriptorArgumentException('@Route.description must contain a string.');
381+
// isn't required parameter...
382+
if (null === $description) {
383+
return '';
363384
}
364385

386+
InvalidDescriptorArgumentException::assertIsString(
387+
$description,
388+
'@Route.description must contain a string.'
389+
);
390+
365391
return $description;
366392
}
367393

@@ -374,16 +400,23 @@ private function extractDescriptionFromParams(array $params) : string
374400
*/
375401
private function extractTagsFromParams(array $params) : array
376402
{
377-
$tags = $params['tags'] ?? [];
403+
$tags = $params['tags'] ?? null;
378404

379-
if (!is_array($tags)) {
380-
throw new InvalidDescriptorArgumentException('@Route.tags must contain an array.');
405+
// isn't required parameter...
406+
if (null === $tags) {
407+
return [];
381408
}
382409

410+
InvalidDescriptorArgumentException::assertIsArray(
411+
$tags,
412+
'@Route.tags must contain an array.'
413+
);
414+
383415
foreach ($tags as $value) {
384-
if (!is_string($value)) {
385-
throw new InvalidDescriptorArgumentException('@Route.tags must contain strings.');
386-
}
416+
InvalidDescriptorArgumentException::assertIsNotEmptyString(
417+
$value,
418+
'@Route.tags must contain non-empty strings.'
419+
);
387420
}
388421

389422
return $tags;
@@ -398,12 +431,18 @@ private function extractTagsFromParams(array $params) : array
398431
*/
399432
private function extractPriorityFromParams(array $params) : int
400433
{
401-
$priority = $params['priority'] ?? 0;
434+
$priority = $params['priority'] ?? null;
402435

403-
if (!is_int($priority)) {
404-
throw new InvalidDescriptorArgumentException('@Route.priority must contain an integer.');
436+
// isn't required parameter...
437+
if (null === $priority) {
438+
return 0;
405439
}
406440

441+
InvalidDescriptorArgumentException::assertIsInteger(
442+
$priority,
443+
'@Route.priority must contain an integer.'
444+
);
445+
407446
return $priority;
408447
}
409448
}

src/Attribute/Route.php

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919
use Sunrise\Http\Router\Exception\InvalidDescriptorArgumentException;
2020
use Sunrise\Http\Router\RouteDescriptorInterface;
2121

22-
/**
23-
* Import functions
24-
*/
25-
use function is_string;
26-
use function is_subclass_of;
27-
2822
/**
2923
* Attribute for a route description
3024
*
@@ -240,9 +234,10 @@ public function getPriority() : int
240234
*/
241235
private function assertValidName() : void
242236
{
243-
if ('' === $this->name) {
244-
throw new InvalidDescriptorArgumentException('#[Route.name] must contain a non-empty string.');
245-
}
237+
InvalidDescriptorArgumentException::assertIsNotEmpty(
238+
$this->name,
239+
'#[Route.name] must contain a non-empty string.'
240+
);
246241
}
247242

248243
/**
@@ -254,9 +249,10 @@ private function assertValidName() : void
254249
*/
255250
private function assertValidHost() : void
256251
{
257-
if ('' === $this->host) {
258-
throw new InvalidDescriptorArgumentException('#[Route.host] must contain a non-empty string or null.');
259-
}
252+
InvalidDescriptorArgumentException::assertIsNotEmpty(
253+
$this->host,
254+
'#[Route.host] must contain a non-empty string or null.'
255+
);
260256
}
261257

262258
/**
@@ -268,9 +264,10 @@ private function assertValidHost() : void
268264
*/
269265
private function assertValidPath() : void
270266
{
271-
if ('' === $this->path) {
272-
throw new InvalidDescriptorArgumentException('#[Route.path] must contain a non-empty string.');
273-
}
267+
InvalidDescriptorArgumentException::assertIsNotEmpty(
268+
$this->path,
269+
'#[Route.path] must contain a non-empty string.'
270+
);
274271
}
275272

276273
/**
@@ -282,14 +279,16 @@ private function assertValidPath() : void
282279
*/
283280
private function assertValidMethods() : void
284281
{
285-
if ([] === $this->methods) {
286-
throw new InvalidDescriptorArgumentException('#[Route.methods] must contain at least one element.');
287-
}
282+
InvalidDescriptorArgumentException::assertIsNotEmpty(
283+
$this->methods,
284+
'#[Route.methods] must contain at least one element.'
285+
);
288286

289287
foreach ($this->methods as $value) {
290-
if ('' === $value || !is_string($value)) {
291-
throw new InvalidDescriptorArgumentException('#[Route.methods] must contain non-empty strings.');
292-
}
288+
InvalidDescriptorArgumentException::assertIsNotEmptyString(
289+
$value,
290+
'#[Route.methods] must contain non-empty strings.'
291+
);
293292
}
294293
}
295294

@@ -307,11 +306,11 @@ private function assertValidMiddlewares() : void
307306
}
308307

309308
foreach ($this->middlewares as $value) {
310-
if ('' === $value || !is_string($value) || !is_subclass_of($value, MiddlewareInterface::class)) {
311-
throw new InvalidDescriptorArgumentException(
312-
'#[Route.middlewares] must contain the class names of existing middlewares.'
313-
);
314-
}
309+
InvalidDescriptorArgumentException::assertIsSubclassOf(
310+
$value,
311+
MiddlewareInterface::class,
312+
'#[Route.middlewares] must contain the class names of existing middlewares.'
313+
);
315314
}
316315
}
317316

@@ -329,9 +328,10 @@ private function assertValidTags() : void
329328
}
330329

331330
foreach ($this->tags as $value) {
332-
if ('' === $value || !is_string($value)) {
333-
throw new InvalidDescriptorArgumentException('#[Route.tags] must contain non-empty strings.');
334-
}
331+
InvalidDescriptorArgumentException::assertIsNotEmptyString(
332+
$value,
333+
'#[Route.tags] must contain non-empty strings.'
334+
);
335335
}
336336
}
337337
}

0 commit comments

Comments
 (0)