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 |
||
24 | class ConvertEncoding implements FileModifierInterface, LoggerAwareInterface, ProcessFactoryAwareInterface |
||
25 | { |
||
26 | use OptionalLoggerTrait; |
||
27 | use FileProcessTrait; |
||
28 | use GetOptionTrait; |
||
29 | |||
30 | /** |
||
31 | * @extend Graze\DataFile\Node\File\LocalFile Only apply to local files |
||
32 | * |
||
33 | * @param LocalFile $file |
||
34 | * @param string $encoding Encoding as defined by iconv |
||
35 | * @param array $options -postfix <string> (Default: toEncoding) |
||
36 | * -keepOldFile <bool> (Default: true) |
||
37 | * |
||
38 | * @return LocalFile |
||
39 | */ |
||
40 | 7 | public function toEncoding(LocalFile $file, $encoding, array $options = []) |
|
70 | |||
71 | /** |
||
72 | * Can this file be modified by this modifier |
||
73 | * |
||
74 | * @param FileNodeInterface $file |
||
75 | * |
||
76 | * @return bool |
||
77 | */ |
||
78 | 3 | public function canModify(FileNodeInterface $file) |
|
85 | |||
86 | /** |
||
87 | * Modify the file |
||
88 | * |
||
89 | * @param FileNodeInterface $file |
||
90 | * @param array $options List of options: |
||
91 | * -encoding <string> |
||
92 | * -postfix <string> (Default: toEncoding) Set this to blank to replace inline |
||
93 | * -keepOldFile <bool> (Default: true) |
||
94 | * |
||
95 | * @return FileNodeInterface |
||
96 | */ |
||
97 | 2 | View Code Duplication | public function modify(FileNodeInterface $file, array $options = []) |
109 | } |
||
110 |
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.