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 | * Entities collection. |
||
| 31 | * |
||
| 32 | * @var ArrayCollection |
||
| 33 | */ |
||
| 34 | protected $entities; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var MessageHandlerInterface |
||
| 38 | */ |
||
| 39 | protected $messageHandler; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var EntityManagerInterface |
||
| 43 | */ |
||
| 44 | protected $entityManager; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var DataProviderInterface |
||
| 48 | */ |
||
| 49 | protected $dataProvider; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Admin configuration object |
||
| 53 | * |
||
| 54 | * @var AdminConfiguration |
||
| 55 | */ |
||
| 56 | protected $configuration; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Admin configured actions |
||
| 60 | * |
||
| 61 | * @var ActionInterface[] |
||
| 62 | */ |
||
| 63 | protected $actions = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Admin current action. It will be set after calling the handleRequest() |
||
| 67 | * |
||
| 68 | * @var ActionInterface |
||
| 69 | */ |
||
| 70 | protected $currentAction; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Admin name |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $name; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var EventDispatcherInterface |
||
| 81 | */ |
||
| 82 | protected $eventDispatcher; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var RequestFilterInterface |
||
| 86 | */ |
||
| 87 | protected $requestFilter; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Admin constructor. |
||
| 91 | * |
||
| 92 | * @param string $name |
||
| 93 | * @param DataProviderInterface $dataProvider |
||
| 94 | * @param AdminConfiguration $configuration |
||
| 95 | 27 | * @param MessageHandlerInterface $messageHandler |
|
| 96 | * @param EventDispatcherInterface $eventDispatcher |
||
| 97 | * @param RequestFilterInterface $requestFilter |
||
| 98 | */ |
||
| 99 | public function __construct( |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Load entities and set current action according to request. |
||
| 118 | 9 | * |
|
| 119 | * @param Request $request |
||
| 120 | * @param null $user |
||
| 121 | 9 | * @return void |
|
| 122 | * @throws AdminException |
||
| 123 | 9 | */ |
|
| 124 | public function handleRequest(Request $request, $user = null) |
||
| 158 | 9 | ||
| 159 | /** |
||
| 160 | 9 | * Check if user is allowed to be here |
|
| 161 | 9 | * |
|
| 162 | * @param UserInterface|string $user |
||
| 163 | 1 | * @throws Exception |
|
| 164 | 1 | */ |
|
| 165 | public function checkPermissions($user) |
||
| 197 | |||
| 198 | /** |
||
| 199 | 5 | * Create and return a new entity. |
|
| 200 | * |
||
| 201 | 5 | * @return object |
|
| 202 | */ |
||
| 203 | public function create() |
||
| 217 | |||
| 218 | /** |
||
| 219 | 2 | * Save entity via admin manager. Error are catch, logged and a flash message is added to session |
|
| 220 | 2 | * |
|
| 221 | * @return bool true if the entity was saved without errors |
||
| 222 | 2 | */ |
|
| 223 | 1 | View Code Duplication | public function save() |
| 247 | |||
| 248 | /** |
||
| 249 | 2 | * Remove an entity with data provider |
|
| 250 | 2 | * |
|
| 251 | * @return bool true if the entity was saved without errors |
||
| 252 | 2 | */ |
|
| 253 | 1 | View Code Duplication | public function remove() |
| 277 | |||
| 278 | /** |
||
| 279 | * Generate a route for admin and action name (like lag.admin.my_admin) |
||
| 280 | 16 | * |
|
| 281 | * @param $actionName |
||
| 282 | 16 | * |
|
| 283 | 2 | * @return string |
|
| 284 | 2 | * |
|
| 285 | 2 | * @throws Exception |
|
| 286 | 2 | */ |
|
| 287 | 2 | public function generateRouteName($actionName) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Load entities according to the given criteria and the current action configuration. |
||
| 308 | 9 | * |
|
| 309 | * @param array $criteria |
||
| 310 | 9 | * @param array $orderBy |
|
| 311 | 9 | * @param int $limit |
|
| 312 | 9 | * @param int $offset |
|
| 313 | 9 | * @throws Exception |
|
| 314 | 9 | */ |
|
| 315 | 9 | public function load(array $criteria, array $orderBy = [], $limit = 25, $offset = 1) |
|
| 366 | |||
| 367 | 1 | /** |
|
| 368 | 1 | * Return loaded entities |
|
| 369 | * |
||
| 370 | 1 | * @return Collection |
|
| 371 | 1 | */ |
|
| 372 | public function getEntities() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Return entity for current admin. If entity does not exist, it throws an exception. |
||
| 379 | * |
||
| 380 | * @return mixed |
||
| 381 | 20 | * |
|
| 382 | * @throws Exception |
||
| 383 | 20 | */ |
|
| 384 | public function getUniqueEntity() |
||
| 396 | 2 | ||
| 397 | /** |
||
| 398 | * Return admin name |
||
| 399 | 2 | * |
|
| 400 | 2 | * @return string |
|
| 401 | */ |
||
| 402 | 2 | public function getName() |
|
| 406 | 2 | ||
| 407 | 2 | /** |
|
| 408 | 2 | * Return true if current action is granted for user. |
|
| 409 | 2 | * |
|
| 410 | 2 | * @param string $actionName Le plus grand de tous les héros |
|
| 411 | 2 | * @param array $roles |
|
| 412 | 2 | * |
|
| 413 | 2 | * @return bool |
|
| 414 | */ |
||
| 415 | 2 | public function isActionGranted($actionName, array $roles) |
|
| 438 | |||
| 439 | 9 | /** |
|
| 440 | * @return ActionInterface[] |
||
| 441 | 9 | */ |
|
| 442 | 1 | public function getActions() |
|
| 446 | |||
| 447 | 9 | /** |
|
| 448 | * @return integer[] |
||
| 449 | */ |
||
| 450 | public function getActionNames() |
||
| 454 | |||
| 455 | /** |
||
| 456 | 1 | * @param $name |
|
| 457 | * @return ActionInterface |
||
| 458 | 1 | * @throws Exception |
|
| 459 | */ |
||
| 460 | public function getAction($name) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Return if an action with specified name exists form this admin. |
||
| 473 | * |
||
| 474 | * @param $name |
||
| 475 | * @return bool |
||
| 476 | 10 | */ |
|
| 477 | public function hasAction($name) |
||
| 481 | |||
| 482 | 1 | /** |
|
| 483 | * @param ActionInterface $action |
||
| 484 | * @return void |
||
| 485 | 9 | */ |
|
| 486 | public function addAction(ActionInterface $action) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Return the current action or an exception if it is not set. |
||
| 493 | 1 | * |
|
| 494 | * @return ActionInterface |
||
| 495 | 1 | * @throws Exception |
|
| 496 | */ |
||
| 497 | public function getCurrentAction() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Return if the current action has been initialized and set. |
||
| 511 | * |
||
| 512 | * @return boolean |
||
| 513 | */ |
||
| 514 | 4 | public function isCurrentActionDefined() |
|
| 518 | 4 | ||
| 519 | 4 | /** |
|
| 520 | 4 | * Return admin configuration object. |
|
| 521 | * |
||
| 522 | * @return AdminConfiguration |
||
| 523 | */ |
||
| 524 | public function getConfiguration() |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Return a translation key for a message according to the Admin's translation pattern. |
||
| 531 | * |
||
| 532 | * @param string $message |
||
| 533 | * @return string |
||
| 534 | */ |
||
| 535 | protected function generateMessageTranslationKey($message) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Load entities using PagerFanta. |
||
| 546 | * |
||
| 547 | * @param array $criteria |
||
| 548 | * @param array $orderBy |
||
| 549 | * @param int $limit |
||
| 550 | * @param int $offset |
||
| 551 | */ |
||
| 552 | protected function loadPaginate(array $criteria, array $orderBy, $limit, $offset) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Load entities using to configured data provider. |
||
| 568 | * |
||
| 569 | * @param array $criteria |
||
| 570 | * @param array $orderBy |
||
| 571 | * @param int $limit |
||
| 572 | * @param int $offset |
||
| 573 | */ |
||
| 574 | protected function loadWithoutPagination(array $criteria, $orderBy, $limit, $offset) |
||
| 589 | } |
||
| 590 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..