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 |
||
| 19 | class DoctrineORMMapper implements EventSubscriber |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $associations; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $discriminators; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $discriminatorColumns; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $inheritanceTypes; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var ManagerRegistry |
||
| 43 | */ |
||
| 44 | protected $doctrine; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $indexes; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $uniques; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param ManagerRegistry $doctrine |
||
| 58 | * @param array $associations |
||
| 59 | * @param array $indexes |
||
| 60 | * @param array $discriminators |
||
| 61 | * @param array $discriminatorColumns |
||
| 62 | * @param array $inheritanceTypes |
||
| 63 | * @param array $uniques |
||
| 64 | */ |
||
| 65 | public function __construct(ManagerRegistry $doctrine, array $associations = array(), array $indexes = array(), array $discriminators = array(), array $discriminatorColumns = array(), array $inheritanceTypes = array(), array $uniques = array()) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | public function getSubscribedEvents() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $class |
||
| 88 | * @param string $field |
||
| 89 | * @param array $options |
||
| 90 | */ |
||
| 91 | public function addAssociation($class, $field, array $options) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Add a discriminator to a class. |
||
| 102 | * |
||
| 103 | * @param string $class The Class |
||
| 104 | * @param string $key Key is the database value and values are the classes |
||
| 105 | * @param string $discriminatorClass The mapped class |
||
| 106 | */ |
||
| 107 | public function addDiscriminator($class, $key, $discriminatorClass) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $class |
||
| 120 | * @param array $columnDef |
||
| 121 | */ |
||
| 122 | public function addDiscriminatorColumn($class, array $columnDef) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $class |
||
| 131 | * @param string $type |
||
| 132 | */ |
||
| 133 | public function addInheritanceType($class, $type) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param string $class |
||
| 142 | * @param string $name |
||
| 143 | * @param array $columns |
||
| 144 | */ |
||
| 145 | public function addIndex($class, $name, array $columns) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $class |
||
| 160 | * @param string $name |
||
| 161 | * @param array $columns |
||
| 162 | */ |
||
| 163 | public function addUnique($class, $name, array $columns) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param $eventArgs |
||
| 178 | */ |
||
| 179 | public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param ClassMetadataInfo $metadata |
||
| 194 | * |
||
| 195 | * @throws \RuntimeException |
||
| 196 | */ |
||
| 197 | private function loadAssociations(ClassMetadataInfo $metadata) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param ClassMetadataInfo $metadata |
||
| 222 | * |
||
| 223 | * @throws \RuntimeException |
||
| 224 | */ |
||
| 225 | private function loadDiscriminatorColumns(ClassMetadataInfo $metadata) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param ClassMetadataInfo $metadata |
||
| 246 | * |
||
| 247 | * @throws \RuntimeException |
||
| 248 | */ |
||
| 249 | private function loadInheritanceTypes(ClassMetadataInfo $metadata) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param ClassMetadataInfo $metadata |
||
| 265 | * |
||
| 266 | * @throws \RuntimeException |
||
| 267 | */ |
||
| 268 | private function loadDiscriminators(ClassMetadataInfo $metadata) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param ClassMetadataInfo $metadata |
||
| 288 | */ |
||
| 289 | private function loadIndexes(ClassMetadataInfo $metadata) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param ClassMetadataInfo $metadata |
||
| 302 | */ |
||
| 303 | private function loadUniques(ClassMetadataInfo $metadata) |
||
| 313 | } |
||
| 314 |
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.