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

DigitsTrait::checkDigitLength()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 10
nc 4
nop 1
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