Skip to content

Commit dcf6c9c

Browse files
committed
minor changes
1 parent 33cd894 commit dcf6c9c

File tree

2 files changed

+17
-30
lines changed

2 files changed

+17
-30
lines changed

src/Annotation/Route.php

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,6 @@ final class Route implements RouteDescriptorInterface
115115
*/
116116
public function __construct(array $params)
117117
{
118-
$params += [
119-
'name' => '',
120-
'host' => null,
121-
'path' => '',
122-
'methods' => [],
123-
'middlewares' => [],
124-
'attributes' => [],
125-
'summary' => '',
126-
'description' => '',
127-
'tags' => [],
128-
'priority' => 0,
129-
];
130-
131118
$this->name = $this->extractNameFromParams($params);
132119
$this->host = $this->extractHostFromParams($params);
133120
$this->path = $this->extractPathFromParams($params);
@@ -229,7 +216,7 @@ public function getPriority() : int
229216
*/
230217
private function extractNameFromParams(array $params) : string
231218
{
232-
$name = $params['name'];
219+
$name = $params['name'] ?? '';
233220

234221
if ('' === $name || !is_string($name)) {
235222
throw new InvalidDescriptorArgumentException('@Route.name must contain a non-empty string.');
@@ -247,7 +234,7 @@ private function extractNameFromParams(array $params) : string
247234
*/
248235
private function extractHostFromParams(array $params) : ?string
249236
{
250-
$host = $params['host'];
237+
$host = $params['host'] ?? null;
251238

252239
if (isset($host) && ('' === $host || !is_string($host))) {
253240
throw new InvalidDescriptorArgumentException('@Route.host must contain a non-empty string.');
@@ -265,7 +252,7 @@ private function extractHostFromParams(array $params) : ?string
265252
*/
266253
private function extractPathFromParams(array $params) : string
267254
{
268-
$path = $params['path'];
255+
$path = $params['path'] ?? '';
269256

270257
if ('' === $path || !is_string($path)) {
271258
throw new InvalidDescriptorArgumentException('@Route.path must contain a non-empty string.');
@@ -283,15 +270,15 @@ private function extractPathFromParams(array $params) : string
283270
*/
284271
private function extractMethodsFromParams(array $params) : array
285272
{
286-
$methods = $params['methods'];
273+
$methods = $params['methods'] ?? [];
287274

288275
if ([] === $methods || !is_array($methods)) {
289276
throw new InvalidDescriptorArgumentException('@Route.methods must contain a non-empty array.');
290277
}
291278

292279
foreach ($methods as $value) {
293-
if ('' === $value || !is_string($value)) {
294-
throw new InvalidDescriptorArgumentException('@Route.methods must contain non-empty strings.');
280+
if (!is_string($value)) {
281+
throw new InvalidDescriptorArgumentException('@Route.methods must contain strings.');
295282
}
296283
}
297284

@@ -307,14 +294,14 @@ private function extractMethodsFromParams(array $params) : array
307294
*/
308295
private function extractMiddlewaresFromParams(array $params) : array
309296
{
310-
$middlewares = $params['middlewares'];
297+
$middlewares = $params['middlewares'] ?? [];
311298

312299
if (!is_array($middlewares)) {
313300
throw new InvalidDescriptorArgumentException('@Route.middlewares must contain an array.');
314301
}
315302

316303
foreach ($middlewares as $value) {
317-
if ('' === $value || !is_string($value) || !is_subclass_of($value, MiddlewareInterface::class)) {
304+
if (!is_string($value) || !is_subclass_of($value, MiddlewareInterface::class)) {
318305
throw new InvalidDescriptorArgumentException(
319306
'@Route.middlewares must contain the class names of existing middlewares.'
320307
);
@@ -333,7 +320,7 @@ private function extractMiddlewaresFromParams(array $params) : array
333320
*/
334321
private function extractAttributesFromParams(array $params) : array
335322
{
336-
$attributes = $params['attributes'];
323+
$attributes = $params['attributes'] ?? [];
337324

338325
if (!is_array($attributes)) {
339326
throw new InvalidDescriptorArgumentException('@Route.attributes must contain an array.');
@@ -351,7 +338,7 @@ private function extractAttributesFromParams(array $params) : array
351338
*/
352339
private function extractSummaryFromParams(array $params) : string
353340
{
354-
$summary = $params['summary'];
341+
$summary = $params['summary'] ?? '';
355342

356343
if (!is_string($summary)) {
357344
throw new InvalidDescriptorArgumentException('@Route.summary must contain a string.');
@@ -369,7 +356,7 @@ private function extractSummaryFromParams(array $params) : string
369356
*/
370357
private function extractDescriptionFromParams(array $params) : string
371358
{
372-
$description = $params['description'];
359+
$description = $params['description'] ?? '';
373360

374361
if (!is_string($description)) {
375362
throw new InvalidDescriptorArgumentException('@Route.description must contain a string.');
@@ -387,15 +374,15 @@ private function extractDescriptionFromParams(array $params) : string
387374
*/
388375
private function extractTagsFromParams(array $params) : array
389376
{
390-
$tags = $params['tags'];
377+
$tags = $params['tags'] ?? [];
391378

392379
if (!is_array($tags)) {
393380
throw new InvalidDescriptorArgumentException('@Route.tags must contain an array.');
394381
}
395382

396383
foreach ($tags as $value) {
397-
if ('' === $value || !is_string($value)) {
398-
throw new InvalidDescriptorArgumentException('@Route.tags must contain non-empty strings.');
384+
if (!is_string($value)) {
385+
throw new InvalidDescriptorArgumentException('@Route.tags must contain strings.');
399386
}
400387
}
401388

@@ -411,7 +398,7 @@ private function extractTagsFromParams(array $params) : array
411398
*/
412399
private function extractPriorityFromParams(array $params) : int
413400
{
414-
$priority = $params['priority'];
401+
$priority = $params['priority'] ?? 0;
415402

416403
if (!is_int($priority)) {
417404
throw new InvalidDescriptorArgumentException('@Route.priority must contain an integer.');

tests/Annotation/RouteTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ public function testConstructorParamsContainInvalidPriority($invalidPriority) :
389389
public function testConstructorMethodsParamContainsInvalidValue($invalidMethod) : void
390390
{
391391
$this->expectException(InvalidDescriptorArgumentException::class);
392-
$this->expectExceptionMessage('@Route.methods must contain non-empty strings.');
392+
$this->expectExceptionMessage('@Route.methods must contain strings.');
393393

394394
new Route([
395395
'name' => 'foo',
@@ -456,7 +456,7 @@ public function testConstructorMiddlewaresParamContainsNonMiddlewareClass() : vo
456456
public function testConstructorTagsParamContainsInvalidValue($invalidTag) : void
457457
{
458458
$this->expectException(InvalidDescriptorArgumentException::class);
459-
$this->expectExceptionMessage('@Route.tags must contain non-empty strings.');
459+
$this->expectExceptionMessage('@Route.tags must contain strings.');
460460

461461
new Route([
462462
'name' => 'foo',

0 commit comments

Comments
 (0)