Completed
Pull Request — master (#86)
by
unknown
03:35
created

FreightCharges   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 93.33 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 43.48%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 9
c 5
b 1
f 1
lcom 1
cbo 0
dl 56
loc 60
ccs 10
cts 23
cp 0.4348
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMonetaryValue() 4 4 1
A __construct() 8 8 3
A toNode() 11 11 2
A setMonetaryValue() 11 14 3

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
2
3
namespace Ups\Entity;
4
5
use DOMDocument;
6
use DOMElement;
7
use Ups\NodeInterface;
8
9 View Code Duplication
class FreightCharges implements NodeInterface
0 ignored issues
show
Duplication introduced by
This class 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...
10
{
11
    protected $monetaryValue;
12
13 1
    public function __construct($response = null)
14
    {
15 1
        if (null != $response) {
16
            if (isset($response->MonetaryValue)) {
17
                $this->setMonetaryValue($response->MonetaryValue);
18
            }
19
        }
20 1
    }
21
22
    /**
23
     * @param null|DOMDocument $document
24
     *
25
     * @return DOMElement
26
     */
27
    public function toNode(DOMDocument $document = null)
28
    {
29
        if (null === $document) {
30
            $document = new DOMDocument();
31
        }
32
33
        $node = $document->createElement('FreightCharges');
34
        $node->appendChild($document->createElement('MonetaryValue', $this->getMonetaryValue()));
35
36
        return $node;
37
    }
38
39
    /**
40
     * @return mixed
41
     */
42 1
    public function getMonetaryValue()
43
    {
44 1
        return $this->monetaryValue;
45
    }
46
47
    /**
48
     * @param $var
49
     *
50
     * @throws \Exception
51
     *
52
     * @return $this
53
     */
54 1
    public function setMonetaryValue($var)
55
    {
56 1
        $this->monetaryValue = round($var, 2); // Max 2 decimals places
57
58 1
        if ($this->monetaryValue < 0) {
59
            throw new \Exception('Freight charges cannot be negative');
60
        }
61
62 1
        if (strlen((string)$this->monetaryValue) > 15) {
63
            throw new \Exception('Value too long');
64
        }
65
66 1
        return $this;
67
    }
68
}
69