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

MyCustomerDraft::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
 * @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
use Commercetools\Core\Model\Common\DateDecorator;
16
17
/**
18
 * @package Commercetools\Core\Model\Customer
19
 * @link https://dev.commercetools.com/http-api-projects-me-profile.html#mycustomerdraft
20
 * @method string getEmail()
21
 * @method MyCustomerDraft setEmail(string $email = null)
22
 * @method string getPassword()
23
 * @method MyCustomerDraft setPassword(string $password = null)
24
 * @method string getFirstName()
25
 * @method MyCustomerDraft setFirstName(string $firstName = null)
26
 * @method string getMiddleName()
27
 * @method MyCustomerDraft setMiddleName(string $middleName = null)
28
 * @method string getLastName()
29
 * @method MyCustomerDraft setLastName(string $lastName = null)
30
 * @method string getTitle()
31
 * @method MyCustomerDraft setTitle(string $title = null)
32
 * @method DateDecorator getDateOfBirth()
33
 * @method MyCustomerDraft setDateOfBirth(\DateTime $dateOfBirth = null)
34
 * @method string getCompanyName()
35
 * @method MyCustomerDraft setCompanyName(string $companyName = null)
36
 * @method string getVatId()
37
 * @method MyCustomerDraft setVatId(string $vatId = null)
38
 * @method AddressCollection getAddresses()
39
 * @method MyCustomerDraft setAddresses(AddressCollection $addresses = null)
40
 * @method int getDefaultShippingAddress()
41
 * @method MyCustomerDraft setDefaultShippingAddress(int $defaultShippingAddress = null)
42
 * @method int getDefaultBillingAddress()
43
 * @method MyCustomerDraft setDefaultBillingAddress(int $defaultBillingAddress = null)
44
 * @method CustomFieldObjectDraft getCustom()
45
 * @method MyCustomerDraft setCustom(CustomFieldObjectDraft $custom = null)
46
 * @method string getLocale()
47
 */
48
class MyCustomerDraft extends JsonObject
49
{
50
    public function fieldDefinitions()
51
    {
52
        return [
53
            'email' => [static::TYPE => 'string'],
54
            'password' => [static::TYPE => 'string'],
55
            'firstName' => [static::TYPE => 'string'],
56
            'middleName' => [static::TYPE => 'string'],
57
            'lastName' => [static::TYPE => 'string'],
58
            'title' => [static::TYPE => 'string'],
59
            'dateOfBirth' => [
60
                static::TYPE => '\DateTime',
61
                static::DECORATOR => '\Commercetools\Core\Model\Common\DateDecorator'
62
            ],
63
            'companyName' => [static::TYPE => 'string'],
64
            'vatId' => [static::TYPE => 'string'],
65
            'addresses' => [static::TYPE => '\Commercetools\Core\Model\Common\AddressCollection'],
66
            'defaultShippingAddress' => [static::TYPE => 'int'],
67
            'defaultBillingAddress' => [static::TYPE => 'int'],
68
            'custom' => [static::TYPE => '\Commercetools\Core\Model\CustomField\CustomFieldObjectDraft'],
69
            'locale' => [static::TYPE => 'string'],
70
        ];
71
    }
72
73
    /**
74
     * @param string $email
75
     * @param string $firstName
76
     * @param string $lastName
77
     * @param string $password
78
     * @param Context|callable $context
79
     * @return CustomerDraft
80
     */
81 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...
82
    {
83
        $draft = static::of($context);
84
        return $draft->setEmail($email)
85
            ->setFirstName($firstName)
86
            ->setLastName($lastName)
87
            ->setPassword($password);
88
    }
89
90
    public function setLocale($locale)
91
    {
92
        $locale = \Locale::canonicalize($locale);
93
        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...
94
95
        return $this;
96
    }
97
98
    /**
99
     * @return array
100
     */
101
    public function toJson()
102
    {
103
        $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...
104
        $data['locale'] = str_replace('_', '-', $data['locale']);
105
106
        return $data;
107
    }
108
}
109