Completed
Push — master ( 5c1f18...461424 )
by Derek Stephen
01:05
created

Fraction::getNumerator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
    /** @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();
0 ignored issues
show
Unused Code introduced by
The call to the method Del\Phi\Fraction::refactor() 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...
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();
0 ignored issues
show
Unused Code introduced by
The call to the method Del\Phi\Fraction::refactor() 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...
80 4
        return $this;
81
    }
82
83 4
    private function refactor()
84
    {
85 4
        $remainder = $this->numerator % $this->denominator;
0 ignored issues
show
Unused Code introduced by
$remainder is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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
}