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 HtmlReportHelper |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | const PAGE_HEADER = <<<EOT |
||
| 22 | <!doctype html> |
||
| 23 | <html lang="en"> |
||
| 24 | <head> |
||
| 25 | <meta charset="utf-8"/> |
||
| 26 | <title>Test Documentation</title> |
||
| 27 | <style> |
||
| 28 | body { |
||
| 29 | text-rendering: optimizeLegibility; |
||
| 30 | font-variant-ligatures: common-ligatures; |
||
| 31 | font-kerning: normal; |
||
| 32 | margin-left: 2em; |
||
| 33 | font-family: Verdana, Arial, sans-serif; |
||
| 34 | } |
||
| 35 | h2 { |
||
| 36 | font-family: Tahoma, Helvetica, Arial; |
||
| 37 | font-size: 1em; |
||
| 38 | } |
||
| 39 | ul { |
||
| 40 | list-style: none; |
||
| 41 | margin-bottom: 1em; |
||
| 42 | } |
||
| 43 | table { |
||
| 44 | border-collapse: collapse; |
||
| 45 | width: 100%; |
||
| 46 | } |
||
| 47 | table, th, td { |
||
| 48 | border: 1px solid black; |
||
| 49 | font-size: 0.95em; |
||
| 50 | } |
||
| 51 | th { |
||
| 52 | height: 2em; |
||
| 53 | text-align: center; |
||
| 54 | background-color:#4CAF50; |
||
| 55 | color:white; |
||
| 56 | } |
||
| 57 | td { |
||
| 58 | padding: 0.4em; |
||
| 59 | } |
||
| 60 | tr:nth-child(even) { |
||
| 61 | background-color: #f2f2f2 |
||
| 62 | } |
||
| 63 | </style> |
||
| 64 | </head> |
||
| 65 | <body> |
||
| 66 | EOT; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | const PAGE_FOOTER = <<<EOT |
||
| 72 | </body> |
||
| 73 | </html> |
||
| 74 | EOT; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param RuleInterface[] $rules |
||
| 78 | * @param string $htmlFile |
||
| 79 | */ |
||
| 80 | public static function generateHtmlFile(RuleFilterIterator $rules, $htmlFile) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $name |
||
| 96 | * @param array $tests |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | private static function createTestSuite($name, array $tests) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param array $test |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | private static function createTestCase(array $test) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param string $key |
||
| 149 | * @param array $arrays |
||
| 150 | * @param string $subKey |
||
| 151 | * @return integer |
||
| 152 | */ |
||
| 153 | View Code Duplication | private static function sumArraysKey($key, array $arrays, $subKey = null) |
|
| 163 | |||
| 164 | private static function sanitizeTags($string) |
||
| 172 | } |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: