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 |
||
27 | View Code Duplication | class Tail implements FileModifierInterface, LoggerAwareInterface, BuilderAwareInterface |
|
|
|||
28 | { |
||
29 | use OptionalLoggerTrait; |
||
30 | use FileProcessTrait; |
||
31 | use GetOptionTrait; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $lines; |
||
37 | |||
38 | /** |
||
39 | * Can this file be modified by this modifier |
||
40 | * |
||
41 | * @param FileNodeInterface $file |
||
42 | * |
||
43 | * @return bool |
||
44 | */ |
||
45 | 1 | public function canModify(FileNodeInterface $file) |
|
50 | |||
51 | /** |
||
52 | * Modify the file |
||
53 | * |
||
54 | * @param FileNodeInterface $file |
||
55 | * @param array $options List of options: |
||
56 | * -lines <string> Number of lines to tail (accepts +/- modifiers) |
||
57 | * -postfix <string> (Default: replace) Set this to blank to replace inline |
||
58 | * -keepOldFile <bool> (Default: true) |
||
59 | * |
||
60 | * @return FileNodeInterface |
||
61 | */ |
||
62 | 4 | public function modify(FileNodeInterface $file, array $options = []) |
|
74 | |||
75 | /** |
||
76 | * Tail a file |
||
77 | * |
||
78 | * @param LocalFile $file |
||
79 | * @param string $lines Number of lines to tail (accepts +/- modifiers) |
||
80 | * @param array $options List of options: |
||
81 | * -postfix <string> (Default: tail) |
||
82 | * -keepOldFile <bool> (Default: true) |
||
83 | * |
||
84 | * @throws ProcessFailedException |
||
85 | * @return LocalFile |
||
86 | */ |
||
87 | 8 | public function tail(LocalFile $file, $lines, array $options = []) |
|
104 | } |
||
105 |
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.