Total Complexity | 69 |
Total Lines | 440 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Standard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Standard, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Standard |
||
25 | extends \Aimeos\MShop\Common\Item\Base |
||
26 | implements \Aimeos\MShop\Media\Item\Iface |
||
27 | { |
||
28 | use ListsRef\Traits, PropertyRef\Traits { |
||
29 | PropertyRef\Traits::__clone as __cloneProperty; |
||
30 | ListsRef\Traits::__clone insteadof PropertyRef\Traits; |
||
31 | ListsRef\Traits::__clone as __cloneList; |
||
32 | ListsRef\Traits::getName as getNameList; |
||
33 | } |
||
34 | |||
35 | |||
36 | private $langid; |
||
37 | |||
38 | |||
39 | /** |
||
40 | * Initializes the media item object. |
||
41 | * |
||
42 | * @param array $values Initial values of the media item |
||
43 | * @param \Aimeos\MShop\Common\Item\Lists\Iface[] $listItems List of list items |
||
44 | * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items |
||
45 | * @param \Aimeos\MShop\Common\Item\Property\Iface[] $propItems List of property items |
||
46 | */ |
||
47 | public function __construct( array $values = [], array $listItems = [], |
||
48 | array $refItems = [], array $propItems = [] ) |
||
49 | { |
||
50 | if( isset( $values['media.preview'] ) && !isset( $values['media.previews'] ) ) { |
||
51 | $values['media.previews'] = [1 => $values['media.preview']]; |
||
52 | } |
||
53 | |||
54 | parent::__construct( 'media.', $values ); |
||
55 | |||
56 | $this->langid = ( isset( $values['.languageid'] ) ? $values['.languageid'] : null ); |
||
57 | $this->initListItems( $listItems, $refItems ); |
||
58 | $this->initPropertyItems( $propItems ); |
||
59 | } |
||
60 | |||
61 | |||
62 | /** |
||
63 | * Creates a deep clone of all objects |
||
64 | */ |
||
65 | public function __clone() |
||
66 | { |
||
67 | parent::__clone(); |
||
68 | $this->__cloneList(); |
||
69 | $this->__cloneProperty(); |
||
70 | } |
||
71 | |||
72 | |||
73 | /** |
||
74 | * Returns the name of the file system the referenced file is stored. |
||
75 | * |
||
76 | * @return string Name of the file system |
||
77 | */ |
||
78 | public function getFileSystem() : string |
||
79 | { |
||
80 | return $this->get( 'media.filesystem', 'fs-media' ); |
||
81 | } |
||
82 | |||
83 | |||
84 | /** |
||
85 | * Sets the name of the file system the referenced file is stored. |
||
86 | * |
||
87 | * @param string $value Name of the file system |
||
88 | * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls |
||
89 | */ |
||
90 | public function setFileSystem( string $value ) : \Aimeos\MShop\Media\Item\Iface |
||
91 | { |
||
92 | return $this->set( 'media.filesystem', $value ); |
||
93 | } |
||
94 | |||
95 | |||
96 | /** |
||
97 | * Returns the ISO language code. |
||
98 | * |
||
99 | * @return string|null ISO language code (e.g. de or de_DE) |
||
100 | */ |
||
101 | public function getLanguageId() : ?string |
||
102 | { |
||
103 | return $this->get( 'media.languageid' ); |
||
104 | } |
||
105 | |||
106 | |||
107 | /** |
||
108 | * Sets the ISO language code. |
||
109 | * |
||
110 | * @param string|null $id ISO language code (e.g. de or de_DE) |
||
111 | * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls |
||
112 | * @throws \Aimeos\MShop\Exception If the language ID is invalid |
||
113 | */ |
||
114 | public function setLanguageId( ?string $id ) : \Aimeos\MShop\Media\Item\Iface |
||
115 | { |
||
116 | return $this->set( 'media.languageid', $this->checkLanguageId( $id ) ); |
||
117 | } |
||
118 | |||
119 | |||
120 | /** |
||
121 | * Returns the type code of the media item. |
||
122 | * |
||
123 | * @return string|null Type code of the media item |
||
124 | */ |
||
125 | public function getType() : ?string |
||
126 | { |
||
127 | return $this->get( 'media.type', 'default' ); |
||
128 | } |
||
129 | |||
130 | |||
131 | /** |
||
132 | * Sets the new type of the media. |
||
133 | * |
||
134 | * @param string $type Type of the media |
||
135 | * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls |
||
136 | */ |
||
137 | public function setType( string $type ) : \Aimeos\MShop\Common\Item\Iface |
||
138 | { |
||
139 | return $this->set( 'media.type', $this->checkCode( $type ) ); |
||
140 | } |
||
141 | |||
142 | |||
143 | /** |
||
144 | * Returns the domain of the media item, if available. |
||
145 | * |
||
146 | * @return string Domain the media item belongs to |
||
147 | */ |
||
148 | public function getDomain() : string |
||
149 | { |
||
150 | return (string) $this->get( 'media.domain', '' ); |
||
151 | } |
||
152 | |||
153 | |||
154 | /** |
||
155 | * Sets the domain of the media item. |
||
156 | * |
||
157 | * @param string $domain Domain of media item |
||
158 | * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls |
||
159 | */ |
||
160 | public function setDomain( string $domain ) : \Aimeos\MShop\Common\Item\Iface |
||
161 | { |
||
162 | return $this->set( 'media.domain', (string) $domain ); |
||
163 | } |
||
164 | |||
165 | |||
166 | /** |
||
167 | * Returns the label of the media item. |
||
168 | * |
||
169 | * @return string Label of the media item |
||
170 | */ |
||
171 | public function getLabel() : string |
||
172 | { |
||
173 | return (string) $this->get( 'media.label', '' ); |
||
174 | } |
||
175 | |||
176 | |||
177 | /** |
||
178 | * Sets the new label of the media item. |
||
179 | * |
||
180 | * @param string $label Label of the media item |
||
181 | * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls |
||
182 | */ |
||
183 | public function setLabel( ?string $label ) : \Aimeos\MShop\Media\Item\Iface |
||
184 | { |
||
185 | return $this->set( 'media.label', (string) $label ); |
||
186 | } |
||
187 | |||
188 | |||
189 | /** |
||
190 | * Returns the status of the media item. |
||
191 | * |
||
192 | * @return int Status of the item |
||
193 | */ |
||
194 | public function getStatus() : int |
||
195 | { |
||
196 | return (int) $this->get( 'media.status', 1 ); |
||
197 | } |
||
198 | |||
199 | |||
200 | /** |
||
201 | * Sets the new status of the media item. |
||
202 | * |
||
203 | * @param int $status Status of the item |
||
204 | * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls |
||
205 | */ |
||
206 | public function setStatus( int $status ) : \Aimeos\MShop\Common\Item\Iface |
||
207 | { |
||
208 | return $this->set( 'media.status', $status ); |
||
209 | } |
||
210 | |||
211 | |||
212 | /** |
||
213 | * Returns the mime type of the media item. |
||
214 | * |
||
215 | * @return string Mime type of the media item |
||
216 | */ |
||
217 | public function getMimeType() : string |
||
220 | } |
||
221 | |||
222 | |||
223 | /** |
||
224 | * Sets the new mime type of the media. |
||
225 | * |
||
226 | * @param string $mimetype Mime type of the media item |
||
227 | * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls |
||
228 | */ |
||
229 | public function setMimeType( string $mimetype ) : \Aimeos\MShop\Media\Item\Iface |
||
230 | { |
||
236 | } |
||
237 | |||
238 | |||
239 | /** |
||
240 | * Returns the url of the media item. |
||
241 | * |
||
242 | * @param bool $version TRUE to add file version as parameter, FALSE for path only |
||
243 | * @return string URL of the media file |
||
244 | */ |
||
245 | public function getUrl( bool $version = false ) : string |
||
246 | { |
||
247 | $url = (string) $this->get( 'media.url', '' ); |
||
248 | |||
249 | if( $url && $version && !\Aimeos\Base\Str::starts( $url, ['http', 'data:', '/'] ) && $this->getTimeModified() ) { |
||
250 | $url .= '?v=' . str_replace( ['-', ' ', ':'], '', $this->getTimeModified() ); |
||
251 | } |
||
252 | |||
253 | return $url; |
||
254 | } |
||
255 | |||
256 | |||
257 | /** |
||
258 | * Sets the new url of the media item. |
||
259 | * |
||
260 | * @param string|null $url URL of the media file |
||
261 | * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls |
||
262 | */ |
||
263 | public function setUrl( ?string $url ) : \Aimeos\MShop\Media\Item\Iface |
||
266 | } |
||
267 | |||
268 | |||
269 | /** |
||
270 | * Returns the preview url of the media item. |
||
271 | * |
||
272 | * @param bool|int $size TRUE for the largest image, FALSE for the smallest or a concrete image width |
||
273 | * @return string Preview URL of the media file |
||
274 | */ |
||
275 | public function getPreview( $width = false ) : string |
||
276 | { |
||
277 | if( ( $list = (array) $this->get( 'media.previews', [] ) ) === [] ) { |
||
278 | return ''; |
||
279 | } |
||
280 | |||
281 | ksort( $list ); |
||
282 | $path = ''; |
||
283 | |||
284 | if( $width === false ) { |
||
285 | $path = reset( $list ); |
||
286 | } elseif( $width === true ) { |
||
287 | $path = end( $list ); |
||
288 | } elseif( isset( $list[$width] ) ) { |
||
289 | $path = $list[$width]; |
||
290 | } else { |
||
291 | $before = $after = []; |
||
292 | |||
293 | foreach( $list as $idx => $path ) |
||
294 | { |
||
295 | if( $idx < $width ) { |
||
296 | $before[$idx] = $path; |
||
297 | } else { |
||
298 | $after[$idx] = $path; |
||
299 | } |
||
300 | } |
||
301 | |||
302 | if( ( $path = array_shift( $after ) ) === null && ( $path = array_pop( $before ) ) === null ) { |
||
303 | return ''; |
||
304 | } |
||
305 | } |
||
306 | |||
307 | if( $path && !\Aimeos\Base\Str::starts( $path, ['http', 'data:', '/'] ) && $this->getTimeModified() ) { |
||
308 | $path .= '?v=' . str_replace( ['-', ' ', ':'], '', $this->getTimeModified() ); |
||
309 | } |
||
310 | |||
311 | return $path; |
||
312 | } |
||
313 | |||
314 | |||
315 | /** |
||
316 | * Returns all preview urls for images of different sizes. |
||
317 | * |
||
318 | * @param bool $version TRUE to add file version as parameter, FALSE for path only |
||
319 | * @return array Associative list of widths in pixels as keys and urls as values |
||
320 | */ |
||
321 | public function getPreviews( bool $version = false ) : array |
||
322 | { |
||
323 | $previews = (array) $this->get( 'media.previews', [] ); |
||
324 | |||
325 | if( $version && $this->getTimeModified() ) |
||
326 | { |
||
327 | foreach( $previews as $key => $path ) |
||
328 | { |
||
329 | if( $path && !\Aimeos\Base\Str::starts( $path, ['http', 'data:', '/'] ) ) { |
||
330 | $previews[$key] = $path . '?v=' . str_replace( ['-', ' ', ':'], '', $this->getTimeModified() ); |
||
331 | } |
||
332 | } |
||
333 | } |
||
334 | |||
335 | return $previews; |
||
336 | } |
||
337 | |||
338 | |||
339 | /** |
||
340 | * Sets the new preview url of the media item. |
||
341 | * |
||
342 | * @param string $url Preview URL of the media file |
||
343 | * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls |
||
344 | */ |
||
345 | public function setPreview( string $url ) : \Aimeos\MShop\Media\Item\Iface |
||
348 | } |
||
349 | |||
350 | |||
351 | /** |
||
352 | * Sets the new preview urls for images of different sizes. |
||
353 | * |
||
354 | * @param array $url List of preview URLs with widths of the media file in pixels as keys |
||
355 | * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls |
||
356 | */ |
||
357 | public function setPreviews( array $urls ) : \Aimeos\MShop\Media\Item\Iface |
||
358 | { |
||
359 | return $this->set( 'media.previews', $urls ); |
||
360 | } |
||
361 | |||
362 | |||
363 | /** |
||
364 | * Returns the localized text type of the item or the internal label if no name is available. |
||
365 | * |
||
366 | * @param string $type Text type to be returned |
||
367 | * @param string|null $langId Two letter ISO Language code of the text |
||
368 | * @return string Specified text type or label of the item |
||
369 | */ |
||
370 | public function getName( string $type = 'name', string $langId = null ) : string |
||
371 | { |
||
372 | foreach( $this->getPropertyItems( $type ) as $propItem ) |
||
373 | { |
||
374 | if( $propItem->getLanguageId() === $langId || $langId === null ) { |
||
375 | return $propItem->getValue(); |
||
376 | } |
||
377 | } |
||
378 | |||
379 | return $this->getNameList( $type ); |
||
380 | } |
||
381 | |||
382 | |||
383 | /** |
||
384 | * Returns the item type |
||
385 | * |
||
386 | * @return string Item type, subtypes are separated by slashes |
||
387 | */ |
||
388 | public function getResourceType() : string |
||
389 | { |
||
390 | return 'media'; |
||
391 | } |
||
392 | |||
393 | |||
394 | /** |
||
395 | * Tests if the item is available based on status, time, language and currency |
||
396 | * |
||
397 | * @return bool True if available, false if not |
||
398 | */ |
||
399 | public function isAvailable() : bool |
||
400 | { |
||
401 | return parent::isAvailable() && $this->getStatus() > 0 |
||
402 | && ( $this->langid === null || $this->getLanguageId() === null |
||
403 | || $this->getLanguageId() === $this->langid ); |
||
404 | } |
||
405 | |||
406 | |||
407 | /* |
||
408 | * Sets the item values from the given array and removes that entries from the list |
||
409 | * |
||
410 | * @param array &$list Associative list of item keys and their values |
||
411 | * @param bool True to set private properties too, false for public only |
||
412 | * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls |
||
413 | */ |
||
414 | public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface |
||
439 | } |
||
440 | |||
441 | |||
442 | /** |
||
443 | * Returns the item values as array. |
||
444 | * |
||
445 | * @param bool True to return private properties, false for public only |
||
446 | * @return array Associative list of item properties and their values |
||
447 | */ |
||
448 | public function toArray( bool $private = false ) : array |
||
464 | } |
||
465 | } |
||
466 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths