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 DataProviderInterface |
||
| 44 | */ |
||
| 45 | protected $dataProvider; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Admin configuration object |
||
| 49 | * |
||
| 50 | * @var AdminConfiguration |
||
| 51 | */ |
||
| 52 | protected $configuration; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Admin configured actions |
||
| 56 | * |
||
| 57 | * @var ActionInterface[] |
||
| 58 | */ |
||
| 59 | protected $actions = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Admin current action. It will be set after calling the handleRequest() |
||
| 63 | * |
||
| 64 | * @var ActionInterface |
||
| 65 | */ |
||
| 66 | protected $currentAction; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Admin name |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $name; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var EventDispatcherInterface |
||
| 77 | */ |
||
| 78 | protected $eventDispatcher; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var AuthorizationCheckerInterface |
||
| 82 | */ |
||
| 83 | protected $authorizationChecker; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var TokenStorageInterface |
||
| 87 | */ |
||
| 88 | protected $tokenStorage; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var FormInterface|null |
||
| 92 | */ |
||
| 93 | protected $filterForm; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var EntityLoaderInterface |
||
| 97 | */ |
||
| 98 | protected $entityLoader; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Admin constructor. |
||
| 102 | * |
||
| 103 | * @param string $name |
||
| 104 | * @param EntityLoaderInterface $entityLoader |
||
| 105 | * @param AdminConfiguration $configuration |
||
| 106 | * @param MessageHandlerInterface $messageHandler |
||
| 107 | * @param EventDispatcherInterface $eventDispatcher |
||
| 108 | * @param AuthorizationCheckerInterface $authorizationChecker |
||
| 109 | * @param TokenStorageInterface $tokenStorage |
||
| 110 | * @param array $actions |
||
| 111 | */ |
||
| 112 | 41 | public function __construct( |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Load entities and set current action according to request and the optional filters. |
||
| 136 | * |
||
| 137 | * @param Request $request |
||
| 138 | * @param array $filters |
||
| 139 | */ |
||
| 140 | 20 | public function handleRequest(Request $request, array $filters = []) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Check if user is allowed to be here. |
||
| 185 | * |
||
| 186 | * @throws LogicException|AccessDeniedException |
||
| 187 | */ |
||
| 188 | 24 | public function checkPermissions() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Create and return a new entity. |
||
| 225 | * |
||
| 226 | * @return object |
||
| 227 | */ |
||
| 228 | 12 | public function create() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Save entity via admin manager. |
||
| 245 | */ |
||
| 246 | 4 | public function save() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Remove an entity with data provider. |
||
| 263 | */ |
||
| 264 | 4 | public function remove() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Return the number of entities managed by the Admin. |
||
| 280 | * |
||
| 281 | * @return int |
||
| 282 | */ |
||
| 283 | public function count() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Generate a route for admin and action name (like lag.admin.my_admin) |
||
| 301 | * |
||
| 302 | * @param $actionName |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | * |
||
| 306 | * @throws Exception |
||
| 307 | */ |
||
| 308 | 4 | public function generateRouteName($actionName) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Load entities according to the given criteria and the current action configuration. |
||
| 338 | * |
||
| 339 | * @param array $criteria |
||
| 340 | * @param array $orderBy |
||
| 341 | * @param int $limit |
||
| 342 | * @param int $offset |
||
| 343 | */ |
||
| 344 | 8 | public function load(array $criteria, array $orderBy = [], $limit = 25, $offset = 1) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Return loaded entities |
||
| 375 | * |
||
| 376 | * @return Collection |
||
| 377 | */ |
||
| 378 | 4 | public function getEntities() |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Return entity for current admin. If entity does not exist, it throws an exception. |
||
| 385 | * |
||
| 386 | * @return mixed |
||
| 387 | * |
||
| 388 | * @throws Exception |
||
| 389 | */ |
||
| 390 | public function getUniqueEntity() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Return admin name |
||
| 408 | * |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | 4 | public function getName() |
|
| 415 | |||
| 416 | /** |
||
| 417 | * @return ActionInterface[] |
||
| 418 | */ |
||
| 419 | 20 | public function getActions() |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @return integer[] |
||
| 426 | */ |
||
| 427 | 4 | public function getActionNames() |
|
| 431 | |||
| 432 | /** |
||
| 433 | * @param $name |
||
| 434 | * @return ActionInterface |
||
| 435 | * @throws Exception |
||
| 436 | */ |
||
| 437 | 20 | public function getAction($name) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Return true if the Action with name $name exists in the Admin. If the method return true, it does not necessarily |
||
| 450 | * means that the action is allowed in the current context. |
||
| 451 | * |
||
| 452 | * @param string $name |
||
| 453 | * |
||
| 454 | * @return boolean |
||
| 455 | */ |
||
| 456 | public function hasAction($name) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @param ActionInterface $action |
||
| 463 | * @return void |
||
| 464 | */ |
||
| 465 | 24 | public function addAction(ActionInterface $action) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Return the current action or an exception if it is not set. |
||
| 472 | * |
||
| 473 | * @return ActionInterface |
||
| 474 | * @throws Exception |
||
| 475 | */ |
||
| 476 | public function getCurrentAction() |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Return if the current action has been initialized and set. |
||
| 490 | * |
||
| 491 | * @return boolean |
||
| 492 | */ |
||
| 493 | public function isCurrentActionDefined() |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Return admin configuration object. |
||
| 500 | * |
||
| 501 | * @return AdminConfiguration |
||
| 502 | */ |
||
| 503 | public function getConfiguration() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Return the filter form if it was initialized. |
||
| 510 | * |
||
| 511 | * @return FormInterface |
||
| 512 | * |
||
| 513 | * @throws Exception |
||
| 514 | */ |
||
| 515 | public function getFilterForm() |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Return true if the filter form has been set. |
||
| 529 | * |
||
| 530 | * @return bool |
||
| 531 | */ |
||
| 532 | public function hasFilterForm() |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Return a translation key for a message according to the Admin's translation pattern. |
||
| 539 | * |
||
| 540 | * @param string $message |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | 8 | protected function generateMessageTranslationKey($message) |
|
| 551 | } |
||
| 552 |
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..