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 PropelORMFieldGuesser 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 PropelORMFieldGuesser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class PropelORMFieldGuesser |
||
10 | { |
||
11 | /** |
||
12 | * @var boolean |
||
13 | */ |
||
14 | private $guessRequired; |
||
15 | |||
16 | /** |
||
17 | * @var boolean |
||
18 | */ |
||
19 | private $defaultRequired; |
||
20 | |||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | private $cache = array(); |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | private $formTypes; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private $filterTypes; |
||
35 | |||
36 | public function __construct(array $formTypes, array $filterTypes, $guessRequired, $defaultRequired) |
||
43 | |||
44 | /** |
||
45 | * @param $class |
||
46 | * @return mixed |
||
47 | */ |
||
48 | protected function getMetadatas($class) |
||
52 | |||
53 | /** |
||
54 | * @param $class |
||
55 | * @return array |
||
56 | */ |
||
57 | public function getAllFields($class) |
||
67 | |||
68 | public function getManyToMany($model, $fieldPath) |
||
79 | |||
80 | /** |
||
81 | * Find out the database type for given model field path. |
||
82 | * |
||
83 | * @param string $model The starting model. |
||
84 | * @param string $fieldPath The field path. |
||
85 | * @return string The leaf field's primary key. |
||
86 | */ |
||
87 | public function getDbType($model, $fieldPath) |
||
103 | |||
104 | /** |
||
105 | * @param $fieldName |
||
106 | * @param $class |
||
107 | * @return object|false The relation object or false. |
||
108 | */ |
||
109 | protected function getRelation($fieldName, $class) |
||
122 | |||
123 | /** |
||
124 | * @param $class |
||
125 | * @param $fieldName |
||
126 | * @return string|void |
||
127 | */ |
||
128 | public function getPhpName($class, $fieldName) |
||
136 | |||
137 | /** |
||
138 | * @param $dbType |
||
139 | * @return string |
||
140 | */ |
||
141 | public function getSortType($dbType) |
||
174 | |||
175 | /** |
||
176 | * @param $dbType |
||
177 | * @param $class: for debug only |
||
178 | * @param $columnName: for debug only |
||
179 | * @return string |
||
180 | */ |
||
181 | View Code Duplication | public function getFormType($dbType, $class, $columnName) |
|
208 | |||
209 | /** |
||
210 | * @param $dbType |
||
211 | * @param $columnName |
||
212 | * @return string |
||
213 | */ |
||
214 | View Code Duplication | public function getFilterType($dbType, $class, $columnName) |
|
241 | |||
242 | /** |
||
243 | * @param $formType |
||
244 | * @param $dbType |
||
245 | * @param $model |
||
246 | * @param $fieldPath |
||
247 | * @return array |
||
248 | */ |
||
249 | public function getFormOptions($formType, $dbType, $model, $fieldPath) |
||
253 | |||
254 | /** |
||
255 | * @param $filterType |
||
256 | * @param $dbType |
||
257 | * @param $model |
||
258 | * @param $fieldPath |
||
259 | * @return array |
||
260 | */ |
||
261 | public function getFilterOptions($filterType, $dbType, $model, $fieldPath) |
||
265 | |||
266 | /** |
||
267 | * @param $type |
||
268 | * @param $dbType |
||
269 | * @param $model |
||
270 | * @param $fieldPath |
||
271 | * @param bool $filter |
||
272 | * @return array |
||
273 | */ |
||
274 | protected function getOptions($type, $dbType, $model, $fieldPath, $filter = false) |
||
359 | |||
360 | protected function isRequired($class, $fieldName) |
||
370 | |||
371 | /** |
||
372 | * Find the pk name |
||
373 | */ |
||
374 | public function getModelPrimaryKeyName($class) |
||
384 | |||
385 | protected function getTable($class) |
||
399 | |||
400 | protected function getColumn($class, $property) |
||
419 | |||
420 | /** |
||
421 | * Find out the primary key for given model field path. |
||
422 | * |
||
423 | * @param string $model The starting model. |
||
424 | * @param string $fieldPath The field path. |
||
425 | * @return string The leaf field's primary key. |
||
426 | */ |
||
427 | public function getPrimaryKeyFor($model, $fieldPath) |
||
442 | |||
443 | /** |
||
444 | * Resolve field path for given model to class and field name. |
||
445 | * |
||
446 | * @param string $model The starting model. |
||
447 | * @param string $fieldPath The field path. |
||
448 | * @return array An array containing field and class information. |
||
449 | */ |
||
450 | private function resolveRelatedField($model, $fieldPath) |
||
469 | } |
||
470 |
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.