Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SchemaBuilder 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 SchemaBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class SchemaBuilder extends Component |
||
27 | { |
||
28 | /** |
||
29 | * @var DocumentSchema[] |
||
30 | */ |
||
31 | private $documents = []; |
||
32 | |||
33 | /** |
||
34 | * @var CollectionSchema[] |
||
35 | */ |
||
36 | private $collections = []; |
||
37 | |||
38 | /** |
||
39 | * @var ODMConfig |
||
40 | */ |
||
41 | protected $config = null; |
||
42 | |||
43 | /** |
||
44 | * @invisible |
||
45 | * @var ODM |
||
46 | */ |
||
47 | protected $odm = null; |
||
48 | |||
49 | /** |
||
50 | * @param ODM $odm |
||
51 | * @param ODMConfig $config |
||
52 | * @param ClassLocatorInterface $locator |
||
53 | */ |
||
54 | public function __construct(ODM $odm, ODMConfig $config, ClassLocatorInterface $locator) |
||
62 | |||
63 | /** |
||
64 | * @return ODM |
||
65 | */ |
||
66 | public function odm() |
||
70 | |||
71 | /** |
||
72 | * Resolve database alias. |
||
73 | * |
||
74 | * @param string $database |
||
75 | * @return string |
||
76 | */ |
||
77 | public function databaseAlias($database) |
||
86 | |||
87 | /** |
||
88 | * Check if Document class known to schema builder. |
||
89 | * |
||
90 | * @param string $class |
||
91 | * @return bool |
||
92 | */ |
||
93 | public function hasDocument($class) |
||
97 | |||
98 | /** |
||
99 | * Instance of DocumentSchema associated with given class name. |
||
100 | * |
||
101 | * @param string $class |
||
102 | * @return DocumentSchema |
||
103 | * @throws SchemaException |
||
104 | */ |
||
105 | public function document($class) |
||
118 | |||
119 | /** |
||
120 | * @return DocumentSchema[] |
||
121 | */ |
||
122 | public function getDocuments() |
||
126 | |||
127 | /** |
||
128 | * @return CollectionSchema[] |
||
129 | */ |
||
130 | public function getCollections() |
||
134 | |||
135 | /** |
||
136 | * Create every requested collection index. |
||
137 | * |
||
138 | * @throws \MongoException |
||
139 | */ |
||
140 | public function createIndexes() |
||
164 | |||
165 | /** |
||
166 | * Normalize document schema in lighter structure to be saved in ODM component memory. |
||
167 | * |
||
168 | * @return array |
||
169 | * @throws SchemaException |
||
170 | * @throws DefinitionException |
||
171 | */ |
||
172 | public function normalizeSchema() |
||
211 | |||
212 | /** |
||
213 | * Get all mutators associated with field type. |
||
214 | * |
||
215 | * @param string $type Field type. |
||
216 | * @return array |
||
217 | */ |
||
218 | public function getMutators($type) |
||
222 | |||
223 | /** |
||
224 | * Get mutator alias if presented. Aliases used to simplify schema definition. |
||
225 | * |
||
226 | * @param string $alias |
||
227 | * @return string|array |
||
228 | */ |
||
229 | public function mutatorAlias($alias) |
||
233 | |||
234 | /** |
||
235 | * Locate every available Document class. |
||
236 | * |
||
237 | * @param ClassLocatorInterface $locator |
||
238 | * @return $this |
||
239 | */ |
||
240 | protected function locateDocuments(ClassLocatorInterface $locator) |
||
252 | |||
253 | /** |
||
254 | * Locate ORM entities sources. |
||
255 | * |
||
256 | * @param ClassLocatorInterface $locator |
||
257 | * @return $this |
||
258 | */ |
||
259 | View Code Duplication | protected function locateSources(ClassLocatorInterface $locator) |
|
279 | |||
280 | /** |
||
281 | * Create instances of CollectionSchema associated with found DocumentSchema instances. |
||
282 | * |
||
283 | * @throws SchemaException |
||
284 | */ |
||
285 | protected function describeCollections() |
||
305 | |||
306 | /** |
||
307 | * Pack (normalize) class definition. |
||
308 | * |
||
309 | * @param mixed $definition |
||
310 | * @return array|string |
||
311 | */ |
||
312 | private function packDefinition($definition) |
||
324 | |||
325 | /** |
||
326 | * Pack (normalize) document aggregations. |
||
327 | * |
||
328 | * @param array $aggregations |
||
329 | * @return array |
||
330 | */ |
||
331 | private function packAggregations(array $aggregations) |
||
344 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.