Completed
Push — master ( 93d184...54b3a6 )
by Derek Stephen
01:34
created

Fraction   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 84.85%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 122
ccs 28
cts 33
cp 0.8485
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getWhole() 0 4 1
A setWhole() 0 5 1
A getNumerator() 0 4 1
A setNumerator() 0 5 1
A getDenominator() 0 4 1
A setDenominator() 0 5 1
A isNegative() 0 4 1
A setNegative() 0 5 1
A isInteger() 0 4 1
A __toString() 0 5 2
A toDecimal() 0 7 1
1
<?php
2
3
namespace Del\Phi;
4
5
class Fraction
6
{
7
    /** @var int $whole */
8
    private $whole;
9
10
    /** @var int $numerator */
11
    private $numerator;
12
13
    /** @var int $denominator */
14
    private $denominator;
15
16
    /** @var bool $negative */
17
    private $negative;
18
19 4
    public function __construct($whole = 0, $numerator = 0, $denominator = 1)
20
    {
21 4
        $this->whole = $whole;
22 4
        $this->numerator = $numerator;
23 4
        $this->denominator = $denominator;
24 4
        $this->negative = false;
25 4
    }
26
27
    /**
28
     * @return int
29
     */
30 1
    public function getWhole()
31
    {
32 1
        return $this->whole;
33
    }
34
35
    /**
36
     * @param int $whole
37
     * @return Fraction
38
     */
39 4
    public function setWhole($whole)
40
    {
41 4
        $this->whole = $whole;
42 4
        return $this;
43
    }
44
45
    /**
46
     * @return int
47
     */
48 1
    public function getNumerator()
49
    {
50 1
        return $this->numerator;
51
    }
52
53
    /**
54
     * @param int $numerator
55
     * @return Fraction
56
     */
57 4
    public function setNumerator($numerator)
58
    {
59 4
        $this->numerator = $numerator;
60 4
        return $this;
61
    }
62
63
    /**
64
     * @return int
65
     */
66 1
    public function getDenominator()
67
    {
68 1
        return $this->denominator;
69
    }
70
71
    /**
72
     * @param int $denominator
73
     * @return Fraction
74
     */
75 4
    public function setDenominator($denominator)
76
    {
77 4
        $this->denominator = $denominator;
78 4
        return $this;
79
    }
80
81
    /**
82
     * @return bool
83
     */
84
    public function isNegative()
85
    {
86
        return $this->negative;
87
    }
88
89
    /**
90
     * @param bool $negative
91
     * @return Fraction
92
     */
93
    public function setNegative($negative)
94
    {
95
        $this->negative = $negative;
96
        return $this;
97
    }
98
99
    /**
100
     * @return bool
101
     */
102 1
    public function isInteger()
103
    {
104 1
        return $this->numerator % $this->denominator == 0;
105
    }
106
107
    /**
108
     * @return string
109
     */
110 1
    public function __toString()
111
    {
112 1
        $whole = $this->whole == 0 ? '' : $this->whole.' ';
113 1
        return $whole.$this->numerator.'/'.$this->denominator;
114
    }
115
116
    /**
117
     * @return float
118
     */
119 1
    public function toDecimal()
120
    {
121
        /*
122
         * a divide symbol. so this is broken and will need refactoring to be accurate. ;-)
123
         */
124 1
        return $this->whole + ($this->numerator / $this->denominator);
125
    }
126
}