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

CartDraft::toJson()   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 0
crap 2
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\Cart;
7
8
use Commercetools\Core\Model\Common\Context;
9
use Commercetools\Core\Model\Common\JsonObject;
10
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft;
11
use Commercetools\Core\Model\Common\Address;
12
use Commercetools\Core\Model\ShippingMethod\ShippingMethodReference;
13
14
/**
15
 * @package Commercetools\Core\Model\Cart
16
 * @link https://dev.commercetools.com/http-api-projects-carts.html#cartdraft
17
 * @method string getCurrency()
18
 * @method string getCustomerId()
19
 * @method string getCountry()
20
 * @method string getInventoryMode()
21
 * @method CartDraft setCurrency(string $currency = null)
22
 * @method CartDraft setCustomerId(string $customerId = null)
23
 * @method CartDraft setCountry(string $country = null)
24
 * @method CartDraft setInventoryMode(string $inventoryMode = null)
25
 * @method CustomFieldObjectDraft getCustom()
26
 * @method CartDraft setCustom(CustomFieldObjectDraft $custom = null)
27
 * @method string getCustomerEmail()
28
 * @method CartDraft setCustomerEmail(string $customerEmail = null)
29
 * @method LineItemDraftCollection getLineItems()
30
 * @method CartDraft setLineItems(LineItemDraftCollection $lineItems = null)
31
 * @method Address getShippingAddress()
32
 * @method CartDraft setShippingAddress(Address $shippingAddress = null)
33
 * @method Address getBillingAddress()
34
 * @method CartDraft setBillingAddress(Address $billingAddress = null)
35
 * @method ShippingMethodReference getShippingMethod()
36
 * @method CartDraft setShippingMethod(ShippingMethodReference $shippingMethod = null)
37
 * @method CustomLineItemDraftCollection getCustomLineItems()
38
 * @method CartDraft setCustomLineItems(CustomLineItemDraftCollection $customLineItems = null)
39
 * @method string getTaxMode()
40
 * @method CartDraft setTaxMode(string $taxMode = null)
41
 * @method string getAnonymousId()
42
 * @method CartDraft setAnonymousId(string $anonymousId = null)
43
 * @method string getLocale()
44
 */
45
class CartDraft extends JsonObject
46 72
{
47
    public function fieldDefinitions()
48
    {
49 72
        return [
50 72
            'currency' => [static::TYPE => 'string'],
51 72
            'customerId' => [static::TYPE => 'string'],
52 72
            'customerEmail' => [static::TYPE => 'string'],
53 72
            'country' => [static::TYPE => 'string'],
54 72
            'inventoryMode' => [static::TYPE => 'string'],
55 72
            'lineItems' => [static::TYPE => '\Commercetools\Core\Model\Cart\LineItemDraftCollection'],
56 72
            'customLineItems' => [static::TYPE => '\Commercetools\Core\Model\Cart\CustomLineItemDraftCollection'],
57 72
            'shippingAddress' => [static::TYPE => '\Commercetools\Core\Model\Common\Address'],
58 72
            'billingAddress' => [static::TYPE => '\Commercetools\Core\Model\Common\Address'],
59 72
            'shippingMethod' => [static::TYPE => '\Commercetools\Core\Model\ShippingMethod\ShippingMethodReference'],
60 72
            'custom' => [static::TYPE => '\Commercetools\Core\Model\CustomField\CustomFieldObjectDraft'],
61 72
            'taxMode' => [static::TYPE => 'string'],
62
            'anonymousId' => [static::TYPE => 'string'],
63
            'locale' => [static::TYPE => 'string'],
64
        ];
65
    }
66
67
    /**
68
     * @param string $currency
69
     * @param Context|callable $context
70 72
     * @return CartDraft
71
     */
72 72
    public static function ofCurrency($currency, $context = null)
73 72
    {
74
        $draft = static::of($context);
75
        return $draft->setCurrency($currency);
76
    }
77
78
    public function setLocale($locale)
79
    {
80
        $locale = \Locale::canonicalize($locale);
81
        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\JsonObject as the method setLocale() does only exist in the following sub-classes of Commercetools\Core\Model\Common\JsonObject: Commercetools\Core\Model\Cart\Cart, Commercetools\Core\Model\Cart\CartDraft, Commercetools\Core\Model\Cart\MyCartDraft, Commercetools\Core\Model\Customer\Customer, Commercetools\Core\Model\Customer\CustomerDraft, Commercetools\Core\Model\Customer\MyCustomerDraft, Commercetools\Core\Model\Order\Order, Commercetools\Core\Model\Review\Review, Commercetools\Core\Model\Review\ReviewDraft, Commercetools\Core\Reque...and\CartSetLocaleAction, Commercetools\Core\Reque...CustomerSetLocaleAction, Commercetools\Core\Reque...nd\OrderSetLocaleAction, Commercetools\Core\Reque...d\ReviewSetLocaleAction. 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...
82
83
        return $this;
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    public function toJson()
90
    {
91
        $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...
92
        $data['locale'] = str_replace('_', '-', $data['locale']);
93
94
        return $data;
95
    }
96
}
97