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 |
||
15 | class NativeWriteStream extends NativeStream { |
||
16 | const CHUNK_SIZE = 1048576; // 1MB chunks |
||
17 | |||
18 | /** @var StringBuffer */ |
||
19 | private $writeBuffer; |
||
20 | |||
21 | /** @var int */ |
||
22 | private $pos = 0; |
||
23 | |||
24 | 42 | public function __construct() { |
|
27 | 42 | ||
28 | public function stream_open($path, $mode, $options, &$opened_path): bool { |
||
31 | |||
32 | /** |
||
33 | * Wrap a stream from libsmbclient-php into a regular php stream |
||
34 | * |
||
35 | * @param NativeState $state |
||
36 | * @param resource $smbStream |
||
37 | * @param string $mode |
||
38 | * @param string $url |
||
39 | 42 | * @return resource |
|
40 | 42 | */ |
|
41 | 42 | View Code Duplication | public static function wrap($state, $smbStream, $mode, $url) { |
54 | 2 | ||
55 | 2 | View Code Duplication | public function stream_seek($offset, $whence = SEEK_SET) { |
67 | 42 | ||
68 | private function flushWrite(): void { |
||
71 | 38 | ||
72 | 38 | public function stream_write($data) { |
|
82 | |||
83 | 42 | public function stream_close() { |
|
92 | 2 | ||
93 | public function stream_tell() { |
||
96 | |||
97 | public function stream_read($count) { |
||
100 | 2 | ||
101 | 2 | public function stream_truncate($size) { |
|
106 | } |
||
107 |
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.