| Conditions | 1 |
| Paths | 1 |
| Total Lines | 64 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 63 | public function testMultipleMiddlewaresInOrderAndOmitMalformed(): void |
||
| 64 | { |
||
| 65 | // setup |
||
| 66 | $route = '/route/[i:id]'; |
||
| 67 | $router = new SimpleRouter(); |
||
| 68 | |||
| 69 | $router->addRoute($route, function (string $route, array $parameters): array { |
||
| 70 | return $parameters; |
||
| 71 | }); |
||
| 72 | |||
| 73 | $router->registerMiddleware( |
||
| 74 | $route, |
||
| 75 | function (string $route, array $parameters) { |
||
| 76 | $parameters['_second'] = $parameters['id']; |
||
| 77 | $parameters['id'] += 9; |
||
| 78 | |||
| 79 | return [ |
||
| 80 | $route, |
||
| 81 | $parameters |
||
| 82 | ]; |
||
| 83 | }); |
||
| 84 | |||
| 85 | // This middleware is broken, don't parse the result |
||
| 86 | $router->registerMiddleware( |
||
| 87 | $route, |
||
| 88 | function (string $route, array $parameters) { |
||
| 89 | $parameters['_dont_set_this'] = true; |
||
| 90 | |||
| 91 | return null; |
||
| 92 | }); |
||
| 93 | |||
| 94 | $router->registerMiddleware( |
||
| 95 | $route, |
||
| 96 | function (string $route, array $parameters) { |
||
| 97 | $parameters['_third'] = $parameters['id']; |
||
| 98 | $parameters['id'] *= 2; |
||
| 99 | |||
| 100 | return [ |
||
| 101 | $route, |
||
| 102 | $parameters |
||
| 103 | ]; |
||
| 104 | }); |
||
| 105 | |||
| 106 | $router->registerMiddleware( |
||
| 107 | '*', |
||
| 108 | function (string $route, array $parameters) { |
||
| 109 | $parameters['_first'] = $parameters['id']; |
||
| 110 | $parameters['id'] *= 2; |
||
| 111 | |||
| 112 | return [ |
||
| 113 | $route, |
||
| 114 | $parameters |
||
| 115 | ]; |
||
| 116 | }); |
||
| 117 | |||
| 118 | // test body |
||
| 119 | $result = $router->callRoute('/route/1'); |
||
| 120 | |||
| 121 | // assertions |
||
| 122 | $this->assertEquals(22, $result['id']); |
||
| 123 | $this->assertEquals(1, $result['_first']); |
||
| 124 | $this->assertEquals(2, $result['_second']); |
||
| 125 | $this->assertEquals(11, $result['_third']); |
||
| 126 | $this->assertTrue(empty($result['_dont_set_this'])); |
||
| 127 | } |
||
| 129 |