Complex classes like UserProfile often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UserProfile, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class UserProfile implements InputFilterAwareInterface |
||
| 26 | { |
||
| 27 | public $user_id; |
||
| 28 | |||
| 29 | public $street; |
||
| 30 | public $city; |
||
| 31 | public $phone; |
||
| 32 | public $cell; |
||
| 33 | |||
| 34 | public $twitter; |
||
| 35 | public $facebook; |
||
| 36 | public $skype; |
||
| 37 | public $icq; |
||
| 38 | |||
| 39 | protected $inputFilter; |
||
| 40 | protected $userService; |
||
| 41 | protected $serviceManager; |
||
| 42 | protected $serviceLocator; |
||
| 43 | |||
| 44 | public function load( $id ) |
||
| 65 | |||
| 66 | public function save() |
||
| 79 | |||
| 80 | public function exchangeArray($data) |
||
| 96 | |||
| 97 | public function getArrayCopy() |
||
| 101 | |||
| 102 | public function setInputFilter(InputFilterInterface $inputFilter) |
||
| 106 | |||
| 107 | public function getInputFilter() |
||
| 340 | |||
| 341 | |||
| 342 | |||
| 343 | |||
| 344 | /** |
||
| 345 | * Getters/setters for DI stuff |
||
| 346 | */ |
||
| 347 | |||
| 348 | public function getUserService() |
||
| 355 | |||
| 356 | public function setUserService(UserService $userService) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Retrieve service manager instance |
||
| 364 | * |
||
| 365 | * @return ServiceManager |
||
| 366 | */ |
||
| 367 | public function getServiceManager() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Set service manager instance |
||
| 374 | * |
||
| 375 | * @param ServiceManager $serviceManager |
||
| 376 | * @return User |
||
| 377 | */ |
||
| 378 | public function setServiceManager(ServiceManager $serviceManager) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Set serviceManager instance |
||
| 386 | * |
||
| 387 | * @param ServiceLocatorInterface $serviceLocator |
||
| 388 | * @return void |
||
| 389 | */ |
||
| 390 | public function setServiceLocator(ServiceLocatorInterface $serviceLocator) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Retrieve serviceManager instance |
||
| 397 | * |
||
| 398 | * @return ServiceLocatorInterface |
||
| 399 | */ |
||
| 400 | public function getServiceLocator() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @return the $user_id |
||
| 407 | */ |
||
| 408 | public function getId() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param Ambigous <unknown, NULL> $user_id |
||
| 415 | */ |
||
| 416 | public function setId($user_id) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @return the $street |
||
| 423 | */ |
||
| 424 | public function getStreet() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @param Ambigous <string, unknown> $street |
||
| 431 | */ |
||
| 432 | public function setStreet($street) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @return the $city |
||
| 439 | */ |
||
| 440 | public function getCity() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param Ambigous <string, unknown> $city |
||
| 447 | */ |
||
| 448 | public function setCity($city) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @return the $phone |
||
| 455 | */ |
||
| 456 | public function getPhone() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @param Ambigous <string, unknown> $phone |
||
| 463 | */ |
||
| 464 | public function setPhone($phone) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @return the $cell |
||
| 471 | */ |
||
| 472 | public function getCell() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param Ambigous <string, unknown> $cell |
||
| 479 | */ |
||
| 480 | public function setCell($cell) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @return the $twitter |
||
| 487 | */ |
||
| 488 | public function getTwitter() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @param Ambigous <string, unknown> $twitter |
||
| 495 | */ |
||
| 496 | public function setTwitter($twitter) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @return the $facebook |
||
| 503 | */ |
||
| 504 | public function getFacebook() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @param Ambigous <string, unknown> $facebook |
||
| 511 | */ |
||
| 512 | public function setFacebook($facebook) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @return the $skype |
||
| 519 | */ |
||
| 520 | public function getSkype() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @param Ambigous <string, unknown> $skype |
||
| 527 | */ |
||
| 528 | public function setSkype($skype) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @return the $icq |
||
| 535 | */ |
||
| 536 | public function getIcq() |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @param Ambigous <string, unknown> $icq |
||
| 543 | */ |
||
| 544 | public function setIcq($icq) |
||
| 548 | |||
| 549 | |||
| 550 | |||
| 551 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.