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 |
||
| 19 | class FileInputStream implements InputStream, Lockable |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The file handle |
||
| 24 | * |
||
| 25 | * @var resource |
||
| 26 | */ |
||
| 27 | private $handle; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The absolute file path and name |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $fileName; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Whether the access is locked |
||
| 38 | * |
||
| 39 | * @var bool |
||
| 40 | */ |
||
| 41 | private $locked; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Create a new FileInputStream |
||
| 45 | * |
||
| 46 | * @param string $file |
||
| 47 | * The absolute (or relative) path to the file to open |
||
| 48 | * @throws FileNotFoundException |
||
| 49 | */ |
||
| 50 | 25 | public function __construct($file) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Cleanup (e.g. |
||
| 71 | * release lock) |
||
| 72 | */ |
||
| 73 | 24 | public function __destruct() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * |
||
| 86 | * {@inheritdoc} |
||
| 87 | * @see \Generics\Streams\Stream::close() |
||
| 88 | */ |
||
| 89 | 18 | public function close() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * |
||
| 99 | * {@inheritdoc} |
||
| 100 | * @see \Generics\Streams\Stream::ready() |
||
| 101 | */ |
||
| 102 | 24 | public function ready(): bool |
|
| 106 | |||
| 107 | /** |
||
| 108 | * |
||
| 109 | * {@inheritdoc} |
||
| 110 | * @see \Generics\Streams\InputStream::read() |
||
| 111 | */ |
||
| 112 | 20 | public function read($length = 1, $offset = null): string |
|
| 126 | |||
| 127 | /** |
||
| 128 | * |
||
| 129 | * {@inheritdoc} |
||
| 130 | * @see \Countable::count() |
||
| 131 | */ |
||
| 132 | 3 | public function count(): int |
|
| 137 | |||
| 138 | /** |
||
| 139 | * |
||
| 140 | * {@inheritdoc} |
||
| 141 | * @see \Generics\Streams\InputStream::reset() |
||
| 142 | */ |
||
| 143 | 3 | public function reset() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * |
||
| 150 | * {@inheritdoc} |
||
| 151 | * @see \Generics\Lockable::lock() |
||
| 152 | */ |
||
| 153 | 2 | View Code Duplication | public function lock() |
| 160 | |||
| 161 | /** |
||
| 162 | * |
||
| 163 | * {@inheritdoc} |
||
| 164 | * @see \Generics\Lockable::unlock() |
||
| 165 | */ |
||
| 166 | 2 | View Code Duplication | public function unlock() |
| 173 | |||
| 174 | /** |
||
| 175 | * |
||
| 176 | * {@inheritdoc} |
||
| 177 | * @see \Generics\Lockable::isLocked() |
||
| 178 | */ |
||
| 179 | 1 | public function isLocked(): bool |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Retrieve the file path and name |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function __toString(): string |
||
| 193 | |||
| 194 | /** |
||
| 195 | * |
||
| 196 | * {@inheritdoc} |
||
| 197 | * @see \Generics\Streams\Stream::isOpen() |
||
| 198 | */ |
||
| 199 | public function isOpen(): bool |
||
| 203 | } |
||
| 204 |