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($state, $smbStream, $mode, $url) { |
|
54 | |||
55 | public function stream_read($count) { |
||
74 | |||
75 | 2 | View Code Duplication | public function stream_seek($offset, $whence = SEEK_SET) { |
87 | |||
88 | public function stream_eof() { |
||
91 | |||
92 | public function stream_tell() { |
||
95 | |||
96 | public function stream_write($data) { |
||
99 | |||
100 | public function stream_truncate($size) { |
||
103 | } |
||
104 |
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.