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 Summary implements Report |
||
16 | { |
||
17 | |||
18 | |||
19 | /** |
||
20 | * Generate a partial report for a single processed file. |
||
21 | * |
||
22 | * Function should return TRUE if it printed or stored data about the file |
||
23 | * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
||
24 | * its data should be counted in the grand totals. |
||
25 | * |
||
26 | * @param array $report Prepared report data. |
||
27 | * @param \Symplify\PHP7_CodeSniffer\File $phpcsFile The file being reported on. |
||
28 | * @param bool $showSources Show sources? |
||
29 | * @param int $width Maximum allowed line width. |
||
30 | * |
||
31 | * @return bool |
||
32 | */ |
||
33 | public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
||
47 | |||
48 | |||
49 | /** |
||
50 | * Generates a summary of errors and warnings for each file processed. |
||
51 | * |
||
52 | * @param string $cachedData Any partial report data that was returned from |
||
53 | * generateFileReport during the run. |
||
54 | * @param int $totalFiles Total number of files processed during the run. |
||
55 | * @param int $totalErrors Total number of errors found during the run. |
||
56 | * @param int $totalWarnings Total number of warnings found during the run. |
||
57 | * @param int $totalFixable Total number of problems that can be fixed. |
||
58 | * @param bool $showSources Show sources? |
||
59 | * @param int $width Maximum allowed line width. |
||
60 | * @param bool $interactive Are we running in interactive mode? |
||
61 | * @param bool $toScreen Is the report being printed to screen? |
||
62 | * |
||
63 | * @return void |
||
64 | */ |
||
65 | public function generate( |
||
160 | |||
161 | |||
162 | }//end class |
||
163 |
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.