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 |
|
$this->refactor(); |
|
|
|
|
61
|
4 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return int |
66
|
|
|
*/ |
67
|
1 |
|
public function getDenominator() |
68
|
|
|
{ |
69
|
1 |
|
return $this->denominator; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param int $denominator |
74
|
|
|
* @return Fraction |
75
|
|
|
*/ |
76
|
4 |
|
public function setDenominator($denominator) |
77
|
|
|
{ |
78
|
4 |
|
$this->denominator = $denominator; |
79
|
4 |
|
$this->refactor(); |
|
|
|
|
80
|
4 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
private function refactor() |
84
|
|
|
{ |
85
|
4 |
|
$remainder = $this->numerator % $this->denominator; |
|
|
|
|
86
|
4 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function isNegative() |
92
|
|
|
{ |
93
|
|
|
return $this->negative; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param bool $negative |
98
|
|
|
* @return Fraction |
99
|
|
|
*/ |
100
|
|
|
public function setNegative($negative) |
101
|
|
|
{ |
102
|
|
|
$this->negative = $negative; |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
2 |
|
public function isInteger() |
110
|
|
|
{ |
111
|
2 |
|
return $this->numerator % $this->denominator == 0; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
1 |
|
public function __toString() |
118
|
|
|
{ |
119
|
1 |
|
if ($this->isInteger() && $this->numerator > 0) { |
120
|
1 |
|
return (string) ($this->whole + 1); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// if the whole is 0, don't display it |
124
|
1 |
|
$whole = $this->whole == 0 ? '' : $this->whole.' '; |
125
|
|
|
|
126
|
1 |
|
return $whole.$this->numerator.'/'.$this->denominator; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return float |
131
|
|
|
*/ |
132
|
1 |
|
public function toDecimal() |
133
|
|
|
{ |
134
|
|
|
/* |
135
|
|
|
* a divide symbol. so this is broken and will need refactoring to be accurate. ;-) |
136
|
|
|
*/ |
137
|
1 |
|
return $this->whole + ($this->numerator / $this->denominator); |
138
|
|
|
} |
139
|
|
|
} |
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:
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: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: