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