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.

AcceptedOrderItem::deserializesShipDate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
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\OrderEvents;
17
18
use DateTime;
19
use DOMXPath;
20
use eBayEnterprise\RetailOrderManagement\Payload\IPayload;
21
use eBayEnterprise\RetailOrderManagement\Payload\IPayloadMap;
22
use eBayEnterprise\RetailOrderManagement\Payload\ISchemaValidator;
23
use eBayEnterprise\RetailOrderManagement\Payload\IValidatorIterator;
24
use eBayEnterprise\RetailOrderManagement\Payload\PayloadFactory;
25
use eBayEnterprise\RetailOrderManagement\Payload\Payment\TAmount;
26
use Psr\Log\LoggerInterface;
27
use Psr\Log\NullLogger;
28
29
class AcceptedOrderItem extends OrderItem implements IAcceptedOrderItem
30
{
31
    use TProductPrice, TItemShipping, TAmount {
32
        TAmount::serializeAmount insteadof TProductPrice;
33
        TAmount::sanitizeAmount insteadof TProductPrice;
34
    }
35
36
    public function __construct(
37
        IValidatorIterator $validators,
38
        ISchemaValidator $schemaValidator,
39
        IPayloadMap $payloadMap,
40
        LoggerInterface $logger,
41
        IPayload $parentPayload = null
42
    ) {
43
        parent::__construct($validators, $schemaValidator, $payloadMap, $logger, $parentPayload);
44
45
        $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...
46
        $this->payloadMap = $payloadMap;
47
        $this->payloadFactory = new PayloadFactory();
48
49
        $this->extractionPaths = array_merge(
50
            $this->extractionPaths,
51
            [
52
                'shipmentMethod' => 'string(x:Shipping/x:ShipmentMethod)',
53
                'shipmentMethodDisplayText' => 'string(x:Shipping/x:ShipmentMethod/@displayText)',
54
            ]
55
        );
56
        $this->optionalExtractionPaths = array_merge(
57
            $this->optionalExtractionPaths,
58
            [
59
                'amount' => 'x:Pricing/x:Amount',
60
                'unitPrice' => 'x:Pricing/x:UnitPrice',
61
                'remainder' => 'x:Pricing/x:Amount/@remainder',
62
            ]
63
        );
64
    }
65
66
    protected function deserializeExtra($serializedPayload)
67
    {
68
        $xpath = $this->getPayloadAsXPath($serializedPayload);
69
        return $this->deserializeDestination($xpath)->deserializesShipDate($xpath);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deserializ...alizesShipDate($xpath); (eBayEnterprise\RetailOrd...vents\AcceptedOrderItem) is incompatible with the return type of the parent method eBayEnterprise\RetailOrd...rItem::deserializeExtra of type eBayEnterprise\RetailOrd...gement\Payload\TPayload.

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...
70
    }
71
72
    /**
73
     * Shipping destination may be one of two payload types. If the message
74
     * includes a ShippedAddress node, use the mailing address destination type.
75
     * If the message includes a StoreFrontAddress node, use a store front
76
     * details destination type.
77
     *
78
     * @return self
79
     */
80 View Code Duplication
    protected function deserializeDestination(DOMXPath $xpath)
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...
81
    {
82
        $destinationNode = $xpath->query('x:Shipping/x:ShippedAddress|x:Shipping/x:StoreFrontAddress')->item(0);
83
        if ($destinationNode) {
84
            $mailingAddress = static::MAILING_ADDRESS_INTERFACE;
85
            $storeFront = static::STORE_FRONT_DETAILS_INTERFACE;
86
            $destination = null;
87
            switch ($destinationNode->nodeName) {
88
                case $mailingAddress::ROOT_NODE:
89
                    $destination = $this->buildPayloadForInterface($mailingAddress);
90
                    break;
91
                case $storeFront::ROOT_NODE:
92
                    $destination = $this->buildPayloadForInterface($storeFront);
93
                    break;
94
            }
95
            if ($destination) {
96
                $destination->deserialize($destinationNode->C14N());
97
                $this->setDestination($destination);
0 ignored issues
show
Compatibility introduced by
$destination of type object<eBayEnterprise\Re...ement\Payload\IPayload> is not a sub-type of object<eBayEnterprise\Re...derEvents\IDestination>. It seems like you assume a child interface of the interface eBayEnterprise\RetailOrd...gement\Payload\IPayload to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
98
            }
99
        }
100
        return $this;
101
    }
102
103
    /**
104
     * Deserialize the EstimatedShipDate to a DateTime object.
105
     *
106
     * @param DOMXPath
107
     * @return self
108
     */
109
    protected function deserializesShipDate(DOMXPath $xpath)
110
    {
111
        $estimatedShipDate = $xpath->evaluate('string(x:Shipping/x:EstimatedShipDate)');
112
        if ($estimatedShipDate) {
113
            $shipDate = new DateTime($estimatedShipDate);
114
            $this->setEstimatedShipDate($shipDate);
115
        }
116
        return $this;
117
    }
118
119
    protected function serializeContents()
120
    {
121
        return parent::serializeContents()
122
            . ($this->hasPricingData() ? $this->serializeProductPrice() : '')
123
            . ($this->hasShippingData() ? $this->serializeItemShipping() : '');
124
    }
125
126
    protected function getXmlNamespace()
127
    {
128
        return self::XML_NS;
129
    }
130
131
    /**
132
     * Check if the payload has shipping data to include in the serialization.
133
     *
134
     * @return bool
135
     */
136
    protected function hasShippingData()
137
    {
138
        return (bool) $this->getShipmentMethod();
139
    }
140
141
    /**
142
     * Check if the payload has pricing data to include in the serialization.
143
     *
144
     * @return bool
145
     */
146
    protected function hasPricingData()
147
    {
148
        return $this->getAmount() || $this->getUnitPrice();
149
    }
150
}
151