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 |
||
11 | class InlineStyleTest extends \PHPUnit_Framework_TestCase |
||
12 | { |
||
13 | /** |
||
14 | * @var \InlineStyle\InlineStyle |
||
15 | */ |
||
16 | protected $object; |
||
17 | protected $basedir; |
||
18 | |||
19 | /** |
||
20 | * Sets up the fixture, for example, opens a network connection. |
||
21 | * This method is called before a test is executed. |
||
22 | */ |
||
23 | protected function setUp() |
||
28 | |||
29 | /** |
||
30 | * Tears down the fixture, for example, closes a network connection. |
||
31 | * This method is called after a test is executed. |
||
32 | */ |
||
33 | protected function tearDown() |
||
37 | |||
38 | public function testGetHTML() |
||
44 | |||
45 | public function testApplyStyleSheet() |
||
52 | |||
53 | public function testApplyRule() |
||
60 | |||
61 | public function testExtractStylesheets() |
||
66 | |||
67 | public function testApplyExtractedStylesheet() |
||
76 | |||
77 | View Code Duplication | public function testParseStyleSheet() |
|
84 | |||
85 | View Code Duplication | public function testParseStyleSheetWithComments() |
|
92 | |||
93 | public function testIllegalXmlUtf8Chars() |
||
98 | |||
99 | public function testGetScoreForSelector() |
||
125 | |||
126 | function testSortingParsedStylesheet() |
||
170 | |||
171 | function testApplyStylesheetObeysSpecificity() |
||
192 | |||
193 | function testDocDocumentDirectly() |
||
194 | { |
||
195 | $dom = new \DOMDocument(); |
||
196 | $dom->formatOutput = false; |
||
197 | $dom->loadHTML('<!doctype html><html><body><div></div></body></html>'); |
||
198 | |||
199 | $this->object->loadDomDocument($dom); |
||
200 | |||
201 | $this->object->applyRule('div', 'color: red'); |
||
202 | |||
203 | $this->assertEquals('<!DOCTYPE html> |
||
204 | <html><body><div style="color: red"></div></body></html> |
||
205 | ', $dom->saveHTML()); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Regression test for #14 Selectors are sometimes sorted into the wrong cascading order |
||
210 | */ |
||
211 | function testSortingOnSpecifitySameSpecificity() |
||
212 | { |
||
213 | $parsed = $this->object->parseStylesheet(<<<CSS |
||
214 | ul { |
||
215 | color: blue; |
||
216 | } |
||
217 | |||
218 | ul.class { |
||
219 | color: green; |
||
220 | } |
||
221 | |||
222 | ul { |
||
223 | color: red; |
||
224 | } |
||
225 | CSS |
||
226 | ); |
||
227 | |||
228 | $parsed = $this->object->sortSelectorsOnSpecificity($parsed); |
||
229 | |||
230 | $this->assertEquals(array( |
||
231 | array( |
||
232 | 'ul', |
||
233 | 'color: blue' |
||
234 | ), |
||
235 | array( |
||
236 | 'ul', |
||
237 | 'color: red' |
||
238 | ), |
||
239 | array( |
||
240 | 'ul.class', |
||
241 | 'color: green' |
||
242 | ), |
||
243 | ), $parsed); |
||
244 | } |
||
245 | |||
246 | function testNonWorkingPseudoSelectors() |
||
260 | |||
261 | /** |
||
262 | * Regression tests for #10 _styleToArray crashes when presented with an invalid property name |
||
263 | */ |
||
264 | function testInvalidCssProperties() |
||
265 | { |
||
266 | $this->object->applyStylesheet(<<<CSS |
||
267 | ul { |
||
268 | asohdtoairet; |
||
269 | garbage: )&%)*(%); |
||
270 | } |
||
271 | CSS |
||
272 | ); |
||
273 | } |
||
274 | |||
275 | function testRegression24() { |
||
276 | $content = '<p style="text-align:center;">Hello World!</p>'; |
||
277 | $htmldoc = new InlineStyle($content); |
||
278 | $htmldoc->applyStylesheet('p{ |
||
279 | text-align: left; |
||
280 | }'); |
||
281 | $expected = <<<HTML |
||
282 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> |
||
283 | <html><body><p style="text-align:center">Hello World!</p></body></html> |
||
284 | |||
285 | HTML; |
||
286 | |||
287 | |||
288 | $this->assertEquals($expected, $htmldoc->getHTML()); |
||
289 | $htmldoc->applyStylesheet('p{ |
||
290 | text-align: left; |
||
291 | }'); |
||
292 | $this->assertEquals($expected, $htmldoc->getHTML()); |
||
293 | } |
||
294 | |||
295 | View Code Duplication | function testMultipleStylesheets28() { |
|
312 | |||
313 | View Code Duplication | function testMediaStylesheets31() { |
|
334 | |||
335 | View Code Duplication | function testLinkedMediaStylesheets31() { |
|
354 | } |
||
355 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.