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 NativeReadStream extends NativeStream { |
||
16 | const CHUNK_SIZE = 1048576; // 1MB chunks |
||
17 | |||
18 | /** @var StringBuffer */ |
||
19 | private $readBuffer; |
||
20 | |||
21 | public function __construct() { |
||
24 | 40 | ||
25 | 40 | /** @var int */ |
|
26 | private $pos = 0; |
||
27 | 40 | ||
28 | public function stream_open($path, $mode, $options, &$opened_path) { |
||
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 | 40 | * @return resource |
|
40 | 40 | */ |
|
41 | 40 | public static function wrap(NativeState $state, $smbStream, string $mode, string $url) { |
|
44 | 40 | ||
45 | 40 | public function stream_read($count) { |
|
64 | |||
65 | View Code Duplication | public function stream_seek($offset, $whence = SEEK_SET) { |
|
77 | 2 | ||
78 | 2 | public function stream_eof() { |
|
81 | |||
82 | 2 | public function stream_tell() { |
|
85 | 38 | ||
86 | 38 | public function stream_write($data) { |
|
89 | 2 | ||
90 | 2 | public function stream_truncate($size) { |
|
93 | } |
||
94 |
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.