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 |
||
13 | class NativeShare extends AbstractShare { |
||
14 | /** |
||
15 | * @var Server $server |
||
16 | */ |
||
17 | private $server; |
||
18 | |||
19 | /** |
||
20 | * @var string $name |
||
21 | */ |
||
22 | private $name; |
||
23 | |||
24 | /** |
||
25 | * @var \Icewind\SMB\NativeState $state |
||
26 | */ |
||
27 | private $state; |
||
28 | |||
29 | /** |
||
30 | * @param Server $server |
||
31 | * @param string $name |
||
32 | */ |
||
33 | public function __construct($server, $name) { |
||
39 | |||
40 | /** |
||
41 | * @throws \Icewind\SMB\Exception\ConnectionException |
||
42 | * @throws \Icewind\SMB\Exception\AuthenticationException |
||
43 | * @throws \Icewind\SMB\Exception\InvalidHostException |
||
44 | */ |
||
45 | protected function connect() { |
||
52 | |||
53 | /** |
||
54 | * Get the name of the share |
||
55 | * |
||
56 | * @return string |
||
57 | */ |
||
58 | public function getName() { |
||
61 | |||
62 | private function buildUrl($path) { |
||
72 | |||
73 | /** |
||
74 | * List the content of a remote folder |
||
75 | * |
||
76 | * @param string $path |
||
77 | * @return \Icewind\SMB\IFileInfo[] |
||
78 | * |
||
79 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
80 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
81 | */ |
||
82 | public function dir($path) { |
||
97 | |||
98 | /** |
||
99 | * @param string $path |
||
100 | * @return \Icewind\SMB\IFileInfo[] |
||
101 | */ |
||
102 | public function stat($path) { |
||
105 | |||
106 | public function getStat($path) { |
||
110 | |||
111 | /** |
||
112 | * Create a folder on the share |
||
113 | * |
||
114 | * @param string $path |
||
115 | * @return bool |
||
116 | * |
||
117 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
118 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
119 | */ |
||
120 | public function mkdir($path) { |
||
124 | |||
125 | /** |
||
126 | * Remove a folder on the share |
||
127 | * |
||
128 | * @param string $path |
||
129 | * @return bool |
||
130 | * |
||
131 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
132 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
133 | */ |
||
134 | public function rmdir($path) { |
||
138 | |||
139 | /** |
||
140 | * Delete a file on the share |
||
141 | * |
||
142 | * @param string $path |
||
143 | * @return bool |
||
144 | * |
||
145 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
146 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
147 | */ |
||
148 | public function del($path) { |
||
152 | |||
153 | /** |
||
154 | * Rename a remote file |
||
155 | * |
||
156 | * @param string $from |
||
157 | * @param string $to |
||
158 | * @return bool |
||
159 | * |
||
160 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
161 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
162 | */ |
||
163 | public function rename($from, $to) { |
||
167 | |||
168 | /** |
||
169 | * Upload a local file |
||
170 | * |
||
171 | * @param string $source local file |
||
172 | * @param string $target remove file |
||
173 | * @return bool |
||
174 | * |
||
175 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
176 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
177 | */ |
||
178 | public function put($source, $target) { |
||
189 | |||
190 | /** |
||
191 | * Download a remote file |
||
192 | * |
||
193 | * @param string $source remove file |
||
194 | * @param string $target local file |
||
195 | * @return bool |
||
196 | * |
||
197 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
198 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
199 | * @throws \Icewind\SMB\Exception\InvalidPathException |
||
200 | * @throws \Icewind\SMB\Exception\InvalidResourceException |
||
201 | */ |
||
202 | public function get($source, $target) { |
||
230 | |||
231 | /** |
||
232 | * Open a readable stream top a remote file |
||
233 | * |
||
234 | * @param string $source |
||
235 | * @return resource a read only stream with the contents of the remote file |
||
236 | * |
||
237 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
238 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
239 | */ |
||
240 | View Code Duplication | public function read($source) { |
|
246 | |||
247 | /** |
||
248 | * Open a readable stream top a remote file |
||
249 | * |
||
250 | * @param string $source |
||
251 | * @return resource a read only stream with the contents of the remote file |
||
252 | * |
||
253 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
254 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
255 | */ |
||
256 | View Code Duplication | public function write($source) { |
|
262 | |||
263 | /** |
||
264 | * Get extended attributes for the path |
||
265 | * |
||
266 | * @param string $path |
||
267 | * @param string $attribute attribute to get the info |
||
268 | * @return string the attribute value |
||
269 | */ |
||
270 | public function getAttribute($path, $attribute) { |
||
276 | |||
277 | /** |
||
278 | * Get extended attributes for the path |
||
279 | * |
||
280 | * @param string $path |
||
281 | * @param string $attribute attribute to get the info |
||
282 | * @param mixed $value |
||
283 | * @return string the attribute value |
||
284 | */ |
||
285 | public function setAttribute($path, $attribute, $value) { |
||
294 | |||
295 | /** |
||
296 | * @param string $path |
||
297 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
||
298 | * @return mixed |
||
299 | */ |
||
300 | public function setMode($path, $mode) { |
||
303 | |||
304 | /** |
||
305 | * @param string $path |
||
306 | * @param callable $callback callable which will be called for each received change |
||
307 | * @return mixed |
||
308 | */ |
||
309 | public function notify($path, callable $callback) { |
||
312 | |||
313 | public function __destruct() { |
||
316 | } |
||
317 |
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.