Complex classes like Pool 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 Pool, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Pool |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var ContainerInterface |
||
| 29 | */ |
||
| 30 | protected $container; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string[] |
||
| 34 | */ |
||
| 35 | protected $adminServiceIds = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $adminGroups = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $adminClasses = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @deprecated since 3.34, will be dropped in 4.0. Use TemplateRegistry "sonata.admin.global_template_registry" instead |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $templates = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $assets = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $title; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $titleLogo; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $options; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var PropertyAccessorInterface |
||
| 76 | */ |
||
| 77 | protected $propertyAccessor; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var MutableTemplateRegistryInterface |
||
| 81 | */ |
||
| 82 | private $templateRegistry; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param string $title |
||
| 86 | * @param string $logoTitle |
||
| 87 | * @param array $options |
||
| 88 | */ |
||
| 89 | public function __construct( |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | public function getGroups() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Returns whether an admin group exists or not. |
||
| 121 | * |
||
| 122 | * @param string $group |
||
| 123 | * |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | public function hasGroup($group) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | public function getDashboardGroups() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Returns all admins related to the given $group. |
||
| 166 | * |
||
| 167 | * @param string $group |
||
| 168 | * |
||
| 169 | * @throws \InvalidArgumentException |
||
| 170 | * |
||
| 171 | * @return AdminInterface[] |
||
| 172 | */ |
||
| 173 | public function getAdminsByGroup($group) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Return the admin related to the given $class. |
||
| 194 | * |
||
| 195 | * @param string $class |
||
| 196 | * |
||
| 197 | * @return \Sonata\AdminBundle\Admin\AdminInterface|null |
||
| 198 | */ |
||
| 199 | public function getAdminByClass($class) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $class |
||
| 222 | * |
||
| 223 | * @return bool |
||
| 224 | */ |
||
| 225 | public function hasAdminByClass($class) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Returns an admin class by its Admin code |
||
| 232 | * ie : sonata.news.admin.post|sonata.news.admin.comment => return the child class of post. |
||
| 233 | * |
||
| 234 | * @param string $adminCode |
||
| 235 | * |
||
| 236 | * @throws \InvalidArgumentException if the root admin code is an empty string |
||
| 237 | * |
||
| 238 | * @return AdminInterface|false |
||
| 239 | */ |
||
| 240 | public function getAdminByAdminCode($adminCode) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Checks if an admin with a certain admin code exists. |
||
| 297 | */ |
||
| 298 | final public function hasAdminByAdminCode(string $adminCode): bool |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Returns a new admin instance depends on the given code. |
||
| 314 | * |
||
| 315 | * @param string $id |
||
| 316 | * |
||
| 317 | * @throws \InvalidArgumentException |
||
| 318 | * |
||
| 319 | * @return AdminInterface |
||
| 320 | */ |
||
| 321 | public function getInstance($id) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @return ContainerInterface|null |
||
| 362 | */ |
||
| 363 | public function getContainer() |
||
| 367 | |||
| 368 | public function setAdminGroups(array $adminGroups) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | public function getAdminGroups() |
||
| 380 | |||
| 381 | public function setAdminServiceIds(array $adminServiceIds) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return array |
||
| 388 | */ |
||
| 389 | public function getAdminServiceIds() |
||
| 393 | |||
| 394 | public function setAdminClasses(array $adminClasses) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | public function getAdminClasses() |
||
| 406 | |||
| 407 | final public function setTemplateRegistry(MutableTemplateRegistryInterface $templateRegistry) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @deprecated since 3.34, will be dropped in 4.0. Use TemplateRegistry "sonata.admin.global_template_registry" instead |
||
| 414 | */ |
||
| 415 | public function setTemplates(array $templates) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @deprecated since 3.34, will be dropped in 4.0. Use TemplateRegistry "sonata.admin.global_template_registry" instead |
||
| 425 | * |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | public function getTemplates() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @deprecated since 3.34, will be dropped in 4.0. Use TemplateRegistry "sonata.admin.global_template_registry" instead |
||
| 435 | * |
||
| 436 | * @param string $name |
||
| 437 | * |
||
| 438 | * @return string|null |
||
| 439 | */ |
||
| 440 | public function getTemplate($name) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | public function getTitleLogo() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @return string |
||
| 455 | */ |
||
| 456 | public function getTitle() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @param string $name |
||
| 463 | * @param mixed $default |
||
| 464 | * |
||
| 465 | * @return mixed |
||
| 466 | */ |
||
| 467 | public function getOption($name, $default = null) |
||
| 475 | |||
| 476 | public function getPropertyAccessor() |
||
| 484 | } |
||
| 485 |
If you suppress an error, we recommend checking for the error condition explicitly: