Completed
Push — master ( 892247...6e5012 )
by Derek Stephen
01:03
created

Fraction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 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 3
    public function __construct($whole = 1, $numerator = 1, $denominator = 1)
17
    {
18 3
        $this->whole = $whole;
19 3
        $this->numerator = $numerator;
20 3
        $this->denominator = $denominator;
21 3
    }
22
23
    /**
24
     * @return int
25
     */
26 1
    public function getWhole()
27
    {
28 1
        return $this->whole;
29
    }
30
31
    /**
32
     * @param int $whole
33
     * @return Fraction
34
     */
35 3
    public function setWhole($whole)
36
    {
37 3
        $this->whole = $whole;
38 3
        return $this;
39
    }
40
41
    /**
42
     * @return int
43
     */
44 1
    public function getNumerator()
45
    {
46 1
        return $this->numerator;
47
    }
48
49
    /**
50
     * @param int $numerator
51
     * @return Fraction
52
     */
53 3
    public function setNumerator($numerator)
54
    {
55 3
        $this->numerator = $numerator;
56 3
        return $this;
57
    }
58
59
    /**
60
     * @return int
61
     */
62 1
    public function getDenominator()
63
    {
64 1
        return $this->denominator;
65
    }
66
67
    /**
68
     * @param int $denominator
69
     * @return Fraction
70
     */
71 3
    public function setDenominator($denominator)
72
    {
73 3
        $this->denominator = $denominator;
74 3
        return $this;
75
    }
76
77
    /**
78
     * @return bool
79
     */
80
    public function isInteger()
81
    {
82
        return $this->numerator % $this->denominator == 0;
83
    }
84
85
    /**
86
     * @return string
87
     */
88 1
    public function __toString()
89
    {
90 1
        $whole = $this->whole == 0 ? '' : $this->whole.' ';
91 1
        return $whole.$this->numerator.'/'.$this->denominator;
92
    }
93
94
    /**
95
     * @return float
96
     */
97 1
    public function toDecimal()
98
    {
99
        /*
100
         * a divide symbol. so this is broken and will need refactoring to be accurate. ;-)
101
         */
102 1
        return $this->whole + ($this->numerator / $this->denominator);
103
    }
104
}