Passed
Push — develop ( a82d29...5207c2 )
by Jens
08:45
created

CartDiscountTarget::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 2
crap 3
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\CartDiscount;
7
8
use Commercetools\Core\Model\Common\JsonObject;
9
10
/**
11
 * @package Commercetools\Core\Model\CartDiscount
12
 * @link https://dev.commercetools.com/http-api-projects-cartDiscounts.html#cartdiscounttarget
13
 * @method string getType()
14
 * @method CartDiscountTarget setType(string $type = null)
15
 * @method string getPredicate()
16
 * @method CartDiscountTarget setPredicate(string $predicate = null)
17
 */
18
class CartDiscountTarget extends JsonObject
19
{
20
    const TARGET_TYPE = '';
21
    const TYPE_LINE_ITEMS = 'lineItems';
22
23
    /**
24
     * @inheritDoc
25
     */
26 43
    public function __construct(array $data = [], $context = null)
27
    {
28 43
        if (static::TARGET_TYPE != '' && !isset($data[static::TYPE])) {
29 6
            $data[static::TYPE] = static::TARGET_TYPE;
30
        }
31 43
        parent::__construct($data, $context);
32 43
    }
33
34
35 40
    public function fieldDefinitions()
36
    {
37
        return [
38 40
            'type' => [static::TYPE => 'string'],
39 40
            'predicate' => [static::TYPE => 'string'],
40
        ];
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46 7
    public static function fromArray(array $data, $context = null)
47
    {
48 7
        if (get_called_class() === CartDiscountTarget::class && isset($data[static::TYPE])) {
1 ignored issue
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
49 6
            $className = static::targetType($data[static::TYPE]);
50 6
            if (class_exists($className)) {
51 6
                return new $className($data, $context);
52
            }
53
        }
54 1
        return new static($data, $context);
55
    }
56
57 6
    protected static function targetType($typeId)
58
    {
59
        $types = [
60 6
            LineItemsTarget::TARGET_TYPE => LineItemsTarget::class,
61 6
            CustomLineItemsTarget::TARGET_TYPE => CustomLineItemsTarget::class,
62 6
            ShippingCostTarget::TARGET_TYPE => ShippingCostTarget::class,
63 6
            MultiBuyLineItemsTarget::TARGET_TYPE => MultiBuyLineItemsTarget::class,
64
        ];
65 6
        return isset($types[$typeId]) ? $types[$typeId] : CartDiscountTarget::class;
1 ignored issue
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
66
    }
67
}
68