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 |
||
10 | final class UrlTest extends \PHPUnit_Framework_TestCase |
||
11 | { |
||
12 | /** |
||
13 | * Verify basic behavior of getUrl. |
||
14 | * |
||
15 | * @test |
||
16 | * |
||
17 | * @return void |
||
18 | */ |
||
19 | public function getUrl() |
||
23 | |||
24 | /** |
||
25 | * Verify basic behavior of getType. |
||
26 | * |
||
27 | * @test |
||
28 | * |
||
29 | * @return void |
||
30 | */ |
||
31 | public function getType() |
||
35 | |||
36 | /** |
||
37 | * Verify invalid constructor parameters cause exceptions. |
||
38 | * |
||
39 | * @param mixed $type The text identifier for the URL. |
||
40 | * @param mixed $url The full URL (including scheme, domain, and path). |
||
41 | * |
||
42 | * @test |
||
43 | * @dataProvider constructorBadData |
||
44 | * @expectedException \InvalidArgumentException |
||
45 | * |
||
46 | * @return void |
||
47 | */ |
||
48 | public function constructWithInvalidParameters($type, $url) |
||
52 | |||
53 | /** |
||
54 | * Data provider for constructWithInvalidParameters. |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | View Code Duplication | public function constructorBadData() |
|
65 | |||
66 | /** |
||
67 | * Verify invalid constructor parameters cause exceptions. |
||
68 | * |
||
69 | * @param mixed $type The text identifier for the URL. |
||
70 | * @param mixed $url The full URL (including scheme, domain, and path). |
||
71 | * |
||
72 | * @test |
||
73 | * @dataProvider constructorGoodData |
||
74 | * |
||
75 | * @return void |
||
76 | */ |
||
77 | public function constructWithValidParameters($type, $url) |
||
83 | |||
84 | /** |
||
85 | * Data provider for constructWithValidParameters. |
||
86 | * |
||
87 | * @return array |
||
88 | */ |
||
89 | View Code Duplication | public function constructorGoodData() |
|
98 | |||
99 | /** |
||
100 | * Verify basic behavior of fromArray(). |
||
101 | * |
||
102 | * @test |
||
103 | * |
||
104 | * @return void |
||
105 | */ |
||
106 | public function fromArray() |
||
112 | |||
113 | /** |
||
114 | * Verify fromArray throws when input is invalid. |
||
115 | * |
||
116 | * @test |
||
117 | * @expectedException \Exception |
||
118 | * |
||
119 | * @return void |
||
120 | */ |
||
121 | public function fromArrayWithInvalidInput() |
||
125 | |||
126 | /** |
||
127 | * Verify basic behavior of fromArrays(). |
||
128 | * |
||
129 | * @test |
||
130 | * |
||
131 | * @return void |
||
132 | */ |
||
133 | View Code Duplication | public function fromArrays() |
|
148 | |||
149 | /** |
||
150 | * Verify fromArrays throws when input is invalid. |
||
151 | * |
||
152 | * @test |
||
153 | * @expectedException \Exception |
||
154 | * |
||
155 | * @return void |
||
156 | */ |
||
157 | public function fromArraysWithInvalidInput() |
||
166 | } |
||
167 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: