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 |
||
| 25 | class Admin implements AdminInterface |
||
| 26 | { |
||
| 27 | use AdminTrait; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Do not load entities on handleRequest (for create method for example) |
||
| 31 | */ |
||
| 32 | const LOAD_STRATEGY_NONE = 'strategy_none'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Load one entity on handleRequest (edit method for example) |
||
| 36 | */ |
||
| 37 | const LOAD_STRATEGY_UNIQUE = 'strategy_unique'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Load multiple entities on handleRequest (list method for example) |
||
| 41 | */ |
||
| 42 | const LOAD_STRATEGY_MULTIPLE = 'strategy_multiple'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Entities collection. |
||
| 46 | * |
||
| 47 | * @var ArrayCollection |
||
| 48 | */ |
||
| 49 | protected $entities; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var MessageHandlerInterface |
||
| 53 | */ |
||
| 54 | protected $messageHandler; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var EntityManagerInterface |
||
| 58 | */ |
||
| 59 | protected $entityManager; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var DataProviderInterface |
||
| 63 | */ |
||
| 64 | protected $dataProvider; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Admin configuration object |
||
| 68 | * |
||
| 69 | * @var AdminConfiguration |
||
| 70 | */ |
||
| 71 | protected $configuration; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Admin configured actions |
||
| 75 | * |
||
| 76 | * @var ActionInterface[] |
||
| 77 | */ |
||
| 78 | protected $actions = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Admin current action. It will be set after calling the handleRequest() |
||
| 82 | * |
||
| 83 | * @var ActionInterface |
||
| 84 | */ |
||
| 85 | protected $currentAction; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Admin name |
||
| 89 | * |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | protected $name; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Admin constructor. |
||
| 96 | * |
||
| 97 | * @param string $name |
||
| 98 | * @param DataProviderInterface $dataProvider |
||
| 99 | * @param AdminConfiguration $configuration |
||
| 100 | * @param MessageHandlerInterface $messageHandler |
||
| 101 | */ |
||
| 102 | 15 | public function __construct( |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Load entities and set current action according to request |
||
| 117 | * |
||
| 118 | * @param Request $request |
||
| 119 | * @param null $user |
||
| 120 | * @return void |
||
| 121 | * @throws AdminException |
||
| 122 | */ |
||
| 123 | 5 | public function handleRequest(Request $request, $user = null) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Check if user is allowed to be here |
||
| 159 | * |
||
| 160 | * @param UserInterface|string $user |
||
| 161 | * @throws Exception |
||
| 162 | */ |
||
| 163 | 5 | public function checkPermissions($user) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Create and return a new entity. |
||
| 187 | * |
||
| 188 | * @return object |
||
| 189 | */ |
||
| 190 | 3 | public function create() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Save entity via admin manager. Error are catch, logged and a flash message is added to session |
||
| 207 | * |
||
| 208 | * @return bool true if the entity was saved without errors |
||
| 209 | */ |
||
| 210 | 1 | View Code Duplication | public function save() |
| 234 | |||
| 235 | /** |
||
| 236 | * Remove an entity with data provider |
||
| 237 | * |
||
| 238 | * @return bool true if the entity was saved without errors |
||
| 239 | */ |
||
| 240 | 1 | View Code Duplication | public function remove() |
| 264 | |||
| 265 | /** |
||
| 266 | * Generate a route for admin and action name (like lag.admin.my_admin) |
||
| 267 | * |
||
| 268 | * @param $actionName |
||
| 269 | * |
||
| 270 | * @return string |
||
| 271 | * |
||
| 272 | * @throws Exception |
||
| 273 | */ |
||
| 274 | 1 | public function generateRouteName($actionName) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Load entities manually according to criteria. |
||
| 291 | * |
||
| 292 | * @param array $criteria |
||
| 293 | * @param array $orderBy |
||
| 294 | * @param int $limit |
||
| 295 | * @param int $offset |
||
| 296 | * @throws Exception |
||
| 297 | */ |
||
| 298 | 5 | public function load(array $criteria, $orderBy = [], $limit = 25, $offset = 1) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Return loaded entities |
||
| 333 | * |
||
| 334 | * @return Collection |
||
| 335 | */ |
||
| 336 | 2 | public function getEntities() |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Return entity for current admin. If entity does not exist, it throws an exception. |
||
| 343 | * |
||
| 344 | * @return mixed |
||
| 345 | * |
||
| 346 | * @throws Exception |
||
| 347 | */ |
||
| 348 | 1 | public function getUniqueEntity() |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Return admin name |
||
| 361 | * |
||
| 362 | * @return string |
||
| 363 | */ |
||
| 364 | 10 | public function getName() |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Return true if current action is granted for user. |
||
| 371 | * |
||
| 372 | * @param string $actionName Le plus grand de tous les héros |
||
| 373 | * @param array $roles |
||
| 374 | * |
||
| 375 | * @return bool |
||
| 376 | */ |
||
| 377 | 1 | public function isActionGranted($actionName, array $roles) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * @return ActionInterface[] |
||
| 403 | */ |
||
| 404 | 6 | public function getActions() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * @return array |
||
| 411 | */ |
||
| 412 | 1 | public function getActionNames() |
|
| 416 | |||
| 417 | /** |
||
| 418 | * @param $name |
||
| 419 | * @return ActionInterface |
||
| 420 | * @throws Exception |
||
| 421 | */ |
||
| 422 | 5 | public function getAction($name) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Return if an action with specified name exists form this admin. |
||
| 435 | * |
||
| 436 | * @param $name |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | public function hasAction($name) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param ActionInterface $action |
||
| 446 | * @return void |
||
| 447 | */ |
||
| 448 | 11 | public function addAction(ActionInterface $action) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * @return ActionInterface |
||
| 455 | * @throws Exception |
||
| 456 | */ |
||
| 457 | 5 | public function getCurrentAction() |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Return admin configuration object |
||
| 471 | * |
||
| 472 | * @return AdminConfiguration |
||
| 473 | */ |
||
| 474 | 9 | public function getConfiguration() |
|
| 478 | } |
||
| 479 |