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

MyCartDraft::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 http://dev.commercetools.com/http-api-projects-me-carts.html#mycartdraft
17
 * @method string getCurrency()
18
 * @method MyCartDraft setCurrency(string $currency = null)
19
 * @method string getCustomerEmail()
20
 * @method MyCartDraft setCustomerEmail(string $customerEmail = null)
21
 * @method string getCountry()
22
 * @method MyCartDraft setCountry(string $country = null)
23
 * @method string getInventoryMode()
24
 * @method MyCartDraft setInventoryMode(string $inventoryMode = null)
25
 * @method MyLineItemDraftCollection getLineItems()
26
 * @method MyCartDraft setLineItems(MyLineItemDraftCollection $lineItems = null)
27
 * @method Address getShippingAddress()
28
 * @method MyCartDraft setShippingAddress(Address $shippingAddress = null)
29
 * @method Address getBillingAddress()
30
 * @method MyCartDraft setBillingAddress(Address $billingAddress = null)
31
 * @method ShippingMethodReference getShippingMethod()
32
 * @method MyCartDraft setShippingMethod(ShippingMethodReference $shippingMethod = null)
33
 * @method CustomFieldObjectDraft getCustom()
34
 * @method MyCartDraft setCustom(CustomFieldObjectDraft $custom = null)
35
 * @method string getLocale()
36
 */
37
class MyCartDraft extends JsonObject
38 9
{
39
    public function fieldDefinitions()
40
    {
41 9
        return [
42 9
            'currency' => [static::TYPE => 'string'],
43 9
            'customerEmail' => [static::TYPE => 'string'],
44 9
            'country' => [static::TYPE => 'string'],
45 9
            'inventoryMode' => [static::TYPE => 'string'],
46 9
            'lineItems' => [static::TYPE => '\Commercetools\Core\Model\Cart\MyLineItemDraftCollection'],
47 9
            'shippingAddress' => [static::TYPE => '\Commercetools\Core\Model\Common\Address'],
48 9
            'billingAddress' => [static::TYPE => '\Commercetools\Core\Model\Common\Address'],
49 9
            'shippingMethod' => [static::TYPE => '\Commercetools\Core\Model\ShippingMethod\ShippingMethodReference'],
50
            'custom' => [static::TYPE => '\Commercetools\Core\Model\CustomField\CustomFieldObjectDraft'],
51
            'locale' => [static::TYPE => 'string'],
52
        ];
53
    }
54
55
    /**
56
     * @param string $currency
57
     * @param Context|callable $context
58 9
     * @return CartDraft
59
     */
60 9
    public static function ofCurrency($currency, $context = null)
61 9
    {
62
        $draft = static::of($context);
63
        return $draft->setCurrency($currency);
64
    }
65
66
    public function setLocale($locale)
67
    {
68
        $locale = \Locale::canonicalize($locale);
69
        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...
70
71
        return $this;
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    public function toJson()
78
    {
79
        $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...
80
        $data['locale'] = str_replace('_', '-', $data['locale']);
81
82
        return $data;
83
    }
84
}
85