Completed
Push — master ( 2a244a...461950 )
by Derek Stephen
01:12
created

Fraction::__toString()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 8.8571
cc 5
eloc 6
nc 16
nop 0
crap 5
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 1
    private function refactor()
82
    {
83
        // 9/8 would become 1 1/8 for instance
84 1
        if ($this->numerator >= $this->denominator) {
85 1
            $this->refactorWhole();
86
        }
87 1
        $this->refactorFraction();
0 ignored issues
show
Unused Code introduced by
The call to the method Del\Phi\Fraction::refactorFraction() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
88 1
    }
89
90 1
    private function refactorWhole()
91
    {
92 1
        for ($x = $this->numerator; $x >= $this->denominator; $x = $x - $this->denominator) {
93 1
            $this->whole ++;
94 1
            $this->numerator -= $this->denominator;
95
        }
96 1
    }
97
98 1
    private function refactorFraction()
99
    {
100
        /**
101
         *  6/12 needs refactored to 1/2 for example
102
         */
103 1
    }
104
105
    /**
106
     * @return bool
107
     */
108
    public function isNegative()
109
    {
110
        return $this->negative;
111
    }
112
113
    /**
114
     * @param bool $negative
115
     * @return Fraction
116
     */
117
    public function setNegative($negative)
118
    {
119
        $this->negative = $negative;
120
        return $this;
121
    }
122
123
    /**
124
     * @return bool
125
     */
126 1
    public function isInteger()
127
    {
128 1
        return $this->numerator % $this->denominator == 0;
129
    }
130
131
    /**
132
     * @return string
133
     */
134 1
    public function __toString()
135
    {
136 1
        $this->refactor();
137
138
        // if the whole is 0, don't display it
139 1
        $whole = $this->whole == 0 ? '' : $this->whole;
140 1
        $fraction = $this->numerator > 0 ? $this->numerator.'/'.$this->denominator : '';
141 1
        $space = ($whole && $fraction) ? ' ' : '';
142
143 1
        return $whole.$space.$fraction;
144
    }
145
146
    /**
147
     * @return float
148
     */
149 1
    public function toDecimal()
150
    {
151
        /*
152
         * a divide symbol. so this is broken and will need refactoring to be accurate. ;-)
153
         */
154 1
        return $this->whole + ($this->numerator / $this->denominator);
155
    }
156
}