Completed
Push — develop ( 81d0f4...17c567 )
by Jens
07:44
created

Cart   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 6
c 4
b 0
f 2
lcom 0
cbo 2
dl 0
loc 71
ccs 31
cts 31
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B fieldDefinitions() 0 34 1
A getLineItemCount() 0 10 3
A setLocale() 0 7 1
A toJson() 0 7 1
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\Cart;
7
8
use Commercetools\Core\Model\Common\Address;
9
use Commercetools\Core\Model\Common\Resource;
10
use Commercetools\Core\Model\Common\Money;
11
use Commercetools\Core\Model\Common\TaxedPrice;
12
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference;
13
use Commercetools\Core\Model\CustomField\CustomFieldObject;
14
use Commercetools\Core\Model\Common\DateTimeDecorator;
15
use Commercetools\Core\Model\Payment\PaymentInfo;
16
17
/**
18
 * @package Commercetools\Core\Model\Cart
19
 * @link https://dev.commercetools.com/http-api-projects-carts.html#cart
20
 * @method string getId()
21
 * @method Cart setId(string $id = null)
22
 * @method int getVersion()
23
 * @method Cart setVersion(int $version = null)
24
 * @method DateTimeDecorator getCreatedAt()
25
 * @method Cart setCreatedAt(\DateTime $createdAt = null)
26
 * @method DateTimeDecorator getLastModifiedAt()
27
 * @method Cart setLastModifiedAt(\DateTime $lastModifiedAt = null)
28
 * @method string getCustomerId()
29
 * @method Cart setCustomerId(string $customerId = null)
30
 * @method string getCustomerEmail()
31
 * @method Cart setCustomerEmail(string $customerEmail = null)
32
 * @method LineItemCollection getLineItems()
33
 * @method Cart setLineItems(LineItemCollection $lineItems = null)
34
 * @method CustomLineItemCollection getCustomLineItems()
35
 * @method Cart setCustomLineItems(CustomLineItemCollection $customLineItems = null)
36
 * @method Money getTotalPrice()
37
 * @method Cart setTotalPrice(Money $totalPrice = null)
38
 * @method TaxedPrice getTaxedPrice()
39
 * @method Cart setTaxedPrice(TaxedPrice $taxedPrice = null)
40
 * @method string getCartState()
41
 * @method Cart setCartState(string $cartState = null)
42
 * @method Address getShippingAddress()
43
 * @method Cart setShippingAddress(Address $shippingAddress = null)
44
 * @method Address getBillingAddress()
45
 * @method Cart setBillingAddress(Address $billingAddress = null)
46
 * @method string getInventoryMode()
47
 * @method Cart setInventoryMode(string $inventoryMode = null)
48
 * @method CustomerGroupReference getCustomerGroup()
49
 * @method Cart setCustomerGroup(CustomerGroupReference $customerGroup = null)
50
 * @method string getCountry()
51
 * @method Cart setCountry(string $country = null)
52
 * @method ShippingInfo getShippingInfo()
53
 * @method Cart setShippingInfo(ShippingInfo $shippingInfo = null)
54
 * @method DiscountCodeInfoCollection getDiscountCodes()
55
 * @method Cart setDiscountCodes(DiscountCodeInfoCollection $discountCodes = null)
56
 * @method CustomFieldObject getCustom()
57
 * @method Cart setCustom(CustomFieldObject $custom = null)
58
 * @method PaymentInfo getPaymentInfo()
59
 * @method Cart setPaymentInfo(PaymentInfo $paymentInfo = null)
60
 * @method string getTaxMode()
61
 * @method Cart setTaxMode(string $taxMode = null)
62
 * @method string getAnonymousId()
63
 * @method Cart setAnonymousId(string $anonymousId = null)
64
 * @method string getLocale()
65
 * @method CartReference getReference()
66
 */
67
class Cart extends Resource
68
{
69
    const TAX_MODE_PLATFORM = 'Platform';
70
    const TAX_MODE_EXTERNAL = 'External';
71
    const TAX_MODE_DISABLED = 'Disabled';
72 85
73
    public function fieldDefinitions()
74
    {
75 85
        return [
76 85
            'id' => [static::TYPE => 'string'],
77
            'version' => [static::TYPE => 'int'],
78 85
            'createdAt' => [
79 85
                static::TYPE => '\DateTime',
80
                static::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator'
81
            ],
82 85
            'lastModifiedAt' => [
83 85
                static::TYPE => '\DateTime',
84
                static::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator'
85 85
            ],
86 85
            'customerId' => [static::TYPE => 'string'],
87 85
            'customerEmail' => [static::TYPE => 'string'],
88 85
            'lineItems' => [static::TYPE => '\Commercetools\Core\Model\Cart\LineItemCollection'],
89 85
            'customLineItems' => [static::TYPE => '\Commercetools\Core\Model\Cart\CustomLineItemCollection'],
90 85
            'totalPrice' => [static::TYPE => '\Commercetools\Core\Model\Common\Money'],
91 85
            'taxedPrice' => [static::TYPE => '\Commercetools\Core\Model\Common\TaxedPrice'],
92 85
            'cartState' => [static::TYPE => 'string'],
93 85
            'shippingAddress' => [static::TYPE => '\Commercetools\Core\Model\Common\Address'],
94 85
            'billingAddress' => [static::TYPE => '\Commercetools\Core\Model\Common\Address'],
95 85
            'inventoryMode' => [static::TYPE => 'string'],
96 85
            'customerGroup' => [static::TYPE => '\Commercetools\Core\Model\CustomerGroup\CustomerGroupReference'],
97 85
            'country' => [static::TYPE => 'string'],
98 85
            'shippingInfo' => [static::TYPE => '\Commercetools\Core\Model\Cart\ShippingInfo'],
99 85
            'discountCodes' => [static::TYPE => '\Commercetools\Core\Model\Cart\DiscountCodeInfoCollection'],
100 85
            'custom' => [static::TYPE => '\Commercetools\Core\Model\CustomField\CustomFieldObject'],
101 85
            'paymentInfo' => [static::TYPE => '\Commercetools\Core\Model\Payment\PaymentInfo'],
102 85
            'taxMode' => [static::TYPE => 'string'],
103
            'anonymousId' => [static::TYPE => 'string'],
104
            'locale' => [static::TYPE => 'string'],
105
        ];
106 4
    }
107
108 4
    public function getLineItemCount()
109 4
    {
110 3
        $count = 0;
111 2
        if ($this->getLineItems() instanceof LineItemCollection) {
112
            foreach ($this->getLineItems() as $lineItem) {
113
                $count+= $lineItem->getQuantity();
114 4
            }
115
        }
116
        return $count;
117
    }
118
119
    public function setLocale($locale)
120
    {
121
        $locale = \Locale::canonicalize($locale);
122
        parent::setLocale($locale);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Commercetools\Core\Model\Common\Resource as the method setLocale() does only exist in the following sub-classes of Commercetools\Core\Model\Common\Resource: Commercetools\Core\Model\Cart\Cart, Commercetools\Core\Model\Customer\Customer, Commercetools\Core\Model\Order\Order, Commercetools\Core\Model\Review\Review. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
123
124
        return $this;
125
    }
126
127
    /**
128
     * @return array
129
     */
130
    public function toJson()
131
    {
132
        $data = parent::toArray();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (toArray() instead of toJson()). Are you sure this is correct? If so, you might want to change this to $this->toArray().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
133
        $data['locale'] = str_replace('_', '-', $data['locale']);
134
135
        return $data;
136
    }
137
}
138