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