Complex classes like DoctrineORMMapper 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 DoctrineORMMapper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class DoctrineORMMapper implements EventSubscriber |
||
22 | { |
||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $associations; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $discriminators; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $discriminatorColumns; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $inheritanceTypes; |
||
42 | |||
43 | /** |
||
44 | * @var ManagerRegistry |
||
45 | */ |
||
46 | protected $doctrine; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $indexes; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $uniques; |
||
57 | |||
58 | /** |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $overrides; |
||
62 | |||
63 | public function __construct(ManagerRegistry $doctrine, array $associations = [], array $indexes = [], array $discriminators = [], array $discriminatorColumns = [], array $inheritanceTypes = [], array $uniques = [], array $overrides = []) |
||
74 | |||
75 | public function getSubscribedEvents(): array |
||
81 | |||
82 | public function addAssociation(string $class, string $field, array $options): void |
||
90 | |||
91 | /** |
||
92 | * Add a discriminator to a class. |
||
93 | * |
||
94 | * @param string $class The Class |
||
95 | * @param string $key Key is the database value and values are the classes |
||
96 | * @param string $discriminatorClass The mapped class |
||
97 | */ |
||
98 | public function addDiscriminator(string $class, string $key, string $discriminatorClass): void |
||
108 | |||
109 | public function addDiscriminatorColumn(string $class, array $columnDef): void |
||
115 | |||
116 | public function addInheritanceType(string $class, string $type): void |
||
122 | |||
123 | public function addIndex(string $class, string $name, array $columns): void |
||
135 | |||
136 | public function addUnique(string $class, string $name, array $columns): void |
||
148 | |||
149 | /** |
||
150 | * Adds new ORM override. |
||
151 | */ |
||
152 | final public function addOverride(string $class, string $type, array $options): void |
||
160 | |||
161 | public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void |
||
174 | |||
175 | /** |
||
176 | * @throws \RuntimeException |
||
177 | */ |
||
178 | private function loadAssociations(ClassMetadataInfo $metadata): void |
||
203 | |||
204 | /** |
||
205 | * @throws \RuntimeException |
||
206 | */ |
||
207 | private function loadDiscriminatorColumns(ClassMetadataInfo $metadata): void |
||
232 | |||
233 | /** |
||
234 | * @throws \RuntimeException |
||
235 | */ |
||
236 | private function loadInheritanceTypes(ClassMetadataInfo $metadata): void |
||
254 | |||
255 | /** |
||
256 | * @throws \RuntimeException |
||
257 | */ |
||
258 | private function loadDiscriminators(ClassMetadataInfo $metadata): void |
||
279 | |||
280 | private function loadIndexes(ClassMetadataInfo $metadata): void |
||
290 | |||
291 | private function loadUniques(ClassMetadataInfo $metadata): void |
||
301 | |||
302 | /** |
||
303 | * @throws \RuntimeException |
||
304 | */ |
||
305 | private function loadOverrides(ClassMetadataInfo $metadata): void |
||
325 | } |
||
326 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.