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 PrepareData 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 PrepareData, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class PrepareData |
||
9 | { |
||
10 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | private $columns = []; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $data = []; |
||
19 | |||
20 | /** |
||
21 | * @var array null |
||
22 | */ |
||
23 | private $dataPrepared; |
||
24 | |||
25 | private $rendererName; |
||
26 | |||
27 | /** |
||
28 | * @var Translator |
||
29 | */ |
||
30 | private $translator; |
||
31 | |||
32 | /** |
||
33 | * @var \Zend\Router\RouteStackInterface |
||
34 | */ |
||
35 | private $router; |
||
36 | |||
37 | /** |
||
38 | * @param array $data |
||
39 | * @param array $columns |
||
40 | */ |
||
41 | public function __construct(array $data, array $columns) |
||
42 | { |
||
43 | $this->setData($data); |
||
44 | $this->setColumns($columns); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param array $columns |
||
49 | */ |
||
50 | public function setColumns(array $columns) |
||
51 | { |
||
52 | $this->columns = $columns; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @return array |
||
57 | */ |
||
58 | public function getColumns() |
||
62 | |||
63 | /** |
||
64 | * @param array $data |
||
65 | */ |
||
66 | public function setData(array $data) |
||
70 | |||
71 | /** |
||
72 | * @param bool $raw |
||
73 | * |
||
74 | * @return array |
||
75 | */ |
||
76 | public function getData($raw = false) |
||
86 | |||
87 | /** |
||
88 | * @param string $name |
||
89 | */ |
||
90 | public function setRendererName($name = null) |
||
94 | |||
95 | /** |
||
96 | * @return string |
||
97 | */ |
||
98 | public function getRendererName() |
||
102 | |||
103 | /** |
||
104 | * @param Translator $translator |
||
105 | * |
||
106 | * @throws \InvalidArgumentException |
||
107 | */ |
||
108 | View Code Duplication | public function setTranslator($translator) |
|
116 | |||
117 | /** |
||
118 | * @return \Zend\I18n\Translator\Translator |
||
119 | */ |
||
120 | public function getTranslator() |
||
124 | |||
125 | /** |
||
126 | * @param \Zend\Router\RouteStackInterface $router |
||
127 | */ |
||
128 | public function setRouter(RouteStackInterface $router) |
||
132 | |||
133 | /** |
||
134 | * @return \Zend\Router\RouteStackInterface |
||
135 | */ |
||
136 | public function getRouter() |
||
140 | |||
141 | /** |
||
142 | * Return true if preparing executed, false if already done! |
||
143 | * |
||
144 | * @throws \Exception |
||
145 | * |
||
146 | * @return bool |
||
147 | */ |
||
148 | public function prepare() |
||
274 | } |
||
275 |
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.