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 |
||
17 | class Directory extends FSAbstract{ |
||
18 | |||
19 | /** |
||
20 | * Directory constructor. |
||
21 | * |
||
22 | * @param \chillerlan\Filereader\Drivers\FSDriverInterface $driver |
||
23 | * @param string $path |
||
24 | */ |
||
25 | public function __construct(FSDriverInterface $driver, string $path){ |
||
30 | |||
31 | /** |
||
32 | * @param string $path |
||
33 | * |
||
34 | * @return \chillerlan\Filereader\Directory |
||
35 | */ |
||
36 | public function change(string $path):Directory{ |
||
41 | |||
42 | /** |
||
43 | * Reads a directory and returns the contents as an array of \stdClass |
||
44 | * |
||
45 | * @return array |
||
46 | * @throws \chillerlan\Filereader\FilereaderException |
||
47 | */ |
||
48 | public function read():array{ |
||
72 | |||
73 | /** |
||
74 | * @param string|null $subdir |
||
75 | * |
||
76 | * @return bool |
||
77 | */ |
||
78 | View Code Duplication | public function create(string $subdir = null):bool{ |
|
86 | |||
87 | /** |
||
88 | * @param string|null $subdir |
||
89 | * |
||
90 | * @return bool |
||
91 | */ |
||
92 | View Code Duplication | public function delete(string $subdir = null):bool{ |
|
100 | |||
101 | /** |
||
102 | * @param string $newname |
||
103 | * @param bool $overwrite |
||
104 | * |
||
105 | * @return \chillerlan\Filereader\Directory |
||
106 | * @throws \chillerlan\Filereader\FilereaderException |
||
107 | */ |
||
108 | public function rename(string $newname, bool $overwrite = true):Directory{ |
||
116 | |||
117 | } |
||
118 |
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.