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 |
||
| 28 | class Admin implements AdminInterface |
||
| 29 | { |
||
| 30 | use AdminTrait; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Entities collection. |
||
| 34 | * |
||
| 35 | * @var ArrayCollection |
||
| 36 | */ |
||
| 37 | protected $entities; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var MessageHandlerInterface |
||
| 41 | */ |
||
| 42 | protected $messageHandler; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var EntityManagerInterface |
||
| 46 | */ |
||
| 47 | protected $entityManager; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var DataProviderInterface |
||
| 51 | */ |
||
| 52 | protected $dataProvider; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Admin configuration object |
||
| 56 | * |
||
| 57 | * @var AdminConfiguration |
||
| 58 | */ |
||
| 59 | protected $configuration; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Admin configured actions |
||
| 63 | * |
||
| 64 | * @var ActionInterface[] |
||
| 65 | */ |
||
| 66 | protected $actions = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Admin current action. It will be set after calling the handleRequest() |
||
| 70 | * |
||
| 71 | * @var ActionInterface |
||
| 72 | */ |
||
| 73 | protected $currentAction; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Admin name |
||
| 77 | * |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $name; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var EventDispatcherInterface |
||
| 84 | */ |
||
| 85 | protected $eventDispatcher; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var RequestFilterInterface |
||
| 89 | */ |
||
| 90 | protected $requestFilter; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Admin constructor. |
||
| 94 | * |
||
| 95 | 26 | * @param string $name |
|
| 96 | * @param DataProviderInterface $dataProvider |
||
| 97 | * @param AdminConfiguration $configuration |
||
| 98 | * @param MessageHandlerInterface $messageHandler |
||
| 99 | * @param EventDispatcherInterface $eventDispatcher |
||
| 100 | * @param RequestFilterInterface $requestFilter |
||
| 101 | */ |
||
| 102 | 26 | public function __construct( |
|
| 118 | 9 | ||
| 119 | /** |
||
| 120 | * Load entities and set current action according to request. |
||
| 121 | 9 | * |
|
| 122 | * @param Request $request |
||
| 123 | 9 | * @param null $user |
|
| 124 | * @return void |
||
| 125 | * @throws AdminException |
||
| 126 | 9 | */ |
|
| 127 | 9 | public function handleRequest(Request $request, $user = null) |
|
| 161 | 9 | ||
| 162 | /** |
||
| 163 | 1 | * Check if user is allowed to be here |
|
| 164 | 1 | * |
|
| 165 | * @param UserInterface|string $user |
||
| 166 | 1 | * @throws Exception |
|
| 167 | 1 | */ |
|
| 168 | 1 | public function checkPermissions($user) |
|
| 200 | |||
| 201 | 5 | /** |
|
| 202 | * Create and return a new entity. |
||
| 203 | * |
||
| 204 | 5 | * @return object |
|
| 205 | */ |
||
| 206 | 5 | public function create() |
|
| 220 | |||
| 221 | 2 | /** |
|
| 222 | * Save entity via admin manager. Error are catch, logged and a flash message is added to session |
||
| 223 | * |
||
| 224 | 2 | * @return bool true if the entity was saved without errors |
|
| 225 | 2 | */ |
|
| 226 | View Code Duplication | public function save() |
|
| 250 | |||
| 251 | 2 | /** |
|
| 252 | * Remove an entity with data provider |
||
| 253 | * |
||
| 254 | 2 | * @return bool true if the entity was saved without errors |
|
| 255 | 2 | */ |
|
| 256 | View Code Duplication | public function remove() |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Generate a route for admin and action name (like lag.admin.my_admin) |
||
| 283 | * |
||
| 284 | * @param $actionName |
||
| 285 | 15 | * |
|
| 286 | * @return string |
||
| 287 | 15 | * |
|
| 288 | 2 | * @throws Exception |
|
| 289 | 2 | */ |
|
| 290 | 2 | public function generateRouteName($actionName) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Load entities manually according to criteria. |
||
| 311 | * |
||
| 312 | * @param array $criteria |
||
| 313 | 9 | * @param array $orderBy |
|
| 314 | * @param int $limit |
||
| 315 | 9 | * @param int $offset |
|
| 316 | 9 | * @throws Exception |
|
| 317 | 9 | */ |
|
| 318 | 9 | public function load(array $criteria, $orderBy = [], $limit = 25, $offset = 1) |
|
| 357 | |||
| 358 | 2 | /** |
|
| 359 | * Return loaded entities |
||
| 360 | 2 | * |
|
| 361 | * @return Collection |
||
| 362 | */ |
||
| 363 | public function getEntities() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Return entity for current admin. If entity does not exist, it throws an exception. |
||
| 370 | 1 | * |
|
| 371 | * @return mixed |
||
| 372 | 1 | * |
|
| 373 | 1 | * @throws Exception |
|
| 374 | */ |
||
| 375 | 1 | public function getUniqueEntity() |
|
| 385 | |||
| 386 | 19 | /** |
|
| 387 | * Return admin name |
||
| 388 | 19 | * |
|
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | public function getName() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Return true if current action is granted for user. |
||
| 398 | * |
||
| 399 | 2 | * @param string $actionName Le plus grand de tous les héros |
|
| 400 | * @param array $roles |
||
| 401 | 2 | * |
|
| 402 | * @return bool |
||
| 403 | */ |
||
| 404 | 2 | public function isActionGranted($actionName, array $roles) |
|
| 427 | |||
| 428 | 10 | /** |
|
| 429 | * @return ActionInterface[] |
||
| 430 | */ |
||
| 431 | public function getActions() |
||
| 435 | |||
| 436 | 2 | /** |
|
| 437 | * @return integer[] |
||
| 438 | */ |
||
| 439 | public function getActionNames() |
||
| 443 | |||
| 444 | 9 | /** |
|
| 445 | * @param $name |
||
| 446 | 9 | * @return ActionInterface |
|
| 447 | 1 | * @throws Exception |
|
| 448 | 1 | */ |
|
| 449 | 1 | public function getAction($name) |
|
| 459 | |||
| 460 | /** |
||
| 461 | 1 | * Return if an action with specified name exists form this admin. |
|
| 462 | * |
||
| 463 | 1 | * @param $name |
|
| 464 | * @return bool |
||
| 465 | */ |
||
| 466 | public function hasAction($name) |
||
| 470 | 15 | ||
| 471 | /** |
||
| 472 | 15 | * @param ActionInterface $action |
|
| 473 | 15 | * @return void |
|
| 474 | */ |
||
| 475 | public function addAction(ActionInterface $action) |
||
| 479 | |||
| 480 | /** |
||
| 481 | 10 | * Return the current action or an exception if it is not set. |
|
| 482 | * |
||
| 483 | 10 | * @return ActionInterface |
|
| 484 | * @throws Exception |
||
| 485 | 1 | */ |
|
| 486 | public function getCurrentAction() |
||
| 497 | |||
| 498 | 1 | /** |
|
| 499 | * Return if the current action has been initialized and set. |
||
| 500 | 1 | * |
|
| 501 | * @return boolean |
||
| 502 | */ |
||
| 503 | public function isCurrentActionDefined() |
||
| 507 | |||
| 508 | 19 | /** |
|
| 509 | * Return admin configuration object. |
||
| 510 | 19 | * |
|
| 511 | * @return AdminConfiguration |
||
| 512 | */ |
||
| 513 | public function getConfiguration() |
||
| 517 | |||
| 518 | /** |
||
| 519 | 5 | * Return a translation key for a message according to the Admin's translation pattern. |
|
| 520 | * |
||
| 521 | 5 | * @param string $message |
|
| 522 | 5 | * @return string |
|
| 523 | 5 | */ |
|
| 524 | 5 | protected function generateMessageTranslationKey($message) |
|
| 532 | } |
||
| 533 |