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 |
||
14 | class QuotedPrintableDecodeStreamFilter extends php_user_filter |
||
15 | { |
||
16 | /** |
||
17 | * Name used when registering with stream_filter_register. |
||
18 | */ |
||
19 | const STREAM_FILTER_NAME = 'convert.quoted-printable-decode'; |
||
20 | |||
21 | /** |
||
22 | * @var string Leftovers from the last incomplete line that was parsed, to |
||
23 | * be prepended to the next line read. |
||
24 | */ |
||
25 | private $leftover = ''; |
||
26 | |||
27 | /** |
||
28 | * Returns an array of complete lines (including line endings) from the |
||
29 | * passed $bucket object. |
||
30 | * |
||
31 | * If the last line on $bucket is incomplete, it's assigned to |
||
32 | * $this->leftover and prepended to the first element of the first line in |
||
33 | * the next call to getLines. |
||
34 | * |
||
35 | * @param object $bucket |
||
36 | * @return string[] |
||
37 | */ |
||
38 | View Code Duplication | private function getLines($bucket) |
|
56 | |||
57 | /** |
||
58 | * Filters the lines in the passed $lines array, returning a concatenated |
||
59 | * string of decoded lines. |
||
60 | * |
||
61 | * @param array $lines |
||
62 | * @param int $consumed |
||
63 | * @return string |
||
64 | */ |
||
65 | View Code Duplication | private function filterBucketLines(array $lines, &$consumed) |
|
75 | |||
76 | /** |
||
77 | * Filter implementation converts encoding before returning PSFS_PASS_ON. |
||
78 | * |
||
79 | * @param resource $in |
||
80 | * @param resource $out |
||
81 | * @param int $consumed |
||
82 | * @param bool $closing |
||
83 | * @return int |
||
84 | */ |
||
85 | View Code Duplication | public function filter($in, $out, &$consumed, $closing) |
|
94 | } |
||
95 |
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.