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 | public static function wrap(NativeState $state, $smbStream, string $mode, string $url) { |
|
44 | 42 | ||
45 | 42 | View Code Duplication | public function stream_seek($offset, $whence = SEEK_SET) { |
57 | 2 | ||
58 | private function flushWrite(): void { |
||
61 | |||
62 | 42 | public function stream_write($data) { |
|
72 | 38 | ||
73 | public function stream_close() { |
||
82 | |||
83 | 42 | public function stream_tell() { |
|
86 | |||
87 | public function stream_read($count) { |
||
90 | |||
91 | 2 | public function stream_truncate($size) { |
|
96 | } |
||
97 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.