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 ResourceGenerator 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 ResourceGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class ResourceGenerator |
||
8 | { |
||
9 | /** |
||
10 | * @var Model |
||
11 | */ |
||
12 | protected $model; |
||
13 | |||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $formats = [ |
||
18 | 'form_field' => "\$form->%s('%s', '%s')", |
||
19 | 'show_field' => "\$show->%s('%s')", |
||
20 | 'grid_column' => "\$grid->%s('%s')", |
||
21 | ]; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $doctrineTypeMapping = [ |
||
27 | 'string' => [ |
||
28 | 'enum', 'geometry', 'geometrycollection', 'linestring', |
||
29 | 'polygon', 'multilinestring', 'multipoint', 'multipolygon', |
||
30 | 'point', |
||
31 | ], |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $fieldTypeMapping = [ |
||
38 | 'ip' => 'ip', |
||
39 | 'email' => 'email|mail', |
||
40 | 'password' => 'password|pwd', |
||
41 | 'url' => 'url|link|src|href', |
||
42 | 'mobile' => 'mobile|phone', |
||
43 | 'color' => 'color|rgb', |
||
44 | 'image' => 'image|img|avatar|pic|picture|cover', |
||
45 | 'file' => 'file|attachment', |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * ResourceGenerator constructor. |
||
50 | * |
||
51 | * @param mixed $model |
||
52 | */ |
||
53 | public function __construct($model) |
||
57 | |||
58 | /** |
||
59 | * @param mixed $model |
||
60 | * |
||
61 | * @return mixed |
||
62 | */ |
||
63 | protected function getModel($model) |
||
75 | |||
76 | /** |
||
77 | * @return string |
||
78 | */ |
||
79 | public function generateForm() |
||
168 | |||
169 | View Code Duplication | public function generateShow() |
|
191 | |||
192 | View Code Duplication | public function generateGrid() |
|
211 | |||
212 | protected function getReservedColumns() |
||
221 | |||
222 | /** |
||
223 | * Get columns of a giving model. |
||
224 | * |
||
225 | * @throws \Exception |
||
226 | * |
||
227 | * @return \Doctrine\DBAL\Schema\Column[] |
||
228 | */ |
||
229 | protected function getTableColumns() |
||
257 | |||
258 | /** |
||
259 | * Format label. |
||
260 | * |
||
261 | * @param string $value |
||
262 | * |
||
263 | * @return string |
||
264 | */ |
||
265 | protected function formatLabel($value) |
||
269 | } |
||
270 |