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

Customer::fieldDefinitions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 36
ccs 25
cts 25
cp 1
rs 8.8571
cc 1
eloc 30
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\Customer;
7
8
use Commercetools\Core\Model\Common\AddressCollection;
9
use Commercetools\Core\Model\Common\Resource;
10
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference;
11
use Commercetools\Core\Model\CustomField\CustomFieldObject;
12
use Commercetools\Core\Model\Common\DateTimeDecorator;
13
use Commercetools\Core\Model\Common\DateDecorator;
14
15
/**
16
 * @package Commercetools\Core\Model\Customer
17
 * @link https://dev.commercetools.com/http-api-projects-customers.html#customer
18
 * @method string getId()
19
 * @method Customer setId(string $id = null)
20
 * @method int getVersion()
21
 * @method Customer setVersion(int $version = null)
22
 * @method string getCustomerNumber()
23
 * @method Customer setCustomerNumber(string $customerNumber = null)
24
 * @method DateTimeDecorator getCreatedAt()
25
 * @method Customer setCreatedAt(\DateTime $createdAt = null)
26
 * @method DateTimeDecorator getLastModifiedAt()
27
 * @method Customer setLastModifiedAt(\DateTime $lastModifiedAt = null)
28
 * @method string getEmail()
29
 * @method Customer setEmail(string $email = null)
30
 * @method string getFirstName()
31
 * @method Customer setFirstName(string $firstName = null)
32
 * @method string getLastName()
33
 * @method Customer setLastName(string $lastName = null)
34
 * @method string getPassword()
35
 * @method Customer setPassword(string $password = null)
36
 * @method string getMiddleName()
37
 * @method Customer setMiddleName(string $middleName = null)
38
 * @method string getTitle()
39
 * @method Customer setTitle(string $title = null)
40
 * @method DateDecorator getDateOfBirth()
41
 * @method Customer setDateOfBirth(\DateTime $dateOfBirth = null)
42
 * @method string getCompanyName()
43
 * @method Customer setCompanyName(string $companyName = null)
44
 * @method string getVatId()
45
 * @method Customer setVatId(string $vatId = null)
46
 * @method AddressCollection getAddresses()
47
 * @method Customer setAddresses(AddressCollection $addresses = null)
48
 * @method string getDefaultShippingAddressId()
49
 * @method Customer setDefaultShippingAddressId(string $defaultShippingAddressId = null)
50
 * @method string getDefaultBillingAddressId()
51
 * @method Customer setDefaultBillingAddressId(string $defaultBillingAddressId = null)
52
 * @method bool getIsEmailVerified()
53
 * @method Customer setIsEmailVerified(bool $isEmailVerified = null)
54
 * @method string getExternalId()
55
 * @method Customer setExternalId(string $externalId = null)
56
 * @method CustomerGroupReference getCustomerGroup()
57
 * @method Customer setCustomerGroup(CustomerGroupReference $customerGroup = null)
58
 * @method CustomFieldObject getCustom()
59
 * @method Customer setCustom(CustomFieldObject $custom = null)
60
 * @method string getLocale()
61
 * @method CustomerReference getReference()
62
 */
63
class Customer extends Resource
64 83
{
65
    public function fieldDefinitions()
66
    {
67 83
        return [
68 83
            'id' => [static::TYPE => 'string'],
69 83
            'version' => [static::TYPE => 'int'],
70
            'customerNumber' => [static::TYPE => 'string'],
71 83
            'createdAt' => [
72 83
                static::TYPE => '\DateTime',
73
                static::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator'
74
            ],
75 83
            'lastModifiedAt' => [
76 83
                static::TYPE => '\DateTime',
77
                static::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator'
78 83
            ],
79 83
            'email' => [static::TYPE => 'string'],
80 83
            'firstName' => [static::TYPE => 'string'],
81 83
            'lastName' => [static::TYPE => 'string'],
82 83
            'password' => [static::TYPE => 'string'],
83 83
            'middleName' => [static::TYPE => 'string'],
84
            'title' => [static::TYPE => 'string'],
85 83
            'dateOfBirth' => [
86 83
                static::TYPE => '\DateTime',
87
                static::DECORATOR => '\Commercetools\Core\Model\Common\DateDecorator'
88 83
            ],
89 83
            'companyName' => [static::TYPE => 'string'],
90 83
            'vatId' => [static::TYPE => 'string'],
91 83
            'addresses' => [static::TYPE => '\Commercetools\Core\Model\Common\AddressCollection'],
92 83
            'defaultShippingAddressId' => [static::TYPE => 'string'],
93 83
            'defaultBillingAddressId' => [static::TYPE => 'string'],
94 83
            'isEmailVerified' => [static::TYPE => 'bool'],
95 83
            'externalId' => [static::TYPE => 'string'],
96 83
            'customerGroup' => [static::TYPE => '\Commercetools\Core\Model\CustomerGroup\CustomerGroupReference'],
97
            'custom' => [static::TYPE => '\Commercetools\Core\Model\CustomField\CustomFieldObject'],
98
            'locale' => [static::TYPE => 'string'],
99
        ];
100 25
    }
101
102 25
    public function getDefaultShippingAddress()
103 24
    {
104
        if (!is_null($this->getAddresses())) {
105 1
            return $this->getAddresses()->getById($this->getDefaultShippingAddressId());
106
        }
107
        return null;
108 25
    }
109
110 25
    public function getDefaultBillingAddress()
111 24
    {
112
        if (!is_null($this->getAddresses())) {
113 1
            return $this->getAddresses()->getById($this->getDefaultBillingAddressId());
114
        }
115
        return null;
116
    }
117
118
    public function setLocale($locale)
119
    {
120
        $locale = \Locale::canonicalize($locale);
121
        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...
122
123
        return $this;
124
    }
125
126
    /**
127
     * @return array
128
     */
129
    public function toJson()
130
    {
131
        $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...
132
        $data['locale'] = str_replace('_', '-', $data['locale']);
133
134
        return $data;
135
    }
136
}
137