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

Order::setLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\Order;
7
8
use Commercetools\Core\Model\Cart\CustomLineItemCollection;
9
use Commercetools\Core\Model\Cart\LineItemCollection;
10
use Commercetools\Core\Model\Cart\ShippingInfo;
11
use Commercetools\Core\Model\Common\Address;
12
use Commercetools\Core\Model\Common\Resource;
13
use Commercetools\Core\Model\Common\Money;
14
use Commercetools\Core\Model\Common\TaxedPrice;
15
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference;
16
use Commercetools\Core\Model\Cart\DiscountCodeInfoCollection;
17
use Commercetools\Core\Model\Cart\CartReference;
18
use Commercetools\Core\Model\CustomField\CustomFieldObject;
19
use Commercetools\Core\Model\Common\DateTimeDecorator;
20
use Commercetools\Core\Model\State\StateReference;
21
use Commercetools\Core\Model\Payment\PaymentInfo;
22
23
/**
24
 * @package Commercetools\Core\Model\Order
25
 * @link https://dev.commercetools.com/http-api-projects-orders.html#order
26
 * @method string getId()
27
 * @method Order setId(string $id = null)
28
 * @method int getVersion()
29
 * @method Order setVersion(int $version = null)
30
 * @method DateTimeDecorator getCreatedAt()
31
 * @method Order setCreatedAt(\DateTime $createdAt = null)
32
 * @method DateTimeDecorator getLastModifiedAt()
33
 * @method Order setLastModifiedAt(\DateTime $lastModifiedAt = null)
34
 * @method string getOrderNumber()
35
 * @method Order setOrderNumber(string $orderNumber = null)
36
 * @method string getCustomerId()
37
 * @method Order setCustomerId(string $customerId = null)
38
 * @method string getCustomerEmail()
39
 * @method Order setCustomerEmail(string $customerEmail = null)
40
 * @method LineItemCollection getLineItems()
41
 * @method Order setLineItems(LineItemCollection $lineItems = null)
42
 * @method CustomLineItemCollection getCustomLineItems()
43
 * @method Order setCustomLineItems(CustomLineItemCollection $customLineItems = null)
44
 * @method Money getTotalPrice()
45
 * @method Order setTotalPrice(Money $totalPrice = null)
46
 * @method TaxedPrice getTaxedPrice()
47
 * @method Order setTaxedPrice(TaxedPrice $taxedPrice = null)
48
 * @method Address getShippingAddress()
49
 * @method Order setShippingAddress(Address $shippingAddress = null)
50
 * @method Address getBillingAddress()
51
 * @method Order setBillingAddress(Address $billingAddress = null)
52
 * @method string getInventoryMode()
53
 * @method Order setInventoryMode(string $inventoryMode = null)
54
 * @method CustomerGroupReference getCustomerGroup()
55
 * @method Order setCustomerGroup(CustomerGroupReference $customerGroup = null)
56
 * @method string getCountry()
57
 * @method Order setCountry(string $country = null)
58
 * @method string getOrderState()
59
 * @method Order setOrderState(string $orderState = null)
60
 * @method string getShipmentState()
61
 * @method Order setShipmentState(string $shipmentState = null)
62
 * @method string getPaymentState()
63
 * @method Order setPaymentState(string $paymentState = null)
64
 * @method ShippingInfo getShippingInfo()
65
 * @method Order setShippingInfo(ShippingInfo $shippingInfo = null)
66
 * @method SyncInfoCollection getSyncInfo()
67
 * @method Order setSyncInfo(SyncInfoCollection $syncInfo = null)
68
 * @method ReturnInfoCollection getReturnInfo()
69
 * @method Order setReturnInfo(ReturnInfoCollection $returnInfo = null)
70
 * @method DiscountCodeInfoCollection getDiscountCodes()
71
 * @method Order setDiscountCodes(DiscountCodeInfoCollection $discountCodes = null)
72
 * @method int getLastMessageSequenceNumber()
73
 * @method Order setLastMessageSequenceNumber(int $lastMessageSequenceNumber = null)
74
 * @method CartReference getCart()
75
 * @method Order setCart(CartReference $cart = null)
76
 * @method CustomFieldObject getCustom()
77
 * @method Order setCustom(CustomFieldObject $custom = null)
78
 * @method StateReference getState()
79
 * @method Order setState(StateReference $state = null)
80
 * @method PaymentInfo getPaymentInfo()
81
 * @method Order setPaymentInfo(PaymentInfo $paymentInfo = null)
82
 * @method DateTimeDecorator getCompletedAt()
83
 * @method Order setCompletedAt(\DateTime $completedAt = null)
84
 * @method string getAnonymousId()
85
 * @method Order setAnonymousId(string $anonymousId = null)
86
 * @method string getLocale()
87
 * @method OrderReference getReference()
88
 */
89
class Order extends Resource
90 20
{
91
    public function fieldDefinitions()
92
    {
93 20
        return [
94 20
            'id' => [static::TYPE => 'string'],
95
            'version' => [static::TYPE => 'int'],
96 20
            'createdAt' => [
97 20
                static::TYPE => '\DateTime',
98
                static::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator'
99
            ],
100 20
            'lastModifiedAt' => [
101 20
                static::TYPE => '\DateTime',
102
                static::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator'
103
            ],
104 20
            'completedAt' => [
105 20
                static::TYPE => '\DateTime',
106
                static::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator'
107 20
            ],
108 20
            'orderNumber' => [static::TYPE => 'string'],
109 20
            'customerId' => [static::TYPE => 'string'],
110 20
            'customerEmail' => [static::TYPE => 'string'],
111 20
            'lineItems' => [static::TYPE => '\Commercetools\Core\Model\Cart\LineItemCollection'],
112 20
            'customLineItems' => [static::TYPE => '\Commercetools\Core\Model\Cart\CustomLineItemCollection'],
113 20
            'totalPrice' => [static::TYPE => '\Commercetools\Core\Model\Common\Money'],
114 20
            'taxedPrice' => [static::TYPE => '\Commercetools\Core\Model\Common\TaxedPrice'],
115 20
            'shippingAddress' => [static::TYPE => '\Commercetools\Core\Model\Common\Address'],
116 20
            'billingAddress' => [static::TYPE => '\Commercetools\Core\Model\Common\Address'],
117 20
            'inventoryMode' => [static::TYPE => 'string'],
118 20
            'customerGroup' => [static::TYPE => '\Commercetools\Core\Model\CustomerGroup\CustomerGroupReference'],
119 20
            'country' => [static::TYPE => 'string'],
120 20
            'orderState' => [static::TYPE => 'string'],
121 20
            'shipmentState' => [static::TYPE => 'string'],
122 20
            'paymentState' => [static::TYPE => 'string'],
123 20
            'shippingInfo' => [static::TYPE => '\Commercetools\Core\Model\Cart\ShippingInfo'],
124 20
            'syncInfo' => [static::TYPE => '\Commercetools\Core\Model\Order\SyncInfoCollection'],
125 20
            'returnInfo' => [static::TYPE => '\Commercetools\Core\Model\Order\ReturnInfoCollection'],
126 20
            'discountCodes' => [static::TYPE => '\Commercetools\Core\Model\Cart\DiscountCodeInfoCollection'],
127 20
            'lastMessageSequenceNumber' => [static::TYPE => 'int'],
128 20
            'cart' => [static::TYPE => '\Commercetools\Core\Model\Cart\CartReference'],
129 20
            'custom' => [static::TYPE => '\Commercetools\Core\Model\CustomField\CustomFieldObject'],
130 20
            'state' => [static::TYPE => '\Commercetools\Core\Model\State\StateReference'],
131 20
            'paymentInfo' => [static::TYPE => '\Commercetools\Core\Model\Payment\PaymentInfo'],
132
            'anonymousId' => [static::TYPE => 'string'],
133
            'locale' => [static::TYPE => 'string'],
134
        ];
135
    }
136
137
    public function setLocale($locale)
138
    {
139
        $locale = \Locale::canonicalize($locale);
140
        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...
141
142
        return $this;
143
    }
144
145
    /**
146
     * @return array
147
     */
148
    public function toJson()
149
    {
150
        $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...
151
        $data['locale'] = str_replace('_', '-', $data['locale']);
152
153
        return $data;
154
    }
155
}
156