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 | * @param MessageHandlerInterface $messageHandler |
||
| 96 | * @param EventDispatcherInterface $eventDispatcher |
||
| 97 | * @param RequestFilterInterface $requestFilter |
||
| 98 | */ |
||
| 99 | 33 | public function __construct( |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Load entities and set current action according to request. |
||
| 118 | * |
||
| 119 | * @param Request $request |
||
| 120 | * @param null $user |
||
| 121 | * @return void |
||
| 122 | * @throws AdminException |
||
| 123 | */ |
||
| 124 | 12 | public function handleRequest(Request $request, $user = null) |
|
| 125 | { |
||
| 126 | // set current action |
||
| 127 | 12 | $this->currentAction = $this->getAction($request->get('_route_params')['_action']); |
|
| 128 | |||
| 129 | // check if user is logged have required permissions to get current action |
||
| 130 | 12 | $this->checkPermissions($user); |
|
| 131 | |||
| 132 | $actionConfiguration = $this |
||
| 133 | 12 | ->currentAction |
|
| 134 | 12 | ->getConfiguration(); |
|
| 135 | |||
| 136 | // configure the request filter with the action and admin configured parameters |
||
| 137 | $this |
||
| 138 | 12 | ->requestFilter |
|
| 139 | 12 | ->configure( |
|
| 140 | 12 | $actionConfiguration->getParameter('criteria'), |
|
| 141 | 12 | $actionConfiguration->getParameter('order'), |
|
| 142 | 12 | $this->configuration->getParameter('max_per_page') |
|
| 143 | ); |
||
| 144 | |||
| 145 | // filter the request with the configured criteria, order and max_per_page parameter |
||
| 146 | $this |
||
| 147 | 12 | ->requestFilter |
|
| 148 | 12 | ->filter($request); |
|
| 149 | |||
| 150 | // load entities according to action and request |
||
| 151 | 12 | $this->load( |
|
| 152 | 12 | $this->requestFilter->getCriteria(), |
|
| 153 | 12 | $this->requestFilter->getOrder(), |
|
| 154 | 12 | $this->requestFilter->getMaxPerPage(), |
|
| 155 | 12 | $this->requestFilter->getCurrentPage() |
|
| 156 | ); |
||
| 157 | 1 | } |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Check if user is allowed to be here |
||
| 161 | * |
||
| 162 | * @param UserInterface|string $user |
||
| 163 | * @throws Exception |
||
| 164 | */ |
||
| 165 | 12 | public function checkPermissions($user) |
|
| 166 | { |
||
| 167 | 12 | if (!($user instanceof UserInterface)) { |
|
| 168 | 12 | return; |
|
| 169 | } |
||
| 170 | 1 | if ($this->currentAction === null) { |
|
| 171 | 1 | throw new Exception('Current action should be set before checking the permissions'); |
|
| 172 | } |
||
| 173 | $roles = $user->getRoles(); |
||
| 174 | $actionName = $this |
||
| 175 | ->getCurrentAction() |
||
| 176 | ->getName(); |
||
| 177 | |||
| 178 | if (!$this->isActionGranted($actionName, $roles)) { |
||
| 179 | $rolesStringArray = []; |
||
| 180 | |||
| 181 | foreach ($roles as $role) { |
||
| 182 | |||
| 183 | if ($role instanceof Role) { |
||
| 184 | $rolesStringArray[] = $role->getRole(); |
||
| 185 | } else { |
||
| 186 | $rolesStringArray[] = $role; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | $message = sprintf('User with roles %s not allowed for action "%s"', |
||
| 191 | implode(', ', $rolesStringArray), |
||
| 192 | $actionName |
||
| 193 | ); |
||
| 194 | throw new NotFoundHttpException($message); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Create and return a new entity. |
||
| 200 | * |
||
| 201 | * @return object |
||
| 202 | */ |
||
| 203 | 5 | public function create() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Save entity via admin manager. Error are catch, logged and a flash message is added to session |
||
| 220 | * |
||
| 221 | * @return bool true if the entity was saved without errors |
||
| 222 | */ |
||
| 223 | 2 | View Code Duplication | public function save() |
| 247 | |||
| 248 | /** |
||
| 249 | * Remove an entity with data provider |
||
| 250 | * |
||
| 251 | * @return bool true if the entity was saved without errors |
||
| 252 | */ |
||
| 253 | 2 | View Code Duplication | public function remove() |
| 277 | |||
| 278 | /** |
||
| 279 | * Generate a route for admin and action name (like lag.admin.my_admin) |
||
| 280 | * |
||
| 281 | * @param $actionName |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | * |
||
| 285 | * @throws Exception |
||
| 286 | */ |
||
| 287 | 19 | public function generateRouteName($actionName) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Load entities according to the given criteria and the current action configuration. |
||
| 308 | * |
||
| 309 | * @param array $criteria |
||
| 310 | * @param array $orderBy |
||
| 311 | * @param int $limit |
||
| 312 | * @param int $offset |
||
| 313 | * @throws Exception |
||
| 314 | */ |
||
| 315 | 12 | public function load(array $criteria, array $orderBy = [], $limit = 25, $offset = 1) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Return loaded entities |
||
| 373 | * |
||
| 374 | * @return Collection |
||
| 375 | */ |
||
| 376 | 1 | public function getEntities() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Return entity for current admin. If entity does not exist, it throws an exception. |
||
| 383 | * |
||
| 384 | * @return mixed |
||
| 385 | * |
||
| 386 | * @throws Exception |
||
| 387 | */ |
||
| 388 | public function getUniqueEntity() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Return admin name |
||
| 403 | * |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | 26 | public function getName() |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Return true if current action is granted for user. |
||
| 413 | * |
||
| 414 | * @param string $actionName Le plus grand de tous les héros |
||
| 415 | * @param array $roles |
||
| 416 | * |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | public function isActionGranted($actionName, array $roles) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return ActionInterface[] |
||
| 445 | */ |
||
| 446 | 13 | public function getActions() |
|
| 450 | |||
| 451 | /** |
||
| 452 | * @return integer[] |
||
| 453 | */ |
||
| 454 | 2 | public function getActionNames() |
|
| 458 | |||
| 459 | /** |
||
| 460 | * @param $name |
||
| 461 | * @return ActionInterface |
||
| 462 | * @throws Exception |
||
| 463 | */ |
||
| 464 | 12 | public function getAction($name) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Return if an action with specified name exists form this admin. |
||
| 477 | * |
||
| 478 | * @param $name |
||
| 479 | * @return bool |
||
| 480 | */ |
||
| 481 | public function hasAction($name) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param ActionInterface $action |
||
| 488 | * @return void |
||
| 489 | */ |
||
| 490 | 18 | public function addAction(ActionInterface $action) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Return the current action or an exception if it is not set. |
||
| 497 | * |
||
| 498 | * @return ActionInterface |
||
| 499 | * @throws Exception |
||
| 500 | */ |
||
| 501 | 13 | public function getCurrentAction() |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Return if the current action has been initialized and set. |
||
| 515 | * |
||
| 516 | * @return boolean |
||
| 517 | */ |
||
| 518 | 1 | public function isCurrentActionDefined() |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Return admin configuration object. |
||
| 525 | * |
||
| 526 | * @return AdminConfiguration |
||
| 527 | */ |
||
| 528 | 23 | public function getConfiguration() |
|
| 532 | |||
| 533 | /** |
||
| 534 | * Return a translation key for a message according to the Admin's translation pattern. |
||
| 535 | * |
||
| 536 | * @param string $message |
||
| 537 | * @return string |
||
| 538 | */ |
||
| 539 | 4 | protected function generateMessageTranslationKey($message) |
|
| 547 | |||
| 548 | /** |
||
| 549 | * Load entities using PagerFanta. |
||
| 550 | * |
||
| 551 | * @param array $criteria |
||
| 552 | * @param array $orderBy |
||
| 553 | * @param int $limit |
||
| 554 | * @param int $offset |
||
| 555 | * |
||
| 556 | * @return array|\Traversable |
||
| 557 | */ |
||
| 558 | 1 | protected function loadPaginate(array $criteria, array $orderBy, $limit, $offset) |
|
| 571 | |||
| 572 | /** |
||
| 573 | * Load entities using to configured data provider. |
||
| 574 | * |
||
| 575 | * @param array $criteria |
||
| 576 | * @param array $orderBy |
||
| 577 | * @param int $limit |
||
| 578 | * @param int $offset |
||
| 579 | * |
||
| 580 | * @return array|Collection |
||
| 581 | */ |
||
| 582 | 8 | protected function loadWithoutPagination(array $criteria, $orderBy, $limit, $offset) |
|
| 597 | } |
||
| 598 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: