Conditions | 1 |
Paths | 1 |
Total Lines | 74 |
Code Lines | 34 |
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) { |
||
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) { |
||
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 array{id: int} $parameters parameters |
||
117 | */ |
||
118 | function (string $route, array $parameters) { |
||
119 | $parameters['_first'] = $parameters['id']; |
||
120 | $parameters['id'] *= 2; |
||
121 | |||
122 | return [ |
||
123 | $route, |
||
124 | $parameters |
||
125 | ]; |
||
126 | }); |
||
127 | |||
128 | // test body |
||
129 | /** @var array<string, mixed> $result */ |
||
130 | $result = $router->callRoute('/route/1'); |
||
131 | |||
132 | // assertions |
||
133 | $this->assertEquals(22, $result['id']); |
||
134 | $this->assertEquals(1, $result['_first']); |
||
135 | $this->assertEquals(2, $result['_second']); |
||
136 | $this->assertEquals(11, $result['_third']); |
||
137 | $this->assertTrue(empty($result['_dont_set_this'])); |
||
138 | } |
||
140 |