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 |
||
| 26 | class Admin implements AdminInterface |
||
| 27 | { |
||
| 28 | use AdminTrait; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Entities collection. |
||
| 32 | * |
||
| 33 | * @var ArrayCollection |
||
| 34 | */ |
||
| 35 | protected $entities; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var MessageHandlerInterface |
||
| 39 | */ |
||
| 40 | protected $messageHandler; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var EntityManagerInterface |
||
| 44 | */ |
||
| 45 | protected $entityManager; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var DataProviderInterface |
||
| 49 | */ |
||
| 50 | protected $dataProvider; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Admin configuration object |
||
| 54 | * |
||
| 55 | * @var AdminConfiguration |
||
| 56 | */ |
||
| 57 | protected $configuration; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Admin configured actions |
||
| 61 | * |
||
| 62 | * @var ActionInterface[] |
||
| 63 | */ |
||
| 64 | protected $actions = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Admin current action. It will be set after calling the handleRequest() |
||
| 68 | * |
||
| 69 | * @var ActionInterface |
||
| 70 | */ |
||
| 71 | protected $currentAction; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Admin name |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $name; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Admin constructor. |
||
| 82 | * |
||
| 83 | * @param string $name |
||
| 84 | * @param DataProviderInterface $dataProvider |
||
| 85 | * @param AdminConfiguration $configuration |
||
| 86 | * @param MessageHandlerInterface $messageHandler |
||
| 87 | */ |
||
| 88 | 19 | public function __construct( |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Load entities and set current action according to request |
||
| 103 | * |
||
| 104 | * @param Request $request |
||
| 105 | * @param null $user |
||
| 106 | * @return void |
||
| 107 | * @throws AdminException |
||
| 108 | */ |
||
| 109 | 6 | public function handleRequest(Request $request, $user = null) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Check if user is allowed to be here |
||
| 145 | * |
||
| 146 | * @param UserInterface|string $user |
||
| 147 | * @throws Exception |
||
| 148 | */ |
||
| 149 | 6 | public function checkPermissions($user) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Create and return a new entity. |
||
| 184 | * |
||
| 185 | * @return object |
||
| 186 | */ |
||
| 187 | 3 | public function create() |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Save entity via admin manager. Error are catch, logged and a flash message is added to session |
||
| 209 | * |
||
| 210 | * @return bool true if the entity was saved without errors |
||
| 211 | */ |
||
| 212 | 1 | View Code Duplication | public function save() |
| 236 | |||
| 237 | /** |
||
| 238 | * Remove an entity with data provider |
||
| 239 | * |
||
| 240 | * @return bool true if the entity was saved without errors |
||
| 241 | */ |
||
| 242 | 1 | View Code Duplication | public function remove() |
| 266 | |||
| 267 | /** |
||
| 268 | * Generate a route for admin and action name (like lag.admin.my_admin) |
||
| 269 | * |
||
| 270 | * @param $actionName |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | * |
||
| 274 | * @throws Exception |
||
| 275 | */ |
||
| 276 | 10 | public function generateRouteName($actionName) |
|
| 277 | { |
||
| 278 | 10 | if (!array_key_exists($actionName, $this->getConfiguration()->getParameter('actions'))) { |
|
| 279 | 2 | throw new Exception( |
|
| 280 | 2 | sprintf('Invalid action name %s for admin %s (available action are: %s)', |
|
| 281 | $actionName, |
||
| 282 | 2 | $this->getName(), |
|
| 283 | 2 | implode(', ', $this->getActionNames())) |
|
| 284 | ); |
||
| 285 | } |
||
| 286 | // get routing name pattern |
||
| 287 | 10 | $routingPattern = $this->getConfiguration()->getParameter('routing_name_pattern'); |
|
| 288 | // replace admin and action name in pattern |
||
| 289 | 10 | $routeName = str_replace('{admin}', Container::underscore($this->getName()), $routingPattern); |
|
| 290 | 10 | $routeName = str_replace('{action}', $actionName, $routeName); |
|
| 291 | |||
| 292 | 10 | return $routeName; |
|
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Load entities manually according to criteria. |
||
| 297 | * |
||
| 298 | * @param array $criteria |
||
| 299 | * @param array $orderBy |
||
| 300 | * @param int $limit |
||
| 301 | * @param int $offset |
||
| 302 | * @throws Exception |
||
| 303 | */ |
||
| 304 | 6 | public function load(array $criteria, $orderBy = [], $limit = 25, $offset = 1) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Return loaded entities |
||
| 346 | * |
||
| 347 | * @return Collection |
||
| 348 | */ |
||
| 349 | 2 | public function getEntities() |
|
| 350 | { |
||
| 351 | 2 | return $this->entities; |
|
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Return entity for current admin. If entity does not exist, it throws an exception. |
||
| 356 | * |
||
| 357 | * @return mixed |
||
| 358 | * |
||
| 359 | * @throws Exception |
||
| 360 | */ |
||
| 361 | 1 | public function getUniqueEntity() |
|
| 362 | { |
||
| 363 | 1 | if ($this->entities->count() == 0) { |
|
| 364 | 1 | throw new Exception("Entity not found in admin \"{$this->getName()}\"."); |
|
| 365 | } |
||
| 366 | 1 | if ($this->entities->count() > 1) { |
|
| 367 | 1 | throw new Exception("Too much entities found in admin \"{$this->getName()}\"."); |
|
| 368 | } |
||
| 369 | 1 | return $this->entities->first(); |
|
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Return admin name |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | 16 | public function getName() |
|
| 378 | { |
||
| 379 | 16 | return $this->name; |
|
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Return true if current action is granted for user. |
||
| 384 | * |
||
| 385 | * @param string $actionName Le plus grand de tous les héros |
||
| 386 | * @param array $roles |
||
| 387 | * |
||
| 388 | * @return bool |
||
| 389 | */ |
||
| 390 | 2 | public function isActionGranted($actionName, array $roles) |
|
| 391 | { |
||
| 392 | 2 | $isGranted = array_key_exists($actionName, $this->actions); |
|
| 393 | |||
| 394 | // if action exists |
||
| 395 | 2 | if ($isGranted) { |
|
| 396 | 2 | $isGranted = false; |
|
| 397 | /** @var ActionInterface $action */ |
||
| 398 | 2 | $action = $this->actions[$actionName]; |
|
| 399 | // checking roles permissions |
||
| 400 | 2 | foreach ($roles as $role) { |
|
| 401 | |||
| 402 | 2 | if ($role instanceof Role) { |
|
| 403 | 2 | $role = $role->getRole(); |
|
| 404 | } |
||
| 405 | 2 | if (in_array($role, $action->getPermissions())) { |
|
| 406 | 2 | $isGranted = true; |
|
| 407 | } |
||
| 408 | } |
||
| 409 | } |
||
| 410 | |||
| 411 | 2 | return $isGranted; |
|
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @return ActionInterface[] |
||
| 416 | */ |
||
| 417 | 7 | public function getActions() |
|
| 421 | |||
| 422 | /** |
||
| 423 | * @return integer[] |
||
| 424 | */ |
||
| 425 | 2 | public function getActionNames() |
|
| 426 | { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param $name |
||
| 432 | * @return ActionInterface |
||
| 433 | * @throws Exception |
||
| 434 | */ |
||
| 435 | 6 | public function getAction($name) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Return if an action with specified name exists form this admin. |
||
| 448 | * |
||
| 449 | * @param $name |
||
| 450 | * @return bool |
||
| 451 | */ |
||
| 452 | 1 | public function hasAction($name) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * @param ActionInterface $action |
||
| 459 | * @return void |
||
| 460 | */ |
||
| 461 | 14 | public function addAction(ActionInterface $action) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Return the current action or an exception if it is not set. |
||
| 468 | * |
||
| 469 | * @return ActionInterface |
||
| 470 | * @throws Exception |
||
| 471 | */ |
||
| 472 | 7 | public function getCurrentAction() |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Return if the current action has been initialized and set. |
||
| 486 | * |
||
| 487 | * @return boolean |
||
| 488 | */ |
||
| 489 | 1 | public function isCurrentActionDefined() |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Return admin configuration object. |
||
| 496 | * |
||
| 497 | * @return AdminConfiguration |
||
| 498 | */ |
||
| 499 | 16 | public function getConfiguration() |
|
| 503 | |||
| 504 | /** |
||
| 505 | * Return an translation key for a message according to the Admin's translation pattern. |
||
| 506 | * |
||
| 507 | * @param string $message |
||
| 508 | * @return string |
||
| 509 | */ |
||
| 510 | 3 | protected function generateMessageTranslationKey($message) |
|
| 518 | } |
||
| 519 |