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 |
||
22 | class FileOutputStream implements OutputStream, Lockable |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * The file handle |
||
27 | * |
||
28 | * @var resource |
||
29 | */ |
||
30 | private $handle; |
||
31 | |||
32 | /** |
||
33 | * The absolute file path and name |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | private $fileName; |
||
38 | |||
39 | /** |
||
40 | * Whether resource is locked |
||
41 | * |
||
42 | * @var bool |
||
43 | */ |
||
44 | private $locked; |
||
45 | |||
46 | /** |
||
47 | * Whether to append |
||
48 | * |
||
49 | * @var bool |
||
50 | */ |
||
51 | private $append; |
||
52 | |||
53 | /** |
||
54 | * Create a new FileOutputStream |
||
55 | * |
||
56 | * @param string $file |
||
57 | * The absolute (or relative) path to file to write into. |
||
58 | * @param boolean $append |
||
59 | * Whether to append the data to an existing file. |
||
60 | * @throws FileExistsException will be thrown in case of file exists and append is set to false. |
||
61 | * @throws NoAccessException will be thrown in case of it is not possible to write to file. |
||
62 | */ |
||
63 | 25 | public function __construct($file, $append = false) |
|
67 | |||
68 | 25 | private function open($file, $append) |
|
102 | |||
103 | /** |
||
104 | * Cleanup (e.g. |
||
105 | * release lock) |
||
106 | */ |
||
107 | 24 | public function __destruct() |
|
117 | |||
118 | /** |
||
119 | * |
||
120 | * {@inheritdoc} |
||
121 | * @see \Generics\Streams\Stream::ready() |
||
122 | */ |
||
123 | 24 | public function ready(): bool |
|
127 | |||
128 | /** |
||
129 | * |
||
130 | * {@inheritdoc} |
||
131 | * @see \Generics\Streams\OutputStream::write() |
||
132 | */ |
||
133 | 19 | public function write($buffer) |
|
156 | |||
157 | /** |
||
158 | * |
||
159 | * {@inheritdoc} |
||
160 | * @see \Generics\Streams\Stream::close() |
||
161 | */ |
||
162 | 21 | public function close() |
|
169 | |||
170 | /** |
||
171 | * |
||
172 | * {@inheritdoc} |
||
173 | * @see \Countable::count() |
||
174 | */ |
||
175 | 2 | public function count(): int |
|
183 | |||
184 | /** |
||
185 | * Retrieve the file path and name |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | 1 | public function __toString(): string |
|
193 | |||
194 | /** |
||
195 | * |
||
196 | * {@inheritdoc} |
||
197 | * @see \Generics\Streams\OutputStream::isWriteable() |
||
198 | */ |
||
199 | 1 | public function isWriteable(): bool |
|
203 | |||
204 | /** |
||
205 | * |
||
206 | * {@inheritdoc} |
||
207 | * @see \Generics\Streams\OutputStream::flush() |
||
208 | */ |
||
209 | 16 | public function flush() |
|
217 | |||
218 | /** |
||
219 | * |
||
220 | * {@inheritdoc} |
||
221 | * @see \Generics\Lockable::lock() |
||
222 | */ |
||
223 | 3 | View Code Duplication | public function lock() |
230 | |||
231 | /** |
||
232 | * |
||
233 | * {@inheritdoc} |
||
234 | * @see \Generics\Lockable::unlock() |
||
235 | */ |
||
236 | 4 | View Code Duplication | public function unlock() |
243 | |||
244 | /** |
||
245 | * |
||
246 | * {@inheritdoc} |
||
247 | * @see \Generics\Lockable::isLocked() |
||
248 | */ |
||
249 | 1 | public function isLocked(): bool |
|
253 | |||
254 | /** |
||
255 | * |
||
256 | * {@inheritdoc} |
||
257 | * @see \Generics\Streams\Stream::isOpen() |
||
258 | */ |
||
259 | public function isOpen(): bool |
||
263 | |||
264 | /** |
||
265 | * |
||
266 | * {@inheritdoc} |
||
267 | * @see \Generics\Resettable::reset() |
||
268 | */ |
||
269 | public function reset() |
||
278 | } |
||
279 |