Completed
Push — master ( 5bebc3...591c9a )
by Ben
9s
created

Request::buildXml()   C

Complexity

Conditions 8
Paths 26

Size

Total Lines 52
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 32
c 2
b 0
f 0
nc 26
nop 0
dl 0
loc 52
rs 6.8493

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace SimpleUPS\AddressValidate;
2
3
use \SimpleUPS\Api\MissingParameterException;
4
5
use \SimpleUPS\UPS;
6
7
/**
8
 * A request/response handler for validating a street address
9
 * @internal
10
 * @link https://www.ups.com/upsdeveloperkit/downloadresource?loc=en_US
11
 */
12
class Request extends \SimpleUPS\Api\Request
13
{
14
    private
15
        /* @var Address $address */
16
        $address;
17
18
    /**
19
     * @param null $debug
20
     */
21
    public function __construct($debug = null)
22
    {
23
        parent::__construct($debug);
24
25
        $this->responseClass = false;
26
    }
27
28
    /**
29
     * Determine which API call will be made
30
     * @return string
31
     */
32
    public function getUrl()
33
    {
34
        return $this->getDebug() ? 'https://wwwcie.ups.com/ups.app/xml/XAV' : 'https://onlinetools.ups.com/ups.app/xml/XAV';
35
    }
36
37
    /**
38
     * Build the validate address request
39
     * @return string
40
     * @throws \SimpleUPS\Api\MissingParameterException
41
     */
42
    public function buildXml()
43
    {
44
        if (serialize($this->getAddress()) == serialize(new Address())) {
45
            throw new MissingParameterException(
46
                'Address requires a Country code and a City, a State\Province code or a Postal code'
47
            );
48
        }
49
50
        if ($this->getAddress()->getCountryCode() === null) {
51
            throw new MissingParameterException('Address requires a Country code');
52
        }
53
54
        $dom = new \DomDocument('1.0');
55
        $dom->formatOutput = $this->getDebug();
56
        $dom->appendChild($addressRequest = $dom->createElement('AddressValidationRequest'));
57
        $addressRequestLang = $dom->createAttribute('xml:lang');
58
        $addressRequestLang->value = parent::getXmlLang();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getXmlLang() instead of buildXml()). Are you sure this is correct? If so, you might want to change this to $this->getXmlLang().

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...
59
        $addressRequest->appendChild($request = $dom->createElement('Request'));
60
        $request->appendChild($transactionReference = $dom->createElement('TransactionReference'));
61
        $transactionReference->appendChild($dom->createElement('CustomerContext', $this->getCustomerContext()));
62
63
        $request->appendChild($dom->createElement('RequestAction', 'XAV'));
64
        $request->appendChild($dom->createElement('RequestOption', '3'));
65
66
        $addressRequest->appendChild($address = $dom->createElement('AddressKeyFormat'));
67
68
        $address->appendChild($dom->createElement('AddressLine', $this->getAddress()->getStreet()));
69
        if ($this->getAddress()->getAddressLine2()) {
70
            $address->appendChild($dom->createElement('AddressLine', $this->getAddress()->getAddressLine2()));
71
            if ($this->getAddress()->getAddressLine3()) {
72
                $address->appendChild($dom->createElement('AddressLine', $this->getAddress()->getAddressLine3()));
73
            }
74
        }
75
        if ($this->getAddress()->getCity() != null) {
76
            $address->appendChild($dom->createElement('PoliticalDivision2', $this->getAddress()->getCity()));
77
        }
78
79
        if ($this->getAddress()->getStateProvinceCode() != null) {
80
            $address->appendChild(
81
                $dom->createElement('PoliticalDivision1', $this->getAddress()->getStateProvinceCode())
82
            );
83
        }
84
85
        if ($this->getAddress()->getPostalCode() != null) {
86
            $address->appendChild($dom->createElement('PostcodePrimaryLow', $this->getAddress()->getPostalCode()));
87
        }
88
89
        $address->appendChild($dom->createElement('CountryCode', $this->getAddress()->getCountryCode()));
90
        $xml = parent::buildAuthenticationXml() . $dom->saveXML();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (buildAuthenticationXml() instead of buildXml()). Are you sure this is correct? If so, you might want to change this to $this->buildAuthenticationXml().

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...
91
92
        return $xml;
93
    }
94
95
    /**
96
     * @return Response|\SimpleUPS\Api\Response|\SimpleXMLElement
97
     */
98 View Code Duplication
    public function sendRequest()
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...
99
    {
100
        $xml = parent::sendRequest();
101
102
        $responseClass = new \SimpleUPS\AddressValidate\Response($this->getAddress());
103
        $response = $responseClass->fromXml($xml);
0 ignored issues
show
Bug introduced by
It seems like $xml defined by parent::sendRequest() on line 100 can also be of type object<SimpleUPS\Api\Response>; however, SimpleUPS\AddressValidate\Response::fromXml() does only seem to accept object<SimpleXMLElement>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
104
105
        return $response;
106
    }
107
108
    /**
109
     * @param Address $address
110
     *
111
     * @return Request
112
     */
113
    public function setAddress(Address $address)
114
    {
115
        $this->address = $address;
116
        return $this;
117
    }
118
119
    /**
120
     * @return Address
121
     */
122
    public function getAddress()
123
    {
124
        return $this->address;
125
    }
126
}