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 NativeStream implements File { |
||
16 | /** |
||
17 | * @var resource |
||
18 | * @psalm-suppress PropertyNotSetInConstructor |
||
19 | */ |
||
20 | public $context; |
||
21 | |||
22 | /** |
||
23 | * @var NativeState |
||
24 | * @psalm-suppress PropertyNotSetInConstructor |
||
25 | */ |
||
26 | protected $state; |
||
27 | |||
28 | /** |
||
29 | * @var resource |
||
30 | * @psalm-suppress PropertyNotSetInConstructor |
||
31 | */ |
||
32 | protected $handle; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | protected $eof = false; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $url = ''; |
||
43 | |||
44 | /** |
||
45 | * Wrap a stream from libsmbclient-php into a regular php stream |
||
46 | * |
||
47 | * @param NativeState $state |
||
48 | * @param resource $smbStream |
||
49 | * @param string $mode |
||
50 | * @param string $url |
||
51 | * @return resource |
||
52 | */ |
||
53 | View Code Duplication | public static function wrap($state, $smbStream, $mode, $url) { |
|
66 | |||
67 | public function stream_close() { |
||
74 | |||
75 | 38 | public function stream_eof() { |
|
78 | |||
79 | 78 | public function stream_flush() { |
|
82 | 78 | ||
83 | 78 | ||
84 | 78 | public function stream_open($path, $mode, $options, &$opened_path) { |
|
106 | 36 | ||
107 | public function stream_read($count) { |
||
114 | |||
115 | public function stream_seek($offset, $whence = SEEK_SET) { |
||
123 | |||
124 | 2 | public function stream_stat() { |
|
131 | |||
132 | public function stream_tell() { |
||
135 | |||
136 | public function stream_write($data) { |
||
139 | |||
140 | public function stream_truncate($size) { |
||
143 | |||
144 | public function stream_set_option($option, $arg1, $arg2) { |
||
147 | |||
148 | public function stream_lock($operation) { |
||
151 | } |
||
152 |
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.