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

digitsTrait::fixFractionDigits()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
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
}