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 |
||
5 | class FileUtils extends UtilsBase |
||
6 | { |
||
7 | const FILES = 1; |
||
8 | const DIRS = 2; |
||
9 | const ALL = 3; |
||
10 | |||
11 | /** |
||
12 | * @param string $path |
||
13 | * @param int $fileOrDirs |
||
14 | * @param bool $recursive |
||
15 | * |
||
16 | * @return array |
||
17 | */ |
||
18 | public function scanDir($path, $fileOrDirs = self::ALL, $recursive = true) |
||
50 | |||
51 | /** |
||
52 | * @var string $tokens ... |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | public function makePath() |
||
62 | |||
63 | private function sanitizePathTokens($tokens) |
||
75 | |||
76 | /** |
||
77 | * @param $fileName |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | View Code Duplication | public function getExtension($fileName) |
|
91 | |||
92 | /** |
||
93 | * @param $fileName |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | View Code Duplication | public function getNameWithoutExtension($fileName) |
|
107 | |||
108 | /** |
||
109 | * @param string $fileName |
||
110 | * @param string $extension |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | public function extensionIs($fileName, $extension) |
||
120 | |||
121 | private function stripPathLastDirectorySeparator($path) |
||
129 | } |
||
130 |
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.