Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
16 | class QuantumViewTest extends \PHPUnit_Framework_TestCase |
||
17 | { |
||
18 | /** |
||
19 | * @var |
||
20 | */ |
||
21 | private $quantumViewMock; |
||
22 | |||
23 | public function setUp() |
||
27 | |||
28 | /** |
||
29 | * when: getSubscriptionIsCalled |
||
30 | * should: callUpsQuantumViewGetSubscriptionMethod. |
||
31 | */ |
||
32 | public function testQuantumViewGetSubscriptionMethodIsCalled() |
||
62 | |||
63 | /** |
||
64 | * when: getBookmarkIsCalled |
||
65 | * should: callUpsQuantumViewGetBookmarkMethod. |
||
66 | */ |
||
67 | public function testQuantumViewGetBookmarkMethodIsCalled() |
||
78 | |||
79 | /** |
||
80 | * when: hasBookmarkIsCalled |
||
81 | * should: callUpsQuantumViewHasBookmarkMethod. |
||
82 | */ |
||
83 | public function testQuantumViewHasBookmarkMethodIsCalled() |
||
94 | |||
95 | /** |
||
96 | * when: setBookmarkIsCalled |
||
97 | * should: callUpsQuantumViewSetBookmarkMethod. |
||
98 | */ |
||
99 | public function testQuantumViewSetBookmarkMethodIsCalled() |
||
112 | |||
113 | /** |
||
114 | * when: getRequestIsCalled |
||
115 | * should: callUpsQuantumViewGetRequestMethod. |
||
116 | */ |
||
117 | public function testQuantumViewGetRequestMethodIsCalled() |
||
130 | |||
131 | /** |
||
132 | * when: getResponseIsCalled |
||
133 | * should: callUpsGetResponseMethod. |
||
134 | */ |
||
135 | View Code Duplication | public function testQuantumViewGetResponseMethodIsCalled() |
|
148 | } |
||
149 |
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: