GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

OrderDestinationIterable::deserialize()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 23
Code Lines 18

Duplication

Lines 23
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 23
loc 23
rs 8.5906
cc 6
eloc 18
nc 9
nop 1
1
<?php
2
/**
3
 * Copyright (c) 2013-2014 eBay Enterprise, Inc.
4
 *
5
 * NOTICE OF LICENSE
6
 *
7
 * This source file is subject to the Open Software License (OSL 3.0)
8
 * that is bundled with this package in the file LICENSE.md.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * @copyright   Copyright (c) 2013-2014 eBay Enterprise, Inc. (http://www.ebayenterprise.com/)
13
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
14
 */
15
16
namespace eBayEnterprise\RetailOrderManagement\Payload\Order;
17
18
use BadMethodCallException;
19
use eBayEnterprise\RetailOrderManagement\Payload\IPayload;
20
use eBayEnterprise\RetailOrderManagement\Payload\IPayloadMap;
21
use eBayEnterprise\RetailOrderManagement\Payload\ISchemaValidator;
22
use eBayEnterprise\RetailOrderManagement\Payload\IValidatorIterator;
23
use eBayEnterprise\RetailOrderManagement\Payload\PayloadFactory;
24
use eBayEnterprise\RetailOrderManagement\Payload\TIterablePayload;
25
use Psr\Log\LoggerInterface;
26
use Psr\Log\NullLogger;
27
use SPLObjectStorage;
28
29
class OrderDestinationIterable extends SPLObjectStorage implements IOrderDestinationIterable
30
{
31
    use TIterablePayload;
32
33
    const SUBPAYLOAD_XPATH = 'x:MailingAddress|x:StoreLocation|x:Email';
34
    const ROOT_NODE = 'Destinations';
35
36
    /**
37
     * @param IValidatorIterator
38
     * @param ISchemaValidator
39
     * @param IPayloadMap
40
     * @param LoggerInterface
41
     * @param IPayload
42
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
43
     */
44
    public function __construct(
45
        IValidatorIterator $validators,
46
        ISchemaValidator $schemaValidator,
0 ignored issues
show
Unused Code introduced by
The parameter $schemaValidator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
        IPayloadMap $payloadMap,
48
        LoggerInterface $logger,
49
        IPayload $parentPayload = null
50
    ) {
51
        $this->logger = $logger;
0 ignored issues
show
Documentation Bug introduced by
It seems like $logger of type object<Psr\Log\LoggerInterface> is incompatible with the declared type object<eBayEnterprise\Re...ayload\LoggerInterface> of property $logger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
        $this->validators = $validators;
53
        $this->payloadMap = $payloadMap;
54
        $this->parentPayload = $parentPayload;
55
        $this->payloadFactory = new PayloadFactory;
56
    }
57
58 View Code Duplication
    public function deserialize($serializedData)
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...
59
    {
60
        $xpath = $this->getPayloadAsXPath($serializedData);
61
        foreach ($xpath->query($this->getSubpayloadXPath()) as $subpayloadNode) {
62
            switch ($subpayloadNode->nodeName) {
63
                case 'MailingAddress':
64
                    $pl = $this->getEmptyMailingAddress();
65
                    break;
66
                case 'StoreLocation':
67
                    $pl = $this->getEmptyStoreLocation();
68
                    break;
69
                case 'Email':
70
                    $pl = $this->getEmptyEmailAddress();
71
                    break;
72
            }
73
            if (isset($pl)) {
74
                $pl->deserialize($subpayloadNode->C14N());
75
                $this->offsetSet($pl);
76
            }
77
        }
78
        $this->validate();
79
        return $this;
80
    }
81
82
    protected function serializeContents()
83
    {
84
        // Destinations must be ordered by type. Build out three separate
85
        // serializations, one for each type, to keep destinations of a certain
86
        // type together.
87
        $mailingDestinations = '';
88
        $storeLocations = '';
89
        $emailDestinations = '';
90
        foreach ($this as $destination) {
91
            if ($destination instanceof IEmailAddressDestination) {
92
                $emailDestinations .= $destination->serialize();
93
            } elseif ($destination instanceof IStoreLocation) {
94
                $storeLocations .= $destination->serialize();
95
            } else {
96
                $mailingDestinations .= $destination->serialize();
97
            }
98
        }
99
        // Concatenate the serializations together in the required order.
100
        return $mailingDestinations . $storeLocations . $emailDestinations;
101
    }
102
103
    public function getNewSubpayload()
104
    {
105
        throw new BadMethodCallException('Method not supported for this type.');
106
    }
107
108
    /**
109
     * Get a new, empty mailing address destination object.
110
     *
111
     * @return IMailingAddress
112
     */
113
    public function getEmptyMailingAddress()
114
    {
115
        return $this->buildPayloadForInterface(static::MAILING_ADDRESS_INTERFACE);
116
    }
117
118
    /**
119
     * Get a new, empty store location destination object.
120
     *
121
     * @return IStoreLocation
122
     */
123
    public function getEmptyStoreLocation()
124
    {
125
        return $this->buildPayloadForInterface(static::STORE_LOCATION_INTERFACE);
126
    }
127
128
    /**
129
     * Get a new, empty email address destination object.
130
     *
131
     * @return IEmailAddressDestination
132
     */
133
    public function getEmptyEmailAddress()
134
    {
135
        return $this->buildPayloadForInterface(static::EMAIL_ADDRESS_INTERFACE);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->buildPaylo...AIL_ADDRESS_INTERFACE); (eBayEnterprise\RetailOrd...gement\Payload\IPayload) is incompatible with the return type declared by the interface eBayEnterprise\RetailOrd...e::getEmptyEmailAddress of type eBayEnterprise\RetailOrd...EmailAddressDestination.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
136
    }
137
138
    public function getSubpayloadXPath()
139
    {
140
        return static::SUBPAYLOAD_XPATH;
141
    }
142
143
    public function getRootNodeName()
144
    {
145
        return static::ROOT_NODE;
146
    }
147
148
    public function getXmlNamespace()
149
    {
150
        return self::XML_NS;
151
    }
152
}
153