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

CustomerDraft::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
 * @created: 11.02.15, 14:23
5
 */
6
7
namespace Commercetools\Core\Model\Customer;
8
9
use Commercetools\Core\Model\Common\Context;
10
use Commercetools\Core\Model\Common\JsonObject;
11
use Commercetools\Core\Model\Common\DateTimeDecorator;
12
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference;
13
use Commercetools\Core\Model\Common\AddressCollection;
14
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft;
15
16
/**
17
 * @package Commercetools\Core\Model\Customer
18
 * @link https://dev.commercetools.com/http-api-projects-customers.html#customerdraft
19
 * @method string getCustomerNumber()
20
 * @method string getEmail()
21
 * @method string getTitle()
22
 * @method string getFirstName()
23
 * @method string getMiddleName()
24
 * @method string getLastName()
25
 * @method string getPassword()
26
 * @method string getAnonymousCartId()
27
 * @method string getExternalId()
28
 * @method CustomerDraft setCustomerNumber(string $customerNumber = null)
29
 * @method CustomerDraft setEmail(string $email = null)
30
 * @method CustomerDraft setTitle(string $title = null)
31
 * @method CustomerDraft setFirstName(string $firstName = null)
32
 * @method CustomerDraft setMiddleName(string $middleName = null)
33
 * @method CustomerDraft setLastName(string $lastName = null)
34
 * @method CustomerDraft setPassword(string $password = null)
35
 * @method CustomerDraft setAnonymousCartId(string $anonymousCartId = null)
36
 * @method CustomerDraft setExternalId(string $externalId = null)
37
 * @method DateTimeDecorator getDateOfBirth()
38
 * @method CustomerDraft setDateOfBirth(\DateTime $dateOfBirth = null)
39
 * @method string getCompanyName()
40
 * @method CustomerDraft setCompanyName(string $companyName = null)
41
 * @method string getVatId()
42
 * @method CustomerDraft setVatId(string $vatId = null)
43
 * @method bool getIsEmailVerified()
44
 * @method CustomerDraft setIsEmailVerified(bool $isEmailVerified = null)
45
 * @method CustomerGroupReference getCustomerGroup()
46
 * @method CustomerDraft setCustomerGroup(CustomerGroupReference $customerGroup = null)
47
 * @method AddressCollection getAddresses()
48
 * @method CustomerDraft setAddresses(AddressCollection $addresses = null)
49
 * @method int getDefaultShippingAddress()
50
 * @method CustomerDraft setDefaultShippingAddress(int $defaultShippingAddress = null)
51
 * @method int getDefaultBillingAddress()
52
 * @method CustomerDraft setDefaultBillingAddress(int $defaultBillingAddress = null)
53
 * @method CustomFieldObjectDraft getCustom()
54
 * @method CustomerDraft setCustom(CustomFieldObjectDraft $custom = null)
55
 * @method string getLocale()
56
 */
57
class CustomerDraft extends JsonObject
58 79
{
59
    public function fieldDefinitions()
60
    {
61 79
        return [
62 79
            'customerNumber' => [static::TYPE => 'string'],
63 79
            'email' => [static::TYPE => 'string'],
64 79
            'title' => [static::TYPE => 'string'],
65 79
            'firstName' => [static::TYPE => 'string'],
66 79
            'middleName' => [static::TYPE => 'string'],
67 79
            'lastName' => [static::TYPE => 'string'],
68 79
            'password' => [static::TYPE => 'string'],
69 79
            'anonymousCartId' => [static::TYPE => 'string'],
70
            'externalId' => [static::TYPE => 'string'],
71 79
            'dateOfBirth' => [
72 79
                static::TYPE => '\DateTime',
73
                static::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator'
74 79
            ],
75 79
            'companyName' => [static::TYPE => 'string'],
76 79
            'vatId' => [static::TYPE => 'string'],
77 79
            'isEmailVerified' => [static::TYPE => 'bool'],
78 79
            'customerGroup' => [static::TYPE => '\Commercetools\Core\Model\CustomerGroup\CustomerGroupReference'],
79 79
            'addresses' => [static::TYPE => '\Commercetools\Core\Model\Common\AddressCollection'],
80 79
            'defaultShippingAddress' => [static::TYPE => 'int'],
81 79
            'defaultBillingAddress' => [static::TYPE => 'int'],
82
            'custom' => [static::TYPE => '\Commercetools\Core\Model\CustomField\CustomFieldObjectDraft'],
83
            'locale' => [static::TYPE => 'string'],
84
        ];
85
    }
86
87
    /**
88
     * @param string $email
89
     * @param string $firstName
90
     * @param string $lastName
91
     * @param string $password
92
     * @param Context|callable $context
93 79
     * @return CustomerDraft
94
     */
95 79 View Code Duplication
    public static function ofEmailNameAndPassword($email, $firstName, $lastName, $password, $context = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96 79
    {
97 79
        $draft = static::of($context);
98 79
        return $draft->setEmail($email)
99 79
            ->setFirstName($firstName)
100
            ->setLastName($lastName)
101
            ->setPassword($password);
102
    }
103
104
    public function setLocale($locale)
105
    {
106
        $locale = \Locale::canonicalize($locale);
107
        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...
108
109
        return $this;
110
    }
111
112
    /**
113
     * @return array
114
     */
115
    public function toJson()
116
    {
117
        $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...
118
        $data['locale'] = str_replace('_', '-', $data['locale']);
119
120
        return $data;
121
    }
122
}
123