| Conditions | 1 |
| Paths | 1 |
| Total Lines | 75 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 64 | public function testMultipleMiddlewaresInOrderAndOmitMalformed(): void |
||
| 65 | { |
||
| 66 | // setup |
||
| 67 | $route = '/route/[i:id]'; |
||
| 68 | $router = $this->getRouter(); |
||
| 69 | |||
| 70 | $router->addRoute($route, function (string $route, array $parameters): array { |
||
| 71 | return $parameters; |
||
| 72 | }); |
||
| 73 | |||
| 74 | $router->registerMiddleware($route, /** |
||
| 75 | * |
||
| 76 | * @psalm-param string $route route |
||
| 77 | * @psalm-param array{id: int} $parameters parameters |
||
| 78 | */ |
||
| 79 | function (string $route, array $parameters): array { |
||
| 80 | $parameters['_second'] = $parameters['id']; |
||
| 81 | $parameters['id'] += 9; |
||
| 82 | |||
| 83 | return [ |
||
| 84 | $route, |
||
| 85 | $parameters |
||
| 86 | ]; |
||
| 87 | }); |
||
| 88 | |||
| 89 | // This middleware is broken, don't parse the result |
||
| 90 | $router->registerMiddleware($route, function (string $route, array $parameters) { |
||
| 91 | $parameters['_dont_set_this'] = true; |
||
| 92 | |||
| 93 | return null; |
||
| 94 | }); |
||
| 95 | |||
| 96 | $router->registerMiddleware($route, /** |
||
| 97 | * |
||
| 98 | * @psalm-param string $route |
||
| 99 | * route |
||
| 100 | * @psalm-param array{id: int} $parameters parameters |
||
| 101 | */ |
||
| 102 | function (string $route, array $parameters): array { |
||
| 103 | $parameters['_third'] = $parameters['id']; |
||
| 104 | $parameters['id'] *= 2; |
||
| 105 | |||
| 106 | return [ |
||
| 107 | $route, |
||
| 108 | $parameters |
||
| 109 | ]; |
||
| 110 | }); |
||
| 111 | |||
| 112 | $router->registerMiddleware('*', /** |
||
| 113 | * |
||
| 114 | * @psalm-param string $route |
||
| 115 | * route |
||
| 116 | * @psalm-param int $id parameter |
||
| 117 | */ |
||
| 118 | function (string $route, int $id): array { |
||
| 119 | $parameters = []; |
||
| 120 | $parameters['_first'] = $id; |
||
| 121 | $parameters['id'] = $id * 2; |
||
| 122 | |||
| 123 | return [ |
||
| 124 | $route, |
||
| 125 | $parameters |
||
| 126 | ]; |
||
| 127 | }); |
||
| 128 | |||
| 129 | // test body |
||
| 130 | /** @var array<string, mixed> $result */ |
||
| 131 | $result = $router->callRoute('/route/1'); |
||
| 132 | |||
| 133 | // assertions |
||
| 134 | $this->assertEquals(22, $result['id']); |
||
| 135 | $this->assertEquals(1, $result['_first']); |
||
| 136 | $this->assertEquals(2, $result['_second']); |
||
| 137 | $this->assertEquals(11, $result['_third']); |
||
| 138 | $this->assertTrue(empty($result['_dont_set_this'])); |
||
| 139 | } |
||
| 171 |