Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Admin 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 Admin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Admin implements AdminInterface |
||
| 28 | { |
||
| 29 | use AdminTrait; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Entities collection. |
||
| 33 | * |
||
| 34 | * @var ArrayCollection |
||
| 35 | */ |
||
| 36 | protected $entities; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var MessageHandlerInterface |
||
| 40 | */ |
||
| 41 | protected $messageHandler; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var EntityManagerInterface |
||
| 45 | */ |
||
| 46 | protected $entityManager; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var DataProviderInterface |
||
| 50 | */ |
||
| 51 | protected $dataProvider; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Admin configuration object |
||
| 55 | * |
||
| 56 | * @var AdminConfiguration |
||
| 57 | */ |
||
| 58 | protected $configuration; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Admin configured actions |
||
| 62 | * |
||
| 63 | * @var ActionInterface[] |
||
| 64 | */ |
||
| 65 | protected $actions = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Admin current action. It will be set after calling the handleRequest() |
||
| 69 | * |
||
| 70 | * @var ActionInterface |
||
| 71 | */ |
||
| 72 | protected $currentAction; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Admin name |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $name; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var EventDispatcherInterface |
||
| 83 | */ |
||
| 84 | protected $eventDispatcher; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Admin constructor. |
||
| 88 | * |
||
| 89 | * @param string $name |
||
| 90 | * @param DataProviderInterface $dataProvider |
||
| 91 | * @param AdminConfiguration $configuration |
||
| 92 | * @param MessageHandlerInterface $messageHandler |
||
| 93 | * @param EventDispatcherInterface $eventDispatcher |
||
| 94 | */ |
||
| 95 | 17 | public function __construct( |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Load entities and set current action according to request |
||
| 112 | * |
||
| 113 | * @param Request $request |
||
| 114 | * @param null $user |
||
| 115 | * @return void |
||
| 116 | * @throws AdminException |
||
| 117 | */ |
||
| 118 | 5 | public function handleRequest(Request $request, $user = null) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Check if user is allowed to be here |
||
| 154 | * |
||
| 155 | * @param UserInterface|string $user |
||
| 156 | * @throws Exception |
||
| 157 | */ |
||
| 158 | 5 | public function checkPermissions($user) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Create and return a new entity. |
||
| 193 | * |
||
| 194 | * @return object |
||
| 195 | */ |
||
| 196 | 3 | public function create() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Save entity via admin manager. Error are catch, logged and a flash message is added to session |
||
| 218 | * |
||
| 219 | * @return bool true if the entity was saved without errors |
||
| 220 | */ |
||
| 221 | 1 | View Code Duplication | public function save() |
| 245 | |||
| 246 | /** |
||
| 247 | * Remove an entity with data provider |
||
| 248 | * |
||
| 249 | * @return bool true if the entity was saved without errors |
||
| 250 | */ |
||
| 251 | 1 | View Code Duplication | public function remove() |
| 275 | |||
| 276 | /** |
||
| 277 | * Generate a route for admin and action name (like lag.admin.my_admin) |
||
| 278 | * |
||
| 279 | * @param $actionName |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | * |
||
| 283 | * @throws Exception |
||
| 284 | */ |
||
| 285 | 8 | public function generateRouteName($actionName) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Load entities manually according to criteria. |
||
| 306 | * |
||
| 307 | * @param array $criteria |
||
| 308 | * @param array $orderBy |
||
| 309 | * @param int $limit |
||
| 310 | * @param int $offset |
||
| 311 | * @throws Exception |
||
| 312 | */ |
||
| 313 | 5 | public function load(array $criteria, $orderBy = [], $limit = 25, $offset = 1) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Return loaded entities |
||
| 355 | * |
||
| 356 | * @return Collection |
||
| 357 | */ |
||
| 358 | 2 | public function getEntities() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Return entity for current admin. If entity does not exist, it throws an exception. |
||
| 365 | * |
||
| 366 | * @return mixed |
||
| 367 | * |
||
| 368 | * @throws Exception |
||
| 369 | */ |
||
| 370 | 1 | public function getUniqueEntity() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Return admin name |
||
| 383 | * |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | 14 | public function getName() |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Return true if current action is granted for user. |
||
| 393 | * |
||
| 394 | * @param string $actionName Le plus grand de tous les héros |
||
| 395 | * @param array $roles |
||
| 396 | * |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | 2 | public function isActionGranted($actionName, array $roles) |
|
| 422 | |||
| 423 | /** |
||
| 424 | * @return ActionInterface[] |
||
| 425 | */ |
||
| 426 | 6 | public function getActions() |
|
| 430 | |||
| 431 | /** |
||
| 432 | * @return integer[] |
||
| 433 | */ |
||
| 434 | 2 | public function getActionNames() |
|
| 438 | |||
| 439 | /** |
||
| 440 | * @param $name |
||
| 441 | * @return ActionInterface |
||
| 442 | * @throws Exception |
||
| 443 | */ |
||
| 444 | 5 | public function getAction($name) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Return if an action with specified name exists form this admin. |
||
| 457 | * |
||
| 458 | * @param $name |
||
| 459 | * @return bool |
||
| 460 | */ |
||
| 461 | 1 | public function hasAction($name) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * @param ActionInterface $action |
||
| 468 | * @return void |
||
| 469 | */ |
||
| 470 | 12 | public function addAction(ActionInterface $action) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Return the current action or an exception if it is not set. |
||
| 477 | * |
||
| 478 | * @return ActionInterface |
||
| 479 | * @throws Exception |
||
| 480 | */ |
||
| 481 | 5 | public function getCurrentAction() |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Return if the current action has been initialized and set. |
||
| 495 | * |
||
| 496 | * @return boolean |
||
| 497 | */ |
||
| 498 | public function isCurrentActionDefined() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Return admin configuration object. |
||
| 505 | * |
||
| 506 | * @return AdminConfiguration |
||
| 507 | */ |
||
| 508 | 14 | public function getConfiguration() |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Return a translation key for a message according to the Admin's translation pattern. |
||
| 515 | * |
||
| 516 | * @param string $message |
||
| 517 | * @return string |
||
| 518 | */ |
||
| 519 | 3 | protected function generateMessageTranslationKey($message) |
|
| 527 | } |
||
| 528 |