Standard   F
last analyzed

Complexity

Total Complexity 66

Size/Duplication

Total Lines 496
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 66
eloc 95
c 0
b 0
f 0
dl 0
loc 496
rs 3.12

37 Methods

Rating   Name   Duplication   Size   Complexity  
A setTimeCreated() 0 3 1
A setLabel() 0 3 1
A setConfig() 0 3 1
A getScale() 0 3 2
A setDateStart() 0 3 1
A setDataset() 0 3 1
A inStock() 0 3 1
A setTarget() 0 3 1
A setDateEnd() 0 3 1
A getUrl() 0 3 2
A setScale() 0 3 2
A getRating() 0 3 1
B isAvailable() 0 7 8
A setStatus() 0 3 1
A setBoost() 0 3 1
A setCode() 0 3 1
A getRatings() 0 3 1
A getName() 0 9 3
A getConfig() 0 3 1
A getDateEnd() 0 4 2
A getType() 0 3 1
A getParentItems() 0 3 1
A __construct() 0 7 1
A setUrl() 0 3 1
C fromArray() 0 29 16
A getStatus() 0 3 1
A getCode() 0 3 1
A __clone() 0 6 1
A getDateStart() 0 4 2
A getDataset() 0 3 1
A getLabel() 0 3 1
A getTimeCreated() 0 3 1
A getTarget() 0 3 1
A boost() 0 3 1
A getSiteItem() 0 3 1
A toArray() 0 22 1
A setInStock() 0 3 1

How to fix   Complexity   

Complex Class

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
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package MShop
7
 * @subpackage Product
8
 */
9
10
11
namespace Aimeos\MShop\Product\Item;
12
13
use \Aimeos\MShop\Common\Item\Config;
0 ignored issues
show
Bug introduced by
The type \Aimeos\MShop\Common\Item\Config was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use \Aimeos\MShop\Common\Item\ListsRef;
0 ignored issues
show
Bug introduced by
The type \Aimeos\MShop\Common\Item\ListsRef was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use \Aimeos\MShop\Common\Item\PropertyRef;
0 ignored issues
show
Bug introduced by
The type \Aimeos\MShop\Common\Item\PropertyRef was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use \Aimeos\MShop\Common\Item\TypeRef;
0 ignored issues
show
Bug introduced by
The type \Aimeos\MShop\Common\Item\TypeRef was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
19
/**
20
 * Default impelementation of a product item.
21
 *
22
 * @package MShop
23
 * @subpackage Product
24
 */
25
class Standard
26
	extends \Aimeos\MShop\Common\Item\Base
27
	implements \Aimeos\MShop\Product\Item\Iface
28
{
29
	use Stock, Config\Traits, ListsRef\Traits, PropertyRef\Traits, TypeRef\Traits {
0 ignored issues
show
Bug introduced by
The type \Aimeos\MShop\Common\Item\ListsRef\Traits was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type \Aimeos\MShop\Common\Item\TypeRef\Traits was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type \Aimeos\MShop\Common\Item\PropertyRef\Traits was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type \Aimeos\MShop\Common\Item\Config\Traits was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
		PropertyRef\Traits::__clone as __cloneProperty;
31
		ListsRef\Traits::__clone as __cloneList;
32
		ListsRef\Traits::getName as getNameList;
33
		Stock::__clone as __cloneStock;
34
	}
35
36
37
	/**
38
	 * Initializes the item object.
39
	 *
40
	 * @param string $prefix Domain specific prefix string
41
	 * @param array $values Parameter for initializing the basic properties
42
	 */
43
	public function __construct( string $prefix, array $values = [] )
44
	{
45
		parent::__construct( $prefix, $values );
46
47
		$this->initPropertyItems( $values['.propitems'] ?? [] );
48
		$this->initListItems( $values['.listitems'] ?? [] );
49
		$this->initStockItems( $values['.stock'] ?? [] );
50
	}
51
52
53
	/**
54
	 * Creates a deep clone of all objects
55
	 */
56
	public function __clone()
57
	{
58
		parent::__clone();
59
		$this->__cloneList();
60
		$this->__cloneStock();
61
		$this->__cloneProperty();
62
	}
63
64
65
	/**
66
	 * Returns the parent product items referencing the product
67
	 *
68
	 * @return \Aimeos\Map Associative list of items implementing \Aimeos\MShop\Product\Item\Iface
69
	 */
70
	public function getParentItems() : \Aimeos\Map
71
	{
72
		return map( $this->get( '.parent' ) );
73
	}
74
75
76
	/**
77
	 * Returns the supplier items referencing the product
78
	 *
79
	 * @return \Aimeos\Map Associative list of items implementing \Aimeos\MShop\Supplier\Item\Iface
80
	 */
81
	public function getSiteItem() : ?\Aimeos\MShop\Locale\Item\Site\Iface
82
	{
83
		return $this->get( '.locale/site' );
84
	}
85
86
87
	/**
88
	 * Returns the status of the product item.
89
	 *
90
	 * @return int Status of the product item
91
	 */
92
	public function getStatus() : int
93
	{
94
		return $this->get( 'product.status', 1 );
95
	}
96
97
98
	/**
99
	 * Sets the new status of the product item.
100
	 *
101
	 * @param int $status New status of the product item
102
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
103
	 */
104
	public function setStatus( int $status ) : \Aimeos\MShop\Common\Item\Iface
105
	{
106
		return $this->set( 'product.status', $status );
107
	}
108
109
110
	/**
111
	 * Returns the code of the product item.
112
	 *
113
	 * @return string Code of the product item
114
	 */
115
	public function getCode() : string
116
	{
117
		return $this->get( 'product.code', '' );
118
	}
119
120
121
	/**
122
	 * Sets the new code of the product item.
123
	 *
124
	 * @param string $code New code of product item
125
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
126
	 */
127
	public function setCode( string $code ) : \Aimeos\MShop\Product\Item\Iface
128
	{
129
		return $this->set( 'product.code', \Aimeos\Utils::code( $code ) );
130
	}
131
132
133
	/**
134
	 * Returns the data set name assigned to the product item.
135
	 *
136
	 * @return string Data set name
137
	 */
138
	public function getDataset() : string
139
	{
140
		return $this->get( 'product.dataset', '' );
141
	}
142
143
144
	/**
145
	 * Sets a new data set name assignd to the product item.
146
	 *
147
	 * @param string $name New data set name
148
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
149
	 */
150
	public function setDataset( ?string $name ) : \Aimeos\MShop\Product\Item\Iface
151
	{
152
		return $this->set( 'product.dataset', \Aimeos\Utils::code( (string) $name ) );
153
	}
154
155
156
	/**
157
	 * Returns the label of the product item.
158
	 *
159
	 * @return string Label of the product item
160
	 */
161
	public function getLabel() : string
162
	{
163
		return $this->get( 'product.label', '' );
164
	}
165
166
167
	/**
168
	 * Sets a new label of the product item.
169
	 *
170
	 * @param string $label New label of the product item
171
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
172
	 */
173
	public function setLabel( string $label ) : \Aimeos\MShop\Product\Item\Iface
174
	{
175
		return $this->set( 'product.label', $label );
176
	}
177
178
179
	/**
180
	 * Returns the localized text type of the item or the internal label if no name is available.
181
	 *
182
	 * @param string $type Text type to be returned
183
	 * @param string|null $langId Two letter ISO Language code of the text
184
	 * @return string Specified text type or label of the item
185
	 */
186
	public function getName( string $type = 'name', ?string $langId = null ) : string
187
	{
188
		$name = $this->getNameList( $type, $langId );
189
190
		if( $type === 'url' && $name === $this->getLabel() ) {
191
			return $this->getUrl();
192
		}
193
194
		return $name;
195
	}
196
197
198
	/**
199
	 * Returns the URL segment for the product item.
200
	 *
201
	 * @return string URL segment of the product item
202
	 */
203
	public function getUrl() : string
204
	{
205
		return (string) $this->get( 'product.url' ) ?: \Aimeos\Base\Str::slug( $this->getLabel() );
206
	}
207
208
209
	/**
210
	 * Sets a new URL segment for the product.
211
	 *
212
	 * @param string|null $url New URL segment of the product item
213
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
214
	 */
215
	public function setUrl( ?string $url ) : \Aimeos\MShop\Product\Item\Iface
216
	{
217
		return $this->set( 'product.url', \Aimeos\Base\Str::slug( $url ) );
218
	}
219
220
221
	/**
222
	 * Returns the configuration values of the item
223
	 *
224
	 * @return array Configuration values
225
	 */
226
	public function getConfig() : array
227
	{
228
		return $this->get( 'product.config', [] );
229
	}
230
231
232
	/**
233
	 * Sets the configuration values of the item.
234
	 *
235
	 * @param array $config Configuration values
236
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
237
	 */
238
	public function setConfig( array $config ) : \Aimeos\MShop\Common\Item\Iface
239
	{
240
		return $this->set( 'product.config', $config );
241
	}
242
243
244
	/**
245
	 * Returns the starting point of time, in which the product is available.
246
	 *
247
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
248
	 */
249
	public function getDateStart() : ?string
250
	{
251
		$value = $this->get( 'product.datestart' );
252
		return $value ? substr( $value, 0, 19 ) : null;
253
	}
254
255
256
	/**
257
	 * Sets a new starting point of time, in which the product is available.
258
	 *
259
	 * @param string|null $date New ISO date in YYYY-MM-DD hh:mm:ss format
260
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
261
	 */
262
	public function setDateStart( ?string $date ) : \Aimeos\MShop\Common\Item\Iface
263
	{
264
		return $this->set( 'product.datestart', \Aimeos\Utils::datetime( $date ) );
265
	}
266
267
268
	/**
269
	 * Returns the ending point of time, in which the product is available.
270
	 *
271
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
272
	 */
273
	public function getDateEnd() : ?string
274
	{
275
		$value = $this->get( 'product.dateend' );
276
		return $value ? substr( $value, 0, 19 ) : null;
277
	}
278
279
280
	/**
281
	 * Sets a new ending point of time, in which the product is available.
282
	 *
283
	 * @param string|null $date New ISO date in YYYY-MM-DD hh:mm:ss format
284
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
285
	 */
286
	public function setDateEnd( ?string $date ) : \Aimeos\MShop\Common\Item\Iface
287
	{
288
		return $this->set( 'product.dateend', \Aimeos\Utils::datetime( $date ) );
289
	}
290
291
292
	/**
293
	 * Returns the rating of the item
294
	 *
295
	 * @return string Decimal value of the item rating
296
	 */
297
	public function getRating() : string
298
	{
299
		return (string) $this->get( 'product.rating', 0 );
300
	}
301
302
303
	/**
304
	 * Returns the total number of ratings for the item
305
	 *
306
	 * @return int Total number of ratings for the item
307
	 */
308
	public function getRatings() : int
309
	{
310
		return (int) $this->get( 'product.ratings', 0 );
311
	}
312
313
314
	/**
315
	 * Returns the quantity scale of the product item.
316
	 *
317
	 * @return float Quantity scale
318
	 */
319
	public function getScale() : float
320
	{
321
		return (float) $this->get( 'product.scale', 1 ) ?: 1;
322
	}
323
324
325
	/**
326
	 * Sets a new quantity scale of the product item.
327
	 *
328
	 * @param float $value New quantity scale
329
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
330
	 */
331
	public function setScale( float $value ) : \Aimeos\MShop\Product\Item\Iface
332
	{
333
		return $this->set( 'product.scale', $value > 0 ? $value : 1 );
334
	}
335
336
337
	/**
338
	 * Returns the URL target specific for that product
339
	 *
340
	 * @return string URL target specific for that product
341
	 */
342
	public function getTarget() : string
343
	{
344
		return $this->get( 'product.target', '' );
345
	}
346
347
348
	/**
349
	 * Sets a new URL target specific for that product
350
	 *
351
	 * @param string $value New URL target specific for that product
352
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
353
	 */
354
	public function setTarget( ?string $value ) : \Aimeos\MShop\Product\Item\Iface
355
	{
356
		return $this->set( 'product.target', (string) $value );
357
	}
358
359
360
	/**
361
	 * Returns the create date of the item
362
	 *
363
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
364
	 */
365
	public function getTimeCreated() : ?string
366
	{
367
		return $this->get( 'product.ctime' );
368
	}
369
370
371
	/**
372
	 * Sets the create date of the item
373
	 *
374
	 * @param string|null $value ISO date in YYYY-MM-DD hh:mm:ss format
375
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
376
	 */
377
	public function setTimeCreated( ?string $value ) : \Aimeos\MShop\Product\Item\Iface
378
	{
379
		return $this->set( 'product.ctime', \Aimeos\Utils::datetime( $value ) );
380
	}
381
382
383
	/**
384
	 * Returns the type of the product item.
385
	 * Overwritten for different default value.
386
	 *
387
	 * @return string Type of the product item
388
	 */
389
	public function getType() : string
390
	{
391
		return $this->get( 'product.type', 'default' );
392
	}
393
394
395
	/**
396
	 * Tests if the item is available based on status, time, language and currency
397
	 *
398
	 * @return bool True if available, false if not
399
	 */
400
	public function isAvailable() : bool
401
	{
402
		$date = $this->get( '.date' ) ?: date( 'Y-m-d H:i:s' );
403
404
		return parent::isAvailable() && $this->getStatus() > 0
405
			&& ( $this->getDateEnd() === null || $this->getDateEnd() > $date )
406
			&& ( $this->getDateStart() === null || $this->getDateStart() < $date || $this->getType() === 'event' );
407
	}
408
409
410
	/**
411
	 * Returns the flag if stock is available for that product.
412
	 *
413
	 * @return int "1" if product is in stock, "0" if product is out of stock
414
	 */
415
	public function inStock() : int
416
	{
417
		return (int) $this->get( 'product.instock', 0 );
418
	}
419
420
421
	/**
422
	 * Sets the flag if stock is available for that product.
423
	 *
424
	 * @param int $value "1" if product is in stock, "0" if product is out of stock
425
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
426
	 */
427
	public function setInStock( int $value ) : \Aimeos\MShop\Product\Item\Iface
428
	{
429
		return $this->set( 'product.instock', $value );
430
	}
431
432
	/**
433
	 * Returns the boost factor for that product.
434
	 *
435
	 * @return float Boost factor
436
	 */
437
	public function boost() : float
438
	{
439
		return (float) $this->get( 'product.boost', 1 );
440
	}
441
442
	/**
443
	 * Sets the boost factor for that product.
444
	 *
445
	 * @param float $value Boost factor
446
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
447
	 */
448
	public function setBoost( float $value ) : \Aimeos\MShop\Product\Item\Iface
449
	{
450
		return $this->set( 'product.boost', $value );
451
	}
452
453
454
	/*
455
	 * Sets the item values from the given array and removes that entries from the list
456
	 *
457
	 * @param array &$list Associative list of item keys and their values
458
	 * @param bool True to set private properties too, false for public only
459
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
460
	 */
461
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
462
	{
463
		$item = parent::fromArray( $list, $private );
464
465
		foreach( $list as $key => $value )
466
		{
467
			switch( $key )
468
			{
469
				case 'product.url': $item->setUrl( $value ); break;
470
				case 'product.type': $item->setType( $value ); break;
471
				case 'product.code': $item->setCode( $value ); break;
472
				case 'product.label': $item->setLabel( $value ); break;
473
				case 'product.dataset': $item->setDataset( $value ); break;
474
				case 'product.scale': $item->setScale( (float) $value ); break;
475
				case 'product.status': $item->setStatus( (int) $value ); break;
476
				case 'product.datestart': $item->setDateStart( $value ); break;
477
				case 'product.dateend': $item->setDateEnd( $value ); break;
478
				case 'product.config': $item->setConfig( $value ); break;
479
				case 'product.target': $item->setTarget( $value ); break;
480
				case 'product.ctime': $item->setTimeCreated( $value ); break;
481
				case 'product.instock': $item->setInStock( (bool) $value ); break;
482
				case 'product.boost': $item->setBoost( (float) $value ); break;
483
				default: continue 2;
484
			}
485
486
			unset( $list[$key] );
487
		}
488
489
		return $item;
490
	}
491
492
493
	/**
494
	 * Returns the item values as array.
495
	 *
496
	 * @param bool True to return private properties, false for public only
497
	 * @return array Associative list of item properties and their values
498
	 */
499
	public function toArray( bool $private = false ) : array
500
	{
501
		$list = parent::toArray( $private );
502
503
		$list['product.url'] = $this->getUrl();
504
		$list['product.type'] = $this->getType();
505
		$list['product.code'] = $this->getCode();
506
		$list['product.label'] = $this->getLabel();
507
		$list['product.status'] = $this->getStatus();
508
		$list['product.dataset'] = $this->getDataset();
509
		$list['product.datestart'] = $this->getDateStart();
510
		$list['product.dateend'] = $this->getDateEnd();
511
		$list['product.config'] = $this->getConfig();
512
		$list['product.scale'] = $this->getScale();
513
		$list['product.target'] = $this->getTarget();
514
		$list['product.ctime'] = $this->getTimeCreated();
515
		$list['product.ratings'] = $this->getRatings();
516
		$list['product.rating'] = $this->getRating();
517
		$list['product.instock'] = $this->inStock();
518
		$list['product.boost'] = $this->boost();
519
520
		return $list;
521
	}
522
}
523