Passed
Push — master ( 299e39...60236a )
by Christopher
03:16 queued 01:34
created

DigitsTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 79
Duplicated Lines 32.91 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

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