Completed
Pull Request — master (#80)
by
unknown
03:13
created

AddressValidationResponse   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 97
Duplicated Lines 14.43 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.29%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 14
c 3
b 2
f 0
lcom 1
cbo 2
dl 14
loc 97
ccs 33
cts 35
cp 0.9429
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A noCandidates() 7 7 2
A isValid() 0 7 2
A isAmbiguous() 7 7 2
A getAddressClassification() 0 7 2
A getCandidateAddressList() 0 11 3
A getValidatedAddress() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Ups\Entity;
2
3
use Ups\AddressValidation;
4
use Ups\Entity\AddressValidation\AVAddress;
5
use Ups\Entity\AddressValidation\AddressClassification;
6
7
class AddressValidationResponse
8
{
9
    protected $response;
10
    protected $requestAction;
11
12
    /**
13
     * AddressValidationResponse constructor.
14
     * @param \SimpleXMLElement $xmlDocument
15
     * @param $requestAction
16
     */
17 14
    public function __construct(\SimpleXMLElement $xmlDocument, $requestAction)
18
    {
19 14
        $this->response = $xmlDocument;
20 14
        $this->requestAction = $requestAction;
21 14
    }
22
23
    /**
24
     * Tells whether or not the NoCandidatesIndicator is present on the XML document.
25
     * This indicator is returned if the address is so badly formed that UPS is
26
     * unable to even offer any suggested alternatives
27
     *
28
     * @return bool
29
     */
30 5 View Code Duplication
    public function noCandidates()
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...
31 2
    {
32 5
        if (AddressValidation::REQUEST_OPTION_ADDRESS_CLASSIFICATION == $this->requestAction) {
33 3
            throw new \BadMethodCallException(__METHOD__ . ' should not be called on Address Classification only requests.');
34 2
        }
35 2
        return isset($this->response->NoCandidatesIndicator);
36 2
    }
37
38
    /**
39
     * Tells whether or not the ValidAddressIndicator is present on the XML document.
40
     * This indicator is present if provided address is valid and represents a
41
     * single, unique address in the UPS Address Validation system.
42
     * @return bool
43
     */
44 3
    public function isValid()
45
    {
46 3
        if (AddressValidation::REQUEST_OPTION_ADDRESS_CLASSIFICATION == $this->requestAction) {
47 1
            return $this->response->AddressClassification->Code > 0;
48
        }
49 2
        return isset($this->response->ValidAddressIndicator);
50
    }
51
52
    /**
53
     * Tells whether or not the AmbiguousAddressIndicator is present on the XML document.
54
     * This indicator is present when the address provided is not specific enough to
55
     * be unique to one physical location, but provides enough
56
     *
57
     * @throws \BadMethodCallException
58
     * @return bool
59
     */
60 3 View Code Duplication
    public function isAmbiguous()
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...
61
    {
62 3
        if (AddressValidation::REQUEST_OPTION_ADDRESS_CLASSIFICATION == $this->requestAction) {
63 1
            throw new \BadMethodCallException(__METHOD__ . ' should not be called on Address Classification only requests.');
64
        }
65 2
        return isset($this->response->AmbiguousAddressIndicator);
66
    }
67
68
    /**
69
     * @throws \BadMethodCallException
70
     * @return AddressClassification
71
     */
72 2
    public function getAddressClassification()
73
    {
74 2
        if ($this->requestAction < AddressValidation::REQUEST_OPTION_ADDRESS_CLASSIFICATION) {
75 1
            throw new \BadMethodCallException('Address Classification was not requested.');
76
        }
77 1
        return new AddressClassification($this->response->AddressClassification);
78
    }
79
80
    /**
81
     * @return \Ups\Entity\AddressValidation\Address[]
82
     */
83 1
    public function getCandidateAddressList()
84
    {
85 1
        if (!isset($this->response->AddressKeyFormat)) {
86
            return [];
87
        }
88 1
        $candidates = [];
89 1
        foreach ($this->response->AddressKeyFormat as $address) {
90 1
            $candidates[] = new AVAddress($address);
91 1
        }
92 1
        return $candidates;
93
    }
94
95 1
    public function getValidatedAddress()
96
    {
97 1
        if ($this->requestAction == AddressValidation::REQUEST_OPTION_ADDRESS_CLASSIFICATION) {
98
            throw new \BadMethodCallException('Only Address Classification was requested. There is no address.');
99
        }
100
101 1
        return new AVAddress($this->response->AddressKeyFormat);
102
    }
103
}
104