Passed
Push — master ( 436565...299e39 )
by Christopher
01:44
created

digitsTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 79
Duplicated Lines 32.91 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 26
loc 79
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setTotalDigits() 0 5 1
A checkDigitLength() 0 18 4
A checkTotalDigits() 12 12 3
A setFractionDigits() 0 5 1
A checkFractionDigits() 14 14 4
A fixFractionDigits() 0 6 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
2
3
namespace AlgoWeb\xsdTypes\Facets;
4
5
6
trait digitsTrait
7
{
8
    /**
9
     * @Exclude
10
     * @var integer Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero
11
     */
12
    private $fractionDigits = null;
13
    /**
14
     * @Exclude
15
     * @var integer Specifies the exact number of digits allowed. Must be greater than zero
16
     */
17
    private $totalDigits = null;
18
19
    public function setTotalDigits($fd)
20
    {
21
        $this->checkDigitLength($fd);
22
        $this->totalDigits = $fd;
23
    }
24
25
    private function checkDigitLength($fd)
26
    {
27
        if (!is_numeric($fd)) {
28
            throw new \InvalidArgumentException(
29
                "the provided fractionDigits for  " . __CLASS__ . " is non numeric "
30
            );
31
        }
32
        if (abs($fd) != $fd) {
33
            throw new \InvalidArgumentException(
34
                "the provided fractionDigits for  " . __CLASS__ . " must be non negative "
35
            );
36
        }
37
        if (round($fd) != $fd) {
38
            throw new \InvalidArgumentException(
39
                "the provided fractionDigits for  " . __CLASS__ . " must be a whole number "
40
            );
41
        }
42
    }
43
44 View Code Duplication
    public function checkTotalDigits($v)
1 ignored issue
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...
45
    {
46
        if (null == $this->totalDigits) {
47
            return;
48
        }
49
        $stringVal = explode(".", (string)$v);
50
        if ($this->totalDigits < strlen($stringVal[0])) {
51
            throw new \InvalidArgumentException(
52
                "the number of fractionDigits for  " . __CLASS__ . " is greater the allowed. "
53
            );
54
        }
55
    }
56
57
    public function setFractionDigits($fd)
58
    {
59
        $this->checkDigitLength($fd);
60
        $this->fractionDigits = $fd;
61
    }
62
63 View Code Duplication
    public function checkFractionDigits($v)
1 ignored issue
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...
64
    {
65
        if (null == $this->fractionDigits) {
66
            return;
67
        }
68
        $stringVal = explode(".", (string)$v);
69
        if (2 == count($stringVal)) {
70
            if ($this->fractionDigits < strlen($stringVal[1])) {
71
                throw new \InvalidArgumentException(
72
                    "the number of fractionDigits for  " . __CLASS__ . " is greater the allowed. "
73
                );
74
            }
75
        }
76
    }
77
78
    public function fixFractionDigits($v)
79
    {
80
        if (null != $this->fractionDigits) {
81
            return round($v, $this->fractionDigits);
82
        }
83
    }
84
}