Complex classes like FormMapper 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 FormMapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 30 | class FormMapper extends BaseGroupedMapper  | 
            ||
| 31 | { | 
            ||
| 32 | /**  | 
            ||
| 33 | * @var FormBuilderInterface  | 
            ||
| 34 | */  | 
            ||
| 35 | protected $formBuilder;  | 
            ||
| 36 | |||
| 37 | /**  | 
            ||
| 38 | * @var FormContractorInterface  | 
            ||
| 39 | */  | 
            ||
| 40 | protected $builder;  | 
            ||
| 41 | |||
| 42 | public function __construct(  | 
            ||
| 43 | FormContractorInterface $formContractor,  | 
            ||
| 44 | FormBuilderInterface $formBuilder,  | 
            ||
| 45 | AdminInterface $admin  | 
            ||
| 46 |     ) { | 
            ||
| 47 | parent::__construct($formContractor, $admin);  | 
            ||
| 48 | $this->formBuilder = $formBuilder;  | 
            ||
| 49 | }  | 
            ||
| 50 | |||
| 51 | public function reorder(array $keys): self  | 
            ||
| 52 |     { | 
            ||
| 53 | $this->admin->reorderFormGroup($this->getCurrentGroupName(), $keys);  | 
            ||
| 54 | |||
| 55 | return $this;  | 
            ||
| 56 | }  | 
            ||
| 57 | |||
| 58 | /**  | 
            ||
| 59 | * @param FormBuilderInterface|string $name  | 
            ||
| 60 | */  | 
            ||
| 61 | public function add($name, ?string $type = null, array $options = [], array $fieldDescriptionOptions = []): self  | 
            ||
| 62 |     { | 
            ||
| 63 |         if (!$this->shouldApply()) { | 
            ||
| 64 | return $this;  | 
            ||
| 65 | }  | 
            ||
| 66 | |||
| 67 |         if ($name instanceof FormBuilderInterface) { | 
            ||
| 68 | $fieldName = $name->getName();  | 
            ||
| 69 |         } else { | 
            ||
| 70 | $fieldName = $name;  | 
            ||
| 71 | }  | 
            ||
| 72 | |||
| 73 | // "Dot" notation is not allowed as form name, but can be used as property path to access nested data.  | 
            ||
| 74 |         if (!$name instanceof FormBuilderInterface && !isset($options['property_path'])) { | 
            ||
| 75 | $options['property_path'] = $fieldName;  | 
            ||
| 76 | |||
| 77 | // fix the form name  | 
            ||
| 78 | $fieldName = $this->sanitizeFieldName($fieldName);  | 
            ||
| 79 | }  | 
            ||
| 80 | |||
| 81 | // change `collection` to `sonata_type_native_collection` form type to  | 
            ||
| 82 | // avoid BC break problems  | 
            ||
| 83 |         if ('collection' === $type || SymfonyCollectionType::class === $type) { | 
            ||
| 84 | $type = CollectionType::class;  | 
            ||
| 85 | }  | 
            ||
| 86 | |||
| 87 | $label = $fieldName;  | 
            ||
| 88 | |||
| 89 | $group = $this->addFieldToCurrentGroup($label);  | 
            ||
| 90 | |||
| 91 | // Try to autodetect type  | 
            ||
| 92 |         if ($name instanceof FormBuilderInterface && null === $type) { | 
            ||
| 93 | $fieldDescriptionOptions['type'] = \get_class($name->getType()->getInnerType());  | 
            ||
| 94 | }  | 
            ||
| 95 | |||
| 96 |         if (!isset($fieldDescriptionOptions['type']) && \is_string($type)) { | 
            ||
| 97 | $fieldDescriptionOptions['type'] = $type;  | 
            ||
| 98 | }  | 
            ||
| 99 | |||
| 100 |         if ($group['translation_domain'] && !isset($fieldDescriptionOptions['translation_domain'])) { | 
            ||
| 101 | $fieldDescriptionOptions['translation_domain'] = $group['translation_domain'];  | 
            ||
| 102 | }  | 
            ||
| 103 | |||
| 104 | $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(  | 
            ||
| 105 | $this->admin->getClass(),  | 
            ||
| 106 | $name instanceof FormBuilderInterface ? $name->getName() : $name,  | 
            ||
| 107 | $fieldDescriptionOptions  | 
            ||
| 108 | );  | 
            ||
| 109 | |||
| 110 | // Note that the builder var is actually the formContractor:  | 
            ||
| 111 | $this->builder->fixFieldDescription($this->admin, $fieldDescription);  | 
            ||
| 112 | |||
| 113 |         if ($fieldName !== $name) { | 
            ||
| 114 | $fieldDescription->setName($fieldName);  | 
            ||
| 115 | }  | 
            ||
| 116 | |||
| 117 |         if ($name instanceof FormBuilderInterface) { | 
            ||
| 118 | $type = null;  | 
            ||
| 119 | $options = [];  | 
            ||
| 120 |         } else { | 
            ||
| 121 | $name = $fieldDescription->getName();  | 
            ||
| 122 | |||
| 123 | // Note that the builder var is actually the formContractor:  | 
            ||
| 124 | $options = array_replace_recursive($this->builder->getDefaultOptions($type, $fieldDescription) ?? [], $options);  | 
            ||
| 125 | |||
| 126 | // be compatible with mopa if not installed, avoid generating an exception for invalid option  | 
            ||
| 127 | // force the default to false ...  | 
            ||
| 128 |             if (!isset($options['label_render'])) { | 
            ||
| 129 | $options['label_render'] = false;  | 
            ||
| 130 | }  | 
            ||
| 131 | |||
| 132 |             if (!isset($options['label'])) { | 
            ||
| 133 | $options['label'] = $this->admin->getLabelTranslatorStrategy()->getLabel($name, 'form', 'label');  | 
            ||
| 134 | }  | 
            ||
| 135 | }  | 
            ||
| 136 | |||
| 137 | $this->admin->addFormFieldDescription($fieldName, $fieldDescription);  | 
            ||
| 138 | |||
| 139 |         if (!isset($fieldDescriptionOptions['role']) || $this->admin->isGranted($fieldDescriptionOptions['role'])) { | 
            ||
| 140 | $this->formBuilder->add($name, $type, $options);  | 
            ||
| 141 | }  | 
            ||
| 142 | |||
| 143 | return $this;  | 
            ||
| 144 | }  | 
            ||
| 145 | |||
| 146 | public function get(string $name): FormBuilderInterface  | 
            ||
| 152 | |||
| 153 | public function has(string $key): bool  | 
            ||
| 159 | |||
| 160 | final public function keys(): array  | 
            ||
| 164 | |||
| 165 | public function remove(string $key): self  | 
            ||
| 174 | |||
| 175 | /**  | 
            ||
| 176 | * Removes a group.  | 
            ||
| 177 | *  | 
            ||
| 178 | * @param string $group The group to delete  | 
            ||
| 179 | * @param string $tab The tab the group belongs to, defaults to 'default'  | 
            ||
| 180 | * @param bool $deleteEmptyTab Whether or not the Tab should be deleted, when the deleted group leaves the tab empty after deletion  | 
            ||
| 181 | */  | 
            ||
| 182 | public function removeGroup(string $group, string $tab = 'default', bool $deleteEmptyTab = false): self  | 
            ||
| 213 | |||
| 214 | public function getFormBuilder(): FormBuilderInterface  | 
            ||
| 218 | |||
| 219 | public function create(string $name, ?string $type = null, array $options = []): FormBuilderInterface  | 
            ||
| 223 | |||
| 224 | /**  | 
            ||
| 225 | * Symfony default form class sadly can't handle  | 
            ||
| 226 | * form element with dots in its name (when data  | 
            ||
| 227 | * get bound, the default dataMapper is a PropertyPathMapper).  | 
            ||
| 228 | * So use this trick to avoid any issue.  | 
            ||
| 229 | */  | 
            ||
| 230 | protected function sanitizeFieldName(string $fieldName): string  | 
            ||
| 234 | |||
| 235 | protected function getGroups(): array  | 
            ||
| 239 | |||
| 240 | protected function setGroups(array $groups): void  | 
            ||
| 244 | |||
| 245 | protected function getTabs(): array  | 
            ||
| 249 | |||
| 250 | protected function setTabs(array $tabs): void  | 
            ||
| 254 | |||
| 255 | protected function getName(): string  | 
            ||
| 259 | }  | 
            ||
| 260 |