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 |
||
12 | class StyleChooserController implements ContainerInjectableInterface |
||
13 | { |
||
14 | use ContainerInjectableTrait; |
||
15 | |||
16 | |||
17 | |||
18 | /** |
||
19 | * @var string $cssUrl The baseurl to where the css files are. |
||
20 | * @var string $cssDir The path to the directory storing css files. |
||
21 | * @var array $styles The styles available in the style directory. |
||
22 | * @var string $key The session key used to store the active style. |
||
23 | */ |
||
24 | private $cssUrl = "css"; |
||
25 | private $cssDir = ANAX_INSTALL_PATH . "/htdocs/css"; |
||
26 | private $styles = []; |
||
27 | private static $key = "AnaxStyleChooser"; |
||
28 | |||
29 | |||
30 | |||
31 | /** |
||
32 | * Get the session key to use to retrieve the active stylesheet. |
||
33 | * |
||
34 | * @return string |
||
35 | */ |
||
36 | public static function getSessionKey() : string |
||
40 | |||
41 | |||
42 | |||
43 | /** |
||
44 | * The initialize method is optional and will always be called before the |
||
45 | * target method/action. This is a convienient method where you could |
||
46 | * setup internal properties that are commonly used by several methods. |
||
47 | * |
||
48 | * @return void |
||
49 | */ |
||
50 | public function initialize() : void |
||
75 | |||
76 | |||
77 | |||
78 | /** |
||
79 | * Display the stylechooser with details on current selected style. |
||
80 | * |
||
81 | * @return object |
||
82 | */ |
||
83 | public function indexAction() : object |
||
103 | |||
104 | |||
105 | |||
106 | /** |
||
107 | * Update current selected style. |
||
108 | * |
||
109 | * @return object |
||
110 | */ |
||
111 | public function updateActionPost() : object |
||
128 | |||
129 | |||
130 | |||
131 | /** |
||
132 | * Update current selected style using a GET url and redirect to last |
||
133 | * page visited. |
||
134 | * |
||
135 | * @param string $style the key to the style to use. |
||
136 | * |
||
137 | * @return object |
||
138 | */ |
||
139 | public function updateActionGet($style) : object |
||
161 | } |
||
162 |
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.