Conditions | 1 |
Total Lines | 102 |
Lines | 0 |
Ratio | 0 % |
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 |
||
256 | public function dataValidationFailed(): array |
||
257 | { |
||
258 | return [ |
||
259 | 'callable' => [ |
||
260 | 20, |
||
261 | [ |
||
262 | new Composite([ |
||
263 | static fn () => (new Result())->addError('Bad value.'), |
||
264 | static fn () => (new Result())->addError('Very bad value.'), |
||
265 | ]), |
||
266 | ], |
||
267 | [ |
||
268 | '' => [ |
||
269 | 'Bad value.', |
||
270 | 'Very bad value.', |
||
271 | ], |
||
272 | ], |
||
273 | ], |
||
274 | 'when true' => [ |
||
275 | 20, |
||
276 | [ |
||
277 | new Composite( |
||
278 | [new Number(max: 13), new Number(min: 21)], |
||
279 | when: fn () => true, |
||
280 | ), |
||
281 | ], |
||
282 | [ |
||
283 | '' => [ |
||
284 | 'Value must be no greater than 13.', |
||
285 | 'Value must be no less than 21.', |
||
286 | ], |
||
287 | ], |
||
288 | ], |
||
289 | 'skip on error with previous error' => [ |
||
290 | 20, |
||
291 | [ |
||
292 | new Equal(19), |
||
293 | new Composite( |
||
294 | [new Number(max: 13)], |
||
295 | skipOnError: true, |
||
296 | ), |
||
297 | ], |
||
298 | [ |
||
299 | '' => ['Value must be equal to "19".'], |
||
300 | ], |
||
301 | ], |
||
302 | 'skip on error without previous error' => [ |
||
303 | 20, |
||
304 | [ |
||
305 | new Composite( |
||
306 | [new Number(max: 13)], |
||
307 | skipOnError: true, |
||
308 | ), |
||
309 | ], |
||
310 | [ |
||
311 | '' => ['Value must be no greater than 13.'], |
||
312 | ], |
||
313 | ], |
||
314 | 'custom error' => [ |
||
315 | 20, |
||
316 | [ |
||
317 | new Composite( |
||
318 | [new Number(max: 13, greaterThanMaxMessage: 'Custom error')], |
||
319 | when: fn () => true, |
||
320 | ), |
||
321 | ], |
||
322 | ['' => ['Custom error']], |
||
323 | ], |
||
324 | 'override constructor' => [ |
||
325 | null, |
||
326 | [ |
||
327 | new class () extends Composite { |
||
328 | public function __construct() |
||
329 | { |
||
330 | $this->rules = [new Required()]; |
||
331 | } |
||
332 | }, |
||
333 | ], |
||
334 | ['' => ['Value cannot be blank.']], |
||
335 | ], |
||
336 | 'multiple attributes' => [ |
||
337 | ['latitude' => -91, 'longitude' => 181], |
||
338 | [new CoordinatesRuleSet()], |
||
339 | [ |
||
340 | 'latitude' => ['Value must be no less than -90.'], |
||
341 | 'longitude' => ['Value must be no greater than 180.'], |
||
342 | ], |
||
343 | ], |
||
344 | 'attribute with multiple rules' => [ |
||
345 | [], |
||
346 | new class () extends Composite { |
||
347 | public function getRules(): array |
||
348 | { |
||
349 | return [ |
||
350 | 'name' => [new Required(), new Length(min: 1)], |
||
351 | ]; |
||
352 | } |
||
353 | }, |
||
354 | [ |
||
355 | 'name' => [ |
||
356 | 'Value not passed.', |
||
357 | 'The value must be a string.', |
||
358 | ], |
||
380 |