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:
1 | <?php |
||
15 | class Renderer |
||
16 | { |
||
17 | /** @var array Colors for drawing dwh states */ |
||
18 | public static $colors = array( |
||
19 | '#2266aa', |
||
20 | '#228866', |
||
21 | '#775533', |
||
22 | '#333311', |
||
23 | '#881122', |
||
24 | '#662266', |
||
25 | '#00212E', |
||
26 | '#42002E', |
||
27 | '#422100', |
||
28 | '#000B11', |
||
29 | '#5A0011', |
||
30 | '#5A0B00' |
||
31 | ); |
||
32 | |||
33 | /** @var array $eventColors colors for drawing the events */ |
||
34 | protected $eventColors = array( |
||
35 | 'manual' => '#00AA00', // color for manually executable event |
||
36 | 'timeout' => '#AAAAAA', // color for timeout event |
||
37 | 'statewasset' => '#0000AA', // color for other events |
||
38 | 'default' => '#222222', // color for other events |
||
39 | ); |
||
40 | |||
41 | /** @var string $defaultFontColor default font color for state nodes */ |
||
42 | protected $defaultFontColor = ''; |
||
43 | /** @var string $defaultBorderColor default border color for state nodes */ |
||
44 | protected $defaultBorderColor = ''; |
||
45 | |||
46 | /** |
||
47 | * @var array $nodeRenderers contains the renderers for the state nodes. |
||
48 | * |
||
49 | * They can be set by setNodeRendererByStateName(). If no renderer is set the default renderer |
||
50 | * Bob_StateMachine_Renderer_Node_DefaultRenderer will be used. |
||
51 | */ |
||
52 | protected $nodeRenderers = array(); |
||
53 | |||
54 | protected $transitionRenderers = array(); |
||
55 | |||
56 | /** |
||
57 | * @return array |
||
58 | */ |
||
59 | public function getEventColors() |
||
63 | |||
64 | /** |
||
65 | * @param array $eventColors |
||
66 | */ |
||
67 | public function setEventColors($eventColors) |
||
71 | |||
72 | /** |
||
73 | * @return string |
||
74 | */ |
||
75 | public function getDefaultFontColor() |
||
79 | |||
80 | /** |
||
81 | * @param string $defaultFontColor |
||
82 | */ |
||
83 | public function setDefaultFontColor($defaultFontColor) |
||
87 | |||
88 | /** |
||
89 | * @return string |
||
90 | */ |
||
91 | public function getDefaultBorderColor() |
||
95 | |||
96 | /** |
||
97 | * @param string $defaultBorderColor |
||
98 | */ |
||
99 | public function setDefaultBorderColor($defaultBorderColor) |
||
103 | |||
104 | /** |
||
105 | * @return array |
||
106 | */ |
||
107 | public function getNodeRenderers() |
||
111 | |||
112 | /** |
||
113 | * @param array $nodeRenderers |
||
114 | */ |
||
115 | public function setNodeRenderers($nodeRenderers) |
||
119 | |||
120 | /** |
||
121 | * @return array |
||
122 | */ |
||
123 | public function getTransitionRenderers() |
||
127 | |||
128 | /** |
||
129 | * @param array $transitionRenderers |
||
130 | */ |
||
131 | public function setTransitionRenderers($transitionRenderers) |
||
135 | |||
136 | /** |
||
137 | * Is there a state node renderer for the given state name? |
||
138 | * |
||
139 | * @param $stateName |
||
140 | * @return bool |
||
141 | */ |
||
142 | public function hasNodeRendererByStateName($stateName) |
||
146 | |||
147 | /** |
||
148 | * Get the renderer for the given state name. |
||
149 | * |
||
150 | * If no renderer was set the default renderer Bob_StateMachine_Renderer_Node_DefaultRenderer() is returned |
||
151 | * |
||
152 | * @param $stateName |
||
153 | * @return NodeRenderer |
||
154 | */ |
||
155 | public function getNodeRendererByStateName($stateName) |
||
162 | |||
163 | public function getTransitionRendererByStateName($stateName) |
||
170 | |||
171 | /** |
||
172 | * Renders a dot graph into an svg |
||
173 | * |
||
174 | * @param string $dotString The dot graph that is fed into the |
||
175 | * @return string|bool the resulting SVG |
||
176 | */ |
||
177 | public function runDot($dotString) |
||
207 | |||
208 | /** |
||
209 | * Creates a dot graph for a process |
||
210 | * |
||
211 | * @param StateCollection $stateCollection |
||
212 | * @return string A dot graph |
||
213 | */ |
||
214 | public function renderDot(StateCollection $stateCollection) |
||
238 | |||
239 | /** |
||
240 | * @param Client $client |
||
241 | * @param Context $context |
||
242 | * @return string |
||
243 | */ |
||
244 | public static function run(Client $client, Context $context = null) |
||
255 | } |
||
256 |
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.