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 |
||
8 | abstract class CssPreprocessor extends BaseTask |
||
9 | { |
||
10 | const FORMAT_NAME = ''; |
||
11 | |||
12 | /** |
||
13 | * Default compiler to use. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $compiler; |
||
18 | |||
19 | /** |
||
20 | * Available compilers list |
||
21 | * |
||
22 | * @var string[] |
||
23 | */ |
||
24 | protected $compilers = []; |
||
25 | |||
26 | /** |
||
27 | * Compiler options. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $compilerOptions = []; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $files = []; |
||
37 | |||
38 | /** |
||
39 | * Constructor. Accepts array of file paths. |
||
40 | * |
||
41 | * @param array $input |
||
42 | */ |
||
43 | public function __construct(array $input) |
||
49 | |||
50 | protected function setDefaultCompiler() |
||
57 | |||
58 | /** |
||
59 | * Sets import directories |
||
60 | * Alias for setImportPaths |
||
61 | * @see CssPreprocessor::setImportPaths |
||
62 | * |
||
63 | * @param array|string $dirs |
||
64 | * |
||
65 | * @return $this |
||
66 | */ |
||
67 | public function importDir($dirs) |
||
71 | |||
72 | /** |
||
73 | * Adds import directory |
||
74 | * |
||
75 | * @param string $dir |
||
76 | * |
||
77 | * @return $this |
||
78 | */ |
||
79 | public function addImportPath($dir) |
||
91 | |||
92 | /** |
||
93 | * Sets import directories |
||
94 | * |
||
95 | * @param array|string $dirs |
||
96 | * |
||
97 | * @return $this |
||
98 | */ |
||
99 | public function setImportPaths($dirs) |
||
109 | |||
110 | /** |
||
111 | * @param string $formatterName |
||
112 | * |
||
113 | * @return $this |
||
114 | */ |
||
115 | public function setFormatter($formatterName) |
||
121 | |||
122 | /** |
||
123 | * Sets the compiler. |
||
124 | * |
||
125 | * @param string $compiler |
||
126 | * @param array $options |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function compiler($compiler, array $options = []) |
||
137 | |||
138 | /** |
||
139 | * Compiles file |
||
140 | * |
||
141 | * @param $file |
||
142 | * |
||
143 | * @return bool|mixed |
||
144 | */ |
||
145 | protected function compile($file) |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | public function run() |
||
214 | } |
||
215 |
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.