| Conditions | 23 |
| Paths | 270 |
| Total Lines | 181 |
| Code Lines | 112 |
| 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 |
||
| 308 | public function testGood( |
||
| 309 | string $implementation, |
||
| 310 | array $params, |
||
| 311 | bool $readable = false, |
||
| 312 | bool $writeable = false |
||
| 313 | ) : void { |
||
| 314 | /** |
||
| 315 | * @var DaftObject $obj |
||
| 316 | */ |
||
| 317 | $obj = new $implementation($params, $writeable); |
||
| 318 | |||
| 319 | if (true === $readable) { |
||
| 320 | $this->assertSame( |
||
| 321 | ($writeable ? count($params) : 0), |
||
| 322 | count($obj->ChangedProperties()) |
||
| 323 | ); |
||
| 324 | |||
| 325 | foreach ($params as $k => $v) { |
||
| 326 | $getterMethod = 'Get' . ucfirst($k); |
||
| 327 | |||
| 328 | $this->assertSame( |
||
| 329 | $params[$k], |
||
| 330 | $obj->$getterMethod(), |
||
| 331 | ( |
||
| 332 | $implementation . |
||
| 333 | '::' . |
||
| 334 | $getterMethod . |
||
| 335 | '() does not match supplied $params' |
||
| 336 | ) |
||
| 337 | ); |
||
| 338 | $this->assertSame( |
||
| 339 | $params[$k], |
||
| 340 | $obj->$k, |
||
| 341 | ( |
||
| 342 | $implementation . |
||
| 343 | '::$' . |
||
| 344 | $k . |
||
| 345 | ' does not match supplied $params' |
||
| 346 | ) |
||
| 347 | ); |
||
| 348 | |||
| 349 | $this->assertSame( |
||
| 350 | (is_null($params[$k]) ? false : true), |
||
| 351 | isset($obj->$k), |
||
| 352 | ( |
||
| 353 | $implementation . |
||
| 354 | '::$' . |
||
| 355 | $k . |
||
| 356 | ' was not found as ' . |
||
| 357 | (is_null($params[$k]) ? 'not set' : 'set') |
||
| 358 | ) |
||
| 359 | ); |
||
| 360 | } |
||
| 361 | } |
||
| 362 | |||
| 363 | foreach (array_keys($params) as $property) { |
||
| 364 | $this->assertSame( |
||
| 365 | $writeable, |
||
| 366 | $obj->HasPropertyChanged($property), |
||
| 367 | ( |
||
| 368 | $implementation . |
||
| 369 | '::$' . |
||
| 370 | $property . |
||
| 371 | ' was' . |
||
| 372 | ($writeable ? ' ' : ' not ') . |
||
| 373 | 'writeable, property should' . |
||
| 374 | ($writeable ? ' ' : ' not ') . |
||
| 375 | 'be changed' |
||
| 376 | ) |
||
| 377 | ); |
||
| 378 | |||
| 379 | if ($writeable) { |
||
| 380 | $obj->MakePropertiesUnchanged($property); |
||
| 381 | $this->assertFalse($obj->HasPropertyChanged($property)); |
||
| 382 | |||
| 383 | if ( |
||
| 384 | in_array( |
||
| 385 | $property, |
||
| 386 | $implementation::DaftObjectNullableProperties(), |
||
| 387 | true |
||
| 388 | ) |
||
| 389 | ) { |
||
| 390 | if ($obj instanceof DaftObjectWorm) { |
||
| 391 | $this->expectException( |
||
| 392 | PropertyNotRewriteableException::class |
||
| 393 | ); |
||
| 394 | $this->expectExceptionMessage( |
||
| 395 | sprintf( |
||
| 396 | 'Property not rewriteable: %s::$%s', |
||
| 397 | $implementation, |
||
| 398 | $property |
||
| 399 | ) |
||
| 400 | ); |
||
| 401 | } |
||
| 402 | unset($obj->$property); |
||
| 403 | $obj->$property = null; |
||
| 404 | } |
||
| 405 | } |
||
| 406 | } |
||
| 407 | |||
| 408 | $obj->MakePropertiesUnchanged(...array_keys($params)); |
||
| 409 | |||
| 410 | ob_start(); |
||
| 411 | var_dump($obj); |
||
|
|
|||
| 412 | |||
| 413 | $debugInfo = ob_get_clean(); |
||
| 414 | |||
| 415 | $props = []; |
||
| 416 | |||
| 417 | foreach ($obj::DaftObjectExportableProperties() as $prop) { |
||
| 418 | $expectedMethod = 'Get' . ucfirst($prop); |
||
| 419 | if ( |
||
| 420 | $obj->__isset($prop) && |
||
| 421 | method_exists($obj, $expectedMethod) && |
||
| 422 | ( |
||
| 423 | new ReflectionMethod($obj, $expectedMethod) |
||
| 424 | )->isPublic() |
||
| 425 | ) { |
||
| 426 | $props[$prop] = $obj->$expectedMethod(); |
||
| 427 | } |
||
| 428 | } |
||
| 429 | |||
| 430 | $regex = |
||
| 431 | '/(?:class |object\()' . |
||
| 432 | preg_quote(get_class($obj), '/') . |
||
| 433 | '[\)]{0,1}#' . |
||
| 434 | '\d+ \(' . |
||
| 435 | preg_quote((string) count($props), '/') . |
||
| 436 | '\) \{.+'; |
||
| 437 | |||
| 438 | foreach ($props as $prop => $val) { |
||
| 439 | $regex .= |
||
| 440 | ' (?:public ' . |
||
| 441 | preg_quote('$' . $prop, '/') . |
||
| 442 | '|' . |
||
| 443 | preg_quote('["' . $prop . '"]', '/') . |
||
| 444 | ')[ ]{0,1}' . |
||
| 445 | preg_quote('=', '/') . |
||
| 446 | '>.+' . |
||
| 447 | ( |
||
| 448 | is_int($val) |
||
| 449 | ? 'int' |
||
| 450 | : ( |
||
| 451 | is_bool($val) |
||
| 452 | ? 'bool' |
||
| 453 | : ( |
||
| 454 | is_float($val) |
||
| 455 | ? '(?:float|double)' |
||
| 456 | : preg_quote(gettype($val), '/') |
||
| 457 | ) |
||
| 458 | ) |
||
| 459 | ) . |
||
| 460 | preg_quote( |
||
| 461 | ( |
||
| 462 | '(' . |
||
| 463 | ( |
||
| 464 | is_string($val) |
||
| 465 | ? mb_strlen($val, '8bit') |
||
| 466 | : ( |
||
| 467 | is_numeric($val) |
||
| 468 | ? (string) $val |
||
| 469 | : var_export($val, true) |
||
| 470 | ) |
||
| 471 | ) . |
||
| 472 | ')' . |
||
| 473 | ( |
||
| 474 | is_string($val) |
||
| 475 | ? (' "' . $val . '"') |
||
| 476 | : '' |
||
| 477 | ) |
||
| 478 | ), |
||
| 479 | '/' |
||
| 480 | ) . |
||
| 481 | '.+'; |
||
| 482 | } |
||
| 483 | |||
| 484 | $regex .= '\}.+$/s'; |
||
| 485 | |||
| 486 | $this->assertRegExp( |
||
| 487 | $regex, |
||
| 488 | str_replace("\n", ' ', (string) $debugInfo) |
||
| 489 | ); |
||
| 607 |