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 FluentFilesLoader implements FluentFilesLoaderContract, FilesTransformerContract |
||
14 | { |
||
15 | use FilesTransformer; |
||
16 | |||
17 | /** |
||
18 | * API Client. |
||
19 | * |
||
20 | * @var \ArgentCrusade\Selectel\CloudStorage\Contracts\Api\ApiClientContract |
||
21 | */ |
||
22 | protected $api; |
||
23 | |||
24 | /** |
||
25 | * Container name. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $containerName = ''; |
||
30 | |||
31 | /** |
||
32 | * Container URL. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $containerUrl = ''; |
||
37 | |||
38 | /** |
||
39 | * Default parameters. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $params = [ |
||
44 | 'limit' => 10000, |
||
45 | 'marker' => '', |
||
46 | 'path' => '', |
||
47 | 'prefix' => '', |
||
48 | 'delimiter' => '', |
||
49 | ]; |
||
50 | |||
51 | /** |
||
52 | * Determines if resulting Collection should container File objects |
||
53 | * instead of file arrays. |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $asFileObjects = false; |
||
58 | |||
59 | /** |
||
60 | * @param \ArgentCrusade\Selectel\CloudStorage\Contracts\Api\ApiClientContract $api |
||
61 | * @param string $container |
||
62 | * @param string $containerUrl |
||
63 | */ |
||
64 | public function __construct(ApiClientContract $api, $container, $containerUrl) |
||
70 | |||
71 | /** |
||
72 | * Sets loader parameter. |
||
73 | * |
||
74 | * @param string $key |
||
75 | * @param string|int $value |
||
76 | * @param bool $trimLeadingSlashes = true |
||
77 | * |
||
78 | * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\FluentFilesLoaderContract |
||
79 | */ |
||
80 | protected function setParam($key, $value, $trimLeadingSlashes = true) |
||
86 | |||
87 | /** |
||
88 | * Container name. |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | public function containerName() |
||
96 | |||
97 | /** |
||
98 | * Sets directory from where load files. This value may be overwritten |
||
99 | * to empty string if you're loading prefixed files from directory. |
||
100 | * |
||
101 | * @param string $directory |
||
102 | * |
||
103 | * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\FluentFilesLoaderContract |
||
104 | */ |
||
105 | public function fromDirectory($directory) |
||
109 | |||
110 | /** |
||
111 | * Sets files prefix. If you're planning to find prefixed files from a directory |
||
112 | * (using along with fromDirectory method), do not provide path to a directory |
||
113 | * here, since it will be appended to final prefix (before sending request). |
||
114 | * |
||
115 | * @param string $prefix |
||
116 | * |
||
117 | * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\FluentFilesLoaderContract |
||
118 | */ |
||
119 | public function withPrefix($prefix) |
||
123 | |||
124 | /** |
||
125 | * Sets files delimiter. |
||
126 | * |
||
127 | * @param string $delimiter |
||
128 | * |
||
129 | * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\FluentFilesLoaderContract |
||
130 | */ |
||
131 | public function withDelimiter($delimiter) |
||
135 | |||
136 | /** |
||
137 | * Sets files limit. If you need to paginate through results, pass markerFile |
||
138 | * argument with latest filename from previous request as value. If you're |
||
139 | * working within a directory, its path will be appended to markerFile. |
||
140 | * |
||
141 | * @param int $limit |
||
142 | * @param string $markerFile = '' |
||
143 | * |
||
144 | * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\FluentFilesLoaderContract |
||
145 | */ |
||
146 | public function limit($limit, $markerFile = '') |
||
151 | |||
152 | /** |
||
153 | * Tells builder to return Collection of File objects instead of arrays. |
||
154 | * |
||
155 | * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\FluentFilesLoaderContract |
||
156 | */ |
||
157 | public function asFileObjects() |
||
163 | |||
164 | /** |
||
165 | * Loads all available files from container. |
||
166 | * |
||
167 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
168 | * |
||
169 | * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\Collections\CollectionContract |
||
170 | */ |
||
171 | public function all() |
||
179 | |||
180 | /** |
||
181 | * Determines whether file exists or not. |
||
182 | * |
||
183 | * @param string $path File path. |
||
184 | * |
||
185 | * @return bool |
||
186 | */ |
||
187 | public function exists($path) |
||
191 | |||
192 | /** |
||
193 | * Finds single file at given path. |
||
194 | * |
||
195 | * @param string $path |
||
196 | * |
||
197 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\FileNotFoundException |
||
198 | * |
||
199 | * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\FileContract |
||
200 | */ |
||
201 | public function find($path) |
||
211 | |||
212 | /** |
||
213 | * Loads file from path. |
||
214 | * |
||
215 | * @param string $path |
||
216 | * |
||
217 | * @return array|null |
||
218 | */ |
||
219 | protected function findFileAt($path) |
||
233 | |||
234 | /** |
||
235 | * Loads files. |
||
236 | * |
||
237 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
238 | * |
||
239 | * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\Collections\CollectionContract |
||
240 | */ |
||
241 | public function get() |
||
272 | |||
273 | /** |
||
274 | * Builds query parameters. |
||
275 | * |
||
276 | * @return array |
||
277 | */ |
||
278 | protected function buildParams() |
||
299 | } |
||
300 |
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.