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 |
||
13 | class File |
||
14 | { |
||
15 | protected $key; |
||
16 | protected $filesystem; |
||
17 | |||
18 | /** |
||
19 | * Content variable is lazy. It will not be read from filesystem until it's requested first time. |
||
20 | * |
||
21 | * @var mixed content |
||
22 | */ |
||
23 | protected $content = null; |
||
24 | |||
25 | /** |
||
26 | * @var array metadata in associative array. Only for adapters that support metadata |
||
27 | */ |
||
28 | protected $metadata = null; |
||
29 | |||
30 | /** |
||
31 | * Human readable filename (usually the end of the key). |
||
32 | * |
||
33 | * @var string name |
||
34 | */ |
||
35 | protected $name = null; |
||
36 | |||
37 | /** |
||
38 | * File size in bytes. |
||
39 | * |
||
40 | * @var int size |
||
41 | */ |
||
42 | protected $size = 0; |
||
43 | |||
44 | /** |
||
45 | * File date modified. |
||
46 | * |
||
47 | * @var int mtime |
||
48 | */ |
||
49 | protected $mtime = null; |
||
50 | |||
51 | /** |
||
52 | * @param string $key |
||
53 | * @param Filesystem $filesystem |
||
54 | */ |
||
55 | public function __construct($key, Filesystem $filesystem) |
||
61 | |||
62 | /** |
||
63 | * Returns the key. |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | public function getKey() |
||
71 | |||
72 | /** |
||
73 | * Returns the content. |
||
74 | * |
||
75 | * @throws FileNotFound |
||
76 | * |
||
77 | * @param array $metadata optional metadata which should be send when read |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | View Code Duplication | public function getContent($metadata = array()) |
|
90 | |||
91 | /** |
||
92 | * Returns Filesystem instance. |
||
93 | * |
||
94 | * @return Filesystem filesystem instance |
||
95 | */ |
||
96 | public function getFilesystem() |
||
100 | |||
101 | /** |
||
102 | * @return string name of the file |
||
103 | */ |
||
104 | public function getName() |
||
108 | |||
109 | /** |
||
110 | * @return int size of the file |
||
111 | */ |
||
112 | public function getSize() |
||
125 | |||
126 | /** |
||
127 | * Returns the file modified time. |
||
128 | * |
||
129 | * @return int |
||
130 | */ |
||
131 | public function getMtime() |
||
135 | |||
136 | /** |
||
137 | * @param int $size size of the file |
||
138 | */ |
||
139 | public function setSize($size) |
||
143 | |||
144 | /** |
||
145 | * Sets the content. |
||
146 | * |
||
147 | * @param string $content |
||
148 | * @param array $metadata optional metadata which should be send when write |
||
149 | * |
||
150 | * @return int The number of bytes that were written into the file, or |
||
151 | * FALSE on failure |
||
152 | */ |
||
153 | View Code Duplication | public function setContent($content, $metadata = array()) |
|
160 | |||
161 | /** |
||
162 | * @param string $name name of the file |
||
163 | */ |
||
164 | public function setName($name) |
||
168 | |||
169 | /** |
||
170 | * Indicates whether the file exists in the filesystem. |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | public function exists() |
||
178 | |||
179 | /** |
||
180 | * Deletes the file from the filesystem. |
||
181 | * |
||
182 | * @throws FileNotFound |
||
183 | * @throws \RuntimeException when cannot delete file |
||
184 | * |
||
185 | * @param array $metadata optional metadata which should be send when write |
||
186 | * |
||
187 | * @return bool TRUE on success |
||
188 | */ |
||
189 | public function delete($metadata = array()) |
||
195 | |||
196 | /** |
||
197 | * Creates a new file stream instance of the file. |
||
198 | * |
||
199 | * @return Stream |
||
200 | */ |
||
201 | public function createStream() |
||
205 | |||
206 | /** |
||
207 | * Sets the metadata array to be stored in adapters that can support it. |
||
208 | * |
||
209 | * @param array $metadata |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | protected function setMetadata(array $metadata) |
||
223 | |||
224 | /** |
||
225 | * @return bool |
||
226 | */ |
||
227 | private function supportsMetadata() |
||
231 | } |
||
232 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.