Complex classes like AdminHelper 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 AdminHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 32 | class AdminHelper | ||
| 33 | { | ||
| 34 | /** | ||
| 35 | * @var string | ||
| 36 | */ | ||
| 37 | private const FORM_FIELD_DELETE = '_delete'; | ||
| 38 | |||
| 39 | /** | ||
| 40 | * @var Pool | ||
| 41 | */ | ||
| 42 | protected $pool; | ||
| 43 | |||
| 44 | public function __construct(Pool $pool) | ||
| 48 | |||
| 49 | /** | ||
| 50 | * @param string $elementId | ||
| 51 | * | ||
| 52 | * @throws \RuntimeException | ||
| 53 | * | ||
| 54 | * @return FormBuilderInterface|null | ||
| 55 | */ | ||
| 56 | public function getChildFormBuilder(FormBuilderInterface $formBuilder, $elementId) | ||
| 66 | |||
| 67 | /** | ||
| 68 | * @param string $elementId | ||
| 69 | * | ||
| 70 | * @return FormView|null | ||
| 71 | */ | ||
| 72 | public function getChildFormView(FormView $formView, $elementId) | ||
| 82 | |||
| 83 | /** | ||
| 84 | * NEXT_MAJOR: remove this method. | ||
| 85 | * | ||
| 86 | * @deprecated | ||
| 87 | * | ||
| 88 | * @param string $code | ||
| 89 | * | ||
| 90 | * @return AdminInterface | ||
| 91 | */ | ||
| 92 | public function getAdmin($code) | ||
| 96 | |||
| 97 | /** | ||
| 98 | * Note: | ||
| 99 | * This code is ugly, but there is no better way of doing it. | ||
| 100 | * | ||
| 101 | * @param object $subject | ||
| 102 | * @param string $elementId | ||
| 103 | * | ||
| 104 | * @throws \RuntimeException | ||
| 105 | * @throws \Exception | ||
| 106 | * | ||
| 107 | * @return array | ||
| 108 | */ | ||
| 109 | public function appendFormFieldElement(AdminInterface $admin, $subject, $elementId) | ||
| 220 | |||
| 221 | /** | ||
| 222 | * Add a new instance to the related FieldDescriptionInterface value. | ||
| 223 | * | ||
| 224 | * @param object $object | ||
| 225 | * | ||
| 226 | * @throws \RuntimeException | ||
| 227 | */ | ||
| 228 | public function addNewInstance($object, FieldDescriptionInterface $fieldDescription): void | ||
| 229 |     { | ||
| 230 | $instance = $fieldDescription->getAssociationAdmin()->getNewInstance(); | ||
| 231 | $mapping = $fieldDescription->getAssociationMapping(); | ||
| 232 | $parentMappings = $fieldDescription->getParentAssociationMappings(); | ||
| 233 | |||
| 234 |         foreach ($parentMappings as $parentMapping) { | ||
| 235 |             $method = sprintf('get%s', Inflector::classify($parentMapping['fieldName'])); | ||
| 236 | |||
| 237 |             if (!(\is_callable([$object, $method]) && method_exists($object, $method))) { | ||
| 238 | /* | ||
| 239 | * NEXT_MAJOR: Use BadMethodCallException instead | ||
| 240 | */ | ||
| 241 | throw new \RuntimeException( | ||
| 242 |                     sprintf('Method %s::%s() does not exist.', ClassUtils::getClass($object), $method) | ||
| 243 | ); | ||
| 244 | } | ||
| 245 | |||
| 246 | $object = $object->$method(); | ||
| 247 | } | ||
| 248 | |||
| 249 |         $method = sprintf('add%s', Inflector::classify($mapping['fieldName'])); | ||
| 250 | |||
| 251 |         if (!(\is_callable([$object, $method]) && method_exists($object, $method))) { | ||
| 252 | $method = rtrim($method, 's'); | ||
| 253 | |||
| 254 |             if (!(\is_callable([$object, $method]) && method_exists($object, $method))) { | ||
| 255 |                 $method = sprintf('add%s', Inflector::classify(Inflector::singularize($mapping['fieldName']))); | ||
| 256 | |||
| 257 |                 if (!(\is_callable([$object, $method]) && method_exists($object, $method))) { | ||
| 258 | /* | ||
| 259 | * NEXT_MAJOR: Use BadMethodCallException instead | ||
| 260 | */ | ||
| 261 | throw new \RuntimeException( | ||
| 262 |                         sprintf('Method %s::%s() does not exist.', ClassUtils::getClass($object), $method) | ||
| 263 | ); | ||
| 264 | } | ||
| 265 | } | ||
| 266 | } | ||
| 267 | |||
| 268 | $object->$method($instance); | ||
| 269 | } | ||
| 270 | |||
| 271 | /** | ||
| 272 | * Get access path to element which works with PropertyAccessor. | ||
| 273 | * | ||
| 274 | * @param string $elementId expects string in format used in form id field. | ||
| 275 | * (uniqueIdentifier_model_sub_model or uniqueIdentifier_model_1_sub_model etc.) | ||
| 276 | * @param mixed $entity | ||
| 277 | * | ||
| 278 | * @throws \Exception | ||
| 279 | * | ||
| 280 | * @return string | ||
| 281 | */ | ||
| 282 | public function getElementAccessPath($elementId, $entity) | ||
| 311 | |||
| 312 | /** | ||
| 313 | * Recursively find the class name of the admin responsible for the element at the end of an association chain. | ||
| 314 | * | ||
| 315 | * @param array $elements | ||
| 316 | * | ||
| 317 | * @return string | ||
| 318 | */ | ||
| 319 | protected function getEntityClassName(AdminInterface $admin, $elements) | ||
| 329 | } | ||
| 330 | 
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.