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:
Complex classes like TestOfDecorator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TestOfDecorator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class TestOfDecorator extends UnitTestCase |
||
16 | { |
||
17 | public $mockengine; |
||
18 | public $mockcal; |
||
19 | public $decorator; |
||
20 | |||
21 | /** |
||
22 | * TestOfDecorator constructor. |
||
23 | */ |
||
24 | public function __construct() |
||
25 | { |
||
26 | parent::__construct('Test of Calendar_Decorator'); |
||
27 | } |
||
28 | |||
29 | public function setUp() |
||
30 | { |
||
31 | $this->mockengine = new Mock_Calendar_Engine($this); |
||
32 | $this->mockcal = new Mock_Calendar_Second($this); |
||
33 | $this->mockcal->setReturnValue('prevYear', 2002); |
||
34 | $this->mockcal->setReturnValue('thisYear', 2003); |
||
35 | $this->mockcal->setReturnValue('nextYear', 2004); |
||
36 | $this->mockcal->setReturnValue('prevMonth', 9); |
||
37 | $this->mockcal->setReturnValue('thisMonth', 10); |
||
38 | $this->mockcal->setReturnValue('nextMonth', 11); |
||
39 | $this->mockcal->setReturnValue('prevDay', 14); |
||
40 | $this->mockcal->setReturnValue('thisDay', 15); |
||
41 | $this->mockcal->setReturnValue('nextDay', 16); |
||
42 | $this->mockcal->setReturnValue('prevHour', 12); |
||
43 | $this->mockcal->setReturnValue('thisHour', 13); |
||
44 | $this->mockcal->setReturnValue('nextHour', 14); |
||
45 | $this->mockcal->setReturnValue('prevMinute', 29); |
||
46 | $this->mockcal->setReturnValue('thisMinute', 30); |
||
47 | $this->mockcal->setReturnValue('nextMinute', 31); |
||
48 | $this->mockcal->setReturnValue('prevSecond', 44); |
||
49 | $this->mockcal->setReturnValue('thisSecond', 45); |
||
50 | $this->mockcal->setReturnValue('nextSecond', 46); |
||
51 | $this->mockcal->setReturnValue('getEngine', $this->mockengine); |
||
52 | $this->mockcal->setReturnValue('getTimestamp', 12345); |
||
53 | } |
||
54 | |||
55 | public function tearDown() |
||
56 | { |
||
57 | unset($this->engine, $this->mockcal); |
||
58 | } |
||
59 | |||
60 | public function testPrevYear() |
||
61 | { |
||
62 | $this->mockcal->expectOnce('prevYear', ['int']); |
||
63 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
64 | $this->assertEqual(2002, $Decorator->prevYear()); |
||
65 | } |
||
66 | |||
67 | public function testThisYear() |
||
68 | { |
||
69 | $this->mockcal->expectOnce('thisYear', ['int']); |
||
70 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
71 | $this->assertEqual(2003, $Decorator->thisYear()); |
||
72 | } |
||
73 | |||
74 | public function testNextYear() |
||
75 | { |
||
76 | $this->mockcal->expectOnce('nextYear', ['int']); |
||
77 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
78 | $this->assertEqual(2004, $Decorator->nextYear()); |
||
79 | } |
||
80 | |||
81 | public function testPrevMonth() |
||
82 | { |
||
83 | $this->mockcal->expectOnce('prevMonth', ['int']); |
||
84 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
85 | $this->assertEqual(9, $Decorator->prevMonth()); |
||
86 | } |
||
87 | |||
88 | public function testThisMonth() |
||
89 | { |
||
90 | $this->mockcal->expectOnce('thisMonth', ['int']); |
||
91 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
92 | $this->assertEqual(10, $Decorator->thisMonth()); |
||
93 | } |
||
94 | |||
95 | public function testNextMonth() |
||
96 | { |
||
97 | $this->mockcal->expectOnce('nextMonth', ['int']); |
||
98 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
99 | $this->assertEqual(11, $Decorator->nextMonth()); |
||
100 | } |
||
101 | |||
102 | public function testPrevWeek() |
||
103 | { |
||
104 | $mockweek = new Mock_Calendar_Week($this); |
||
105 | $mockweek->setReturnValue('prevWeek', 1); |
||
106 | $mockweek->expectOnce('prevWeek', ['n_in_month']); |
||
107 | $Decorator = new Calendar_Decorator($mockweek); |
||
108 | $this->assertEqual(1, $Decorator->prevWeek()); |
||
109 | } |
||
110 | |||
111 | public function testThisWeek() |
||
112 | { |
||
113 | $mockweek = new Mock_Calendar_Week($this); |
||
114 | $mockweek->setReturnValue('thisWeek', 2); |
||
115 | $mockweek->expectOnce('thisWeek', ['n_in_month']); |
||
116 | $Decorator = new Calendar_Decorator($mockweek); |
||
117 | $this->assertEqual(2, $Decorator->thisWeek()); |
||
118 | } |
||
119 | |||
120 | public function testNextWeek() |
||
121 | { |
||
122 | $mockweek = new Mock_Calendar_Week($this); |
||
123 | $mockweek->setReturnValue('nextWeek', 3); |
||
124 | $mockweek->expectOnce('nextWeek', ['n_in_month']); |
||
125 | $Decorator = new Calendar_Decorator($mockweek); |
||
126 | $this->assertEqual(3, $Decorator->nextWeek()); |
||
127 | } |
||
128 | |||
129 | public function testPrevDay() |
||
130 | { |
||
131 | $this->mockcal->expectOnce('prevDay', ['int']); |
||
132 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
133 | $this->assertEqual(14, $Decorator->prevDay()); |
||
134 | } |
||
135 | |||
136 | public function testThisDay() |
||
137 | { |
||
138 | $this->mockcal->expectOnce('thisDay', ['int']); |
||
139 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
140 | $this->assertEqual(15, $Decorator->thisDay()); |
||
141 | } |
||
142 | |||
143 | public function testNextDay() |
||
144 | { |
||
145 | $this->mockcal->expectOnce('nextDay', ['int']); |
||
146 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
147 | $this->assertEqual(16, $Decorator->nextDay()); |
||
148 | } |
||
149 | |||
150 | public function testPrevHour() |
||
151 | { |
||
152 | $this->mockcal->expectOnce('prevHour', ['int']); |
||
153 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
154 | $this->assertEqual(12, $Decorator->prevHour()); |
||
155 | } |
||
156 | |||
157 | public function testThisHour() |
||
158 | { |
||
159 | $this->mockcal->expectOnce('thisHour', ['int']); |
||
160 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
161 | $this->assertEqual(13, $Decorator->thisHour()); |
||
162 | } |
||
163 | |||
164 | public function testNextHour() |
||
165 | { |
||
166 | $this->mockcal->expectOnce('nextHour', ['int']); |
||
167 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
168 | $this->assertEqual(14, $Decorator->nextHour()); |
||
169 | } |
||
170 | |||
171 | public function testPrevMinute() |
||
172 | { |
||
173 | $this->mockcal->expectOnce('prevMinute', ['int']); |
||
174 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
175 | $this->assertEqual(29, $Decorator->prevMinute()); |
||
176 | } |
||
177 | |||
178 | public function testThisMinute() |
||
179 | { |
||
180 | $this->mockcal->expectOnce('thisMinute', ['int']); |
||
181 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
182 | $this->assertEqual(30, $Decorator->thisMinute()); |
||
183 | } |
||
184 | |||
185 | public function testNextMinute() |
||
186 | { |
||
187 | $this->mockcal->expectOnce('nextMinute', ['int']); |
||
188 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
189 | $this->assertEqual(31, $Decorator->nextMinute()); |
||
190 | } |
||
191 | |||
192 | public function testPrevSecond() |
||
193 | { |
||
194 | $this->mockcal->expectOnce('prevSecond', ['int']); |
||
195 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
196 | $this->assertEqual(44, $Decorator->prevSecond()); |
||
197 | } |
||
198 | |||
199 | public function testThisSecond() |
||
200 | { |
||
201 | $this->mockcal->expectOnce('thisSecond', ['int']); |
||
202 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
203 | $this->assertEqual(45, $Decorator->thisSecond()); |
||
204 | } |
||
205 | |||
206 | public function testNextSecond() |
||
207 | { |
||
208 | $this->mockcal->expectOnce('nextSecond', ['int']); |
||
209 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
210 | $this->assertEqual(46, $Decorator->nextSecond()); |
||
211 | } |
||
212 | |||
213 | public function testGetEngine() |
||
214 | { |
||
215 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
216 | $this->assertIsA($Decorator->getEngine(), 'Mock_Calendar_Engine'); |
||
217 | } |
||
218 | |||
219 | public function testSetTimestamp() |
||
220 | { |
||
221 | $this->mockcal->expectOnce('setTimestamp', ['12345']); |
||
222 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
223 | $Decorator->setTimestamp('12345'); |
||
224 | } |
||
225 | |||
226 | public function testGetTimestamp() |
||
227 | { |
||
228 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
229 | $this->assertEqual(12345, $Decorator->getTimestamp()); |
||
230 | } |
||
231 | |||
232 | public function testSetSelected() |
||
233 | { |
||
234 | $this->mockcal->expectOnce('setSelected', [true]); |
||
235 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
236 | $Decorator->setSelected(); |
||
237 | } |
||
238 | |||
239 | public function testIsSelected() |
||
240 | { |
||
241 | $this->mockcal->setReturnValue('isSelected', true); |
||
242 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
243 | $this->assertTrue($Decorator->isSelected()); |
||
244 | } |
||
245 | |||
246 | public function testAdjust() |
||
247 | { |
||
248 | $this->mockcal->expectOnce('adjust', []); |
||
249 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
250 | $Decorator->adjust(); |
||
251 | } |
||
252 | |||
253 | public function testToArray() |
||
254 | { |
||
255 | $this->mockcal->expectOnce('toArray', [12345]); |
||
256 | $testArray = ['foo' => 'bar']; |
||
257 | $this->mockcal->setReturnValue('toArray', $testArray); |
||
258 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
259 | $this->assertEqual($testArray, $Decorator->toArray(12345)); |
||
260 | } |
||
261 | |||
262 | public function testReturnValue() |
||
263 | { |
||
264 | $this->mockcal->expectOnce('returnValue', ['a', 'b', 'c', 'd']); |
||
265 | $this->mockcal->setReturnValue('returnValue', 'foo'); |
||
266 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
267 | $this->assertEqual('foo', $Decorator->returnValue('a', 'b', 'c', 'd')); |
||
268 | } |
||
269 | |||
270 | public function testSetFirst() |
||
271 | { |
||
272 | $mockday = new Mock_Calendar_Day($this); |
||
273 | $mockday->expectOnce('setFirst', [true]); |
||
274 | $Decorator = new Calendar_Decorator($mockday); |
||
275 | $Decorator->setFirst(); |
||
276 | } |
||
277 | |||
278 | public function testSetLast() |
||
279 | { |
||
280 | $mockday = new Mock_Calendar_Day($this); |
||
281 | $mockday->expectOnce('setLast', [true]); |
||
282 | $Decorator = new Calendar_Decorator($mockday); |
||
283 | $Decorator->setLast(); |
||
284 | } |
||
285 | |||
286 | public function testIsFirst() |
||
287 | { |
||
288 | $mockday = new Mock_Calendar_Day($this); |
||
289 | $mockday->setReturnValue('isFirst', true); |
||
290 | $Decorator = new Calendar_Decorator($mockday); |
||
291 | $this->assertTrue($Decorator->isFirst()); |
||
292 | } |
||
293 | |||
294 | public function testIsLast() |
||
295 | { |
||
296 | $mockday = new Mock_Calendar_Day($this); |
||
297 | $mockday->setReturnValue('isLast', true); |
||
298 | $Decorator = new Calendar_Decorator($mockday); |
||
299 | $this->assertTrue($Decorator->isLast()); |
||
300 | } |
||
301 | |||
302 | public function testSetEmpty() |
||
303 | { |
||
304 | $mockday = new Mock_Calendar_Day($this); |
||
305 | $mockday->expectOnce('setEmpty', [true]); |
||
306 | $Decorator = new Calendar_Decorator($mockday); |
||
307 | $Decorator->setEmpty(); |
||
308 | } |
||
309 | |||
310 | public function testIsEmpty() |
||
311 | { |
||
312 | $mockday = new Mock_Calendar_Day($this); |
||
313 | $mockday->setReturnValue('isEmpty', true); |
||
314 | $Decorator = new Calendar_Decorator($mockday); |
||
315 | $this->assertTrue($Decorator->isEmpty()); |
||
316 | } |
||
317 | |||
318 | public function testBuild() |
||
319 | { |
||
320 | $testArray = ['foo' => 'bar']; |
||
321 | $this->mockcal->expectOnce('build', [$testArray]); |
||
322 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
323 | $Decorator->build($testArray); |
||
324 | } |
||
325 | |||
326 | public function testFetch() |
||
327 | { |
||
328 | $this->mockcal->expectOnce('fetch', []); |
||
329 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
330 | $Decorator->fetch(); |
||
331 | } |
||
332 | |||
333 | public function testFetchAll() |
||
334 | { |
||
335 | $this->mockcal->expectOnce('fetchAll', []); |
||
336 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
337 | $Decorator->fetchAll(); |
||
338 | } |
||
339 | |||
340 | public function testSize() |
||
345 | } |
||
346 | |||
347 | public function testIsValid() |
||
348 | { |
||
349 | $this->mockcal->expectOnce('isValid', []); |
||
350 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
351 | $Decorator->isValid(); |
||
352 | } |
||
353 | |||
354 | public function testGetValidator() |
||
355 | { |
||
356 | $this->mockcal->expectOnce('getValidator', []); |
||
357 | $Decorator = new Calendar_Decorator($this->mockcal); |
||
358 | $Decorator->getValidator(); |
||
359 | } |
||
360 | } |
||
361 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.