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 |
||
16 | class Fop |
||
17 | { |
||
18 | const OTUPUT_PDF = 'application/pdf'; |
||
19 | const OTUPUT_RTF = 'text/rtf'; |
||
20 | |||
21 | protected $fopExecutable; |
||
22 | protected $javaExecutable; |
||
23 | protected $configurationFile; |
||
24 | public function __construct($fopExecutable) |
||
28 | /** |
||
29 | * Convert to PDF reading a FOP input and save the result into file |
||
30 | * @param InputInterface|string $source |
||
31 | * @param string $xsl |
||
32 | * @throws \RuntimeException |
||
33 | * @deprecated |
||
34 | */ |
||
35 | public function convertToPdf($source, $destination, $xsl = null) |
||
39 | /** |
||
40 | * Convert to RTF reading a FOP input and save the result into file |
||
41 | * @param InputInterface|string $source |
||
42 | * @param string $xsl |
||
43 | * @throws \RuntimeException |
||
44 | * @deprecated |
||
45 | */ |
||
46 | public function convertToRtf($source, $destination, $xsl = null) |
||
50 | /** |
||
51 | * Convert reading a FOP input, saving the result into file |
||
52 | * @param InputInterface|string $source |
||
53 | * @param string $destination |
||
54 | * @param string|const $outputFormat self::OTUPUT_PDF or self::OTUPUT_RTF or other supported mimes by Apache FOP |
||
55 | * @param InputInterface|string $xsl |
||
56 | * @param array $params |
||
57 | * @throws \RuntimeException |
||
58 | */ |
||
59 | public function convert($source, $destination, $outputFormat, $xsl = null, array $params = array()) |
||
77 | |||
78 | /** |
||
79 | * Convert reading a FOP input, and get the result |
||
80 | * @param InputInterface $source |
||
81 | * @param string $outputFormat self::OTUPUT_PDF or self::OTUPUT_RTF or other supported mimes by Apache FOP |
||
82 | * @param InputInterface|string $xsl |
||
83 | * @param array $params |
||
84 | * @return string |
||
85 | * @throws \RuntimeException |
||
86 | */ |
||
87 | public function get(InputInterface $source, $outputFormat, $xsl = null, array $params = array()) |
||
101 | /** |
||
102 | * Convert reading a FOP input, and flush to output the result |
||
103 | * @param InputInterface $source |
||
104 | * @param string $outputFormat self::OTUPUT_PDF or self::OTUPUT_RTF or other supported mimes by Apache FOP |
||
105 | * @param InputInterface|string $xsl |
||
106 | * @param array $params |
||
107 | * @throws \RuntimeException |
||
108 | */ |
||
109 | View Code Duplication | public function out(InputInterface $source, $outputFormat, $xsl = null, array $params = array()) |
|
127 | /** |
||
128 | * Convert reading a FOP input, and flush to output the result |
||
129 | * @param InputInterface $source |
||
130 | * @param callback $callback will recieve the output |
||
131 | * @param string $outputFormat self::OTUPUT_PDF or self::OTUPUT_RTF or other supported mimes by Apache FOP |
||
132 | * @param string $xsl |
||
133 | * @param array $params |
||
134 | * @throws \RuntimeException |
||
135 | */ |
||
136 | View Code Duplication | public function callback(InputInterface $source, $callback, $outputFormat, $xsl = null, array $params = array()) |
|
154 | |||
155 | /** |
||
156 | * |
||
157 | * |
||
158 | * @param InputInterface $input Ony $input can use StringInput |
||
159 | * @param string $destination The place where save the result |
||
160 | * @param string $outputFormat |
||
161 | * @param InputInterface $xsl |
||
162 | * @param array $params |
||
163 | * @return \Symfony\Component\Process\Process |
||
164 | */ |
||
165 | protected function runProcess(InputInterface $input, $destination, $outputFormat, InputInterface $xsl = null, array $params = array()) |
||
209 | /** |
||
210 | * Get the path for FOP executable |
||
211 | * @return string |
||
212 | */ |
||
213 | public function getFopExecutable() |
||
217 | /** |
||
218 | * Get the path of FOP config file |
||
219 | * @return string |
||
220 | */ |
||
221 | public function getConfigurationFile() |
||
225 | /** |
||
226 | * SET the path for FOP executable |
||
227 | * @return self |
||
228 | */ |
||
229 | public function setFopExecutable($fopExecutable) |
||
238 | /** |
||
239 | * Set the path of FOP config file |
||
240 | * @param string $configurationFile |
||
241 | * @return self |
||
242 | */ |
||
243 | public function setConfigurationFile($configurationFile) |
||
252 | /** |
||
253 | * Get the path for Java executable |
||
254 | * @return string |
||
255 | */ |
||
256 | public function getJavaExecutable() |
||
260 | /** |
||
261 | * SET the path for Java executable |
||
262 | * @param string $javaExecutable |
||
263 | * @return self |
||
264 | */ |
||
265 | public function setJavaExecutable($javaExecutable) |
||
271 | } |
||
272 |
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.