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 |
||
29 | class Path extends JBPAth |
||
30 | { |
||
31 | |||
32 | const LS_MODE_DIR = 'dir'; |
||
33 | const LS_MODE_FILE = 'file'; |
||
34 | |||
35 | /** |
||
36 | * Flag of result path (If true, is real path. If false, is relative path). |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $_isReal = false; |
||
41 | |||
42 | /** |
||
43 | * Get a list of directories from a resource. |
||
44 | * |
||
45 | * @param string $resource |
||
46 | * @param bool $recursive |
||
47 | * @param null $filter |
||
48 | * @return mixed |
||
49 | */ |
||
50 | public function dirs($resource, $recursive = false, $filter = null) |
||
54 | |||
55 | /** |
||
56 | * Get url to a file. |
||
57 | * |
||
58 | * @param string $source |
||
59 | * @param bool $full |
||
60 | * @return null|string |
||
61 | */ |
||
62 | public function url($source, $full = true) |
||
74 | |||
75 | /** |
||
76 | * Get path instance. |
||
77 | * |
||
78 | * @param string $key |
||
79 | * @return Path |
||
80 | * @throws Exception |
||
81 | */ |
||
82 | public static function getInstance($key = 'default') |
||
94 | |||
95 | /** |
||
96 | * Get a list of files or diretories from a resource. |
||
97 | * |
||
98 | * @param string $resource |
||
99 | * @param string $mode |
||
100 | * @param bool $recursive |
||
101 | * @param null $filter |
||
102 | * @return array |
||
103 | * @SuppressWarnings(PHPMD.ShortMethodName) |
||
104 | */ |
||
105 | public function ls($resource, $mode = self::LS_MODE_FILE, $recursive = false, $filter = null) |
||
122 | |||
123 | /** |
||
124 | * Get the list of files or directories in a given path. |
||
125 | * |
||
126 | * @param string $path |
||
127 | * @param string $prefix |
||
128 | * @param string $mode |
||
129 | * @param bool $recursive |
||
130 | * @param null $filter |
||
131 | * @return array |
||
132 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
133 | */ |
||
134 | protected function _list($path, $prefix = '', $mode = 'file', $recursive = false, $filter = null) |
||
184 | } |
||
185 |
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.