Conditions | 8 |
Paths | 1 |
Total Lines | 70 |
Code Lines | 60 |
Lines | 9 |
Ratio | 12.86 % |
Changes | 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 |
||
145 | public function testAsyncBatchRequestWithMultipleChildren() |
||
146 | { |
||
147 | $idA = 123; |
||
148 | $idC = 'abc'; |
||
149 | $idD = 'def'; |
||
150 | $methodA = 'concat'; |
||
151 | $methodB = 'nofify'; |
||
152 | $methodC = 'sum'; |
||
153 | $methodD = 'bar'; |
||
154 | $paramsA = ['foo'=>'abc', 'bar'=>'def']; |
||
155 | $paramsB = ['foo'=>false]; |
||
156 | $paramsC = ['foo'=>123, 'bar'=>456]; |
||
157 | $paramsD = ['foo'=>123, 'bar'=>456]; |
||
158 | $requestA = $this->client->request($idA, $methodA, $paramsA); |
||
159 | $requestB = $this->client->notification($methodB, $paramsB); |
||
160 | $requestC = $this->client->request($idC, $methodC, $paramsC); |
||
161 | $requestD = $this->client->request($idD, $methodD, $paramsD); |
||
162 | $this->promise = $this->client->sendAllAsync([$requestA, $requestB, $requestC, $requestD]); |
||
163 | |||
164 | $this->promise->then(function ($responses) use ($requestA, $requestB, $requestC, $requestD, $idA, $idC, $idD, $methodA, $methodB, $methodC, $methodD, $paramsA, $paramsB, $paramsC, $paramsD) { |
||
165 | $this->assertEquals(ClientInterface::SPEC, $requestA->getRpcVersion()); |
||
166 | $this->assertEquals($idA, $requestA->getRpcId()); |
||
167 | $this->assertEquals($methodA, $requestA->getRpcMethod()); |
||
168 | $this->assertEquals($paramsA, $requestA->getRpcParams()); |
||
169 | $this->assertEquals(ClientInterface::SPEC, $requestB->getRpcVersion()); |
||
170 | $this->assertEquals(null, $requestB->getRpcId()); |
||
171 | $this->assertEquals($methodB, $requestB->getRpcMethod()); |
||
172 | $this->assertEquals($paramsB, $requestB->getRpcParams()); |
||
173 | $this->assertEquals(ClientInterface::SPEC, $requestC->getRpcVersion()); |
||
174 | $this->assertEquals($idC, $requestC->getRpcId()); |
||
175 | $this->assertEquals($methodC, $requestC->getRpcMethod()); |
||
176 | $this->assertEquals($paramsC, $requestC->getRpcParams()); |
||
177 | $this->assertEquals(ClientInterface::SPEC, $requestD->getRpcVersion()); |
||
178 | $this->assertEquals($idD, $requestD->getRpcId()); |
||
179 | $this->assertEquals($methodD, $requestD->getRpcMethod()); |
||
180 | $this->assertEquals($paramsD, $requestD->getRpcParams()); |
||
181 | |||
182 | $this->assertTrue(is_array($responses)); |
||
183 | $this->assertEquals(3, count($responses)); |
||
184 | |||
185 | View Code Duplication | foreach ($responses as $response) { |
|
186 | if ($response->getRpcId() === $idA) { |
||
187 | $responseA = $response; |
||
188 | } elseif ($response->getRpcId() === $idC) { |
||
189 | $responseC = $response; |
||
190 | } elseif ($response->getRpcId() === $idD) { |
||
191 | $responseD = $response; |
||
192 | } |
||
193 | } |
||
194 | if (!isset($responseA) || !isset($responseC) || !isset($responseD)) { |
||
195 | $this->fail('Invalid responses'); |
||
196 | } |
||
197 | |||
198 | $this->assertEquals(ClientInterface::SPEC, $responseA->getRpcVersion()); |
||
199 | $this->assertEquals(implode('', $paramsA), $responseA->getRpcResult()); |
||
200 | $this->assertEquals($idA, $responseA->getRpcId()); |
||
201 | $this->assertEquals(null, $responseA->getRpcErrorCode()); |
||
202 | $this->assertEquals(null, $responseA->getRpcErrorMessage()); |
||
203 | $this->assertEquals(ClientInterface::SPEC, $responseC->getRpcVersion()); |
||
204 | $this->assertEquals(array_sum($paramsC), $responseC->getRpcResult()); |
||
205 | $this->assertEquals($idC, $responseC->getRpcId()); |
||
206 | $this->assertEquals(null, $responseC->getRpcErrorCode()); |
||
207 | $this->assertEquals(null, $responseC->getRpcErrorMessage()); |
||
208 | $this->assertEquals(ClientInterface::SPEC, $responseD->getRpcVersion()); |
||
209 | $this->assertEquals(null, $responseD->getRpcResult()); |
||
210 | $this->assertEquals($idD, $responseD->getRpcId()); |
||
211 | $this->assertTrue(is_int($responseD->getRpcErrorCode())); |
||
212 | $this->assertTrue(is_string($responseD->getRpcErrorMessage())); |
||
213 | }); |
||
214 | } |
||
215 | } |
||
216 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: