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 |
||
| 3 | class Xhgui_Searcher_Pdo implements Xhgui_Searcher_Interface |
||
|
|
|||
| 4 | { |
||
| 5 | /** |
||
| 6 | * @var Xhgui_Saver_Pdo |
||
| 7 | */ |
||
| 8 | private $saver; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @var PDO |
||
| 12 | */ |
||
| 13 | private $pdo; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | private $table; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param Xhgui_Saver_Pdo $saver |
||
| 22 | * @param PDO $pdo An open database connection |
||
| 23 | * @param string $table Table name where Xhgui profiles are stored |
||
| 24 | */ |
||
| 25 | public function __construct(Xhgui_Saver_Pdo $saver, PDO $pdo, $table) |
||
| 31 | |||
| 32 | /** |
||
| 33 | * {@inheritdoc} |
||
| 34 | */ |
||
| 35 | View Code Duplication | public function latest() |
|
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | public function query($conditions, $limit, $fields = []) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | View Code Duplication | public function get($id) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | public function getForUrl($url, $options, $conditions = array()) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | public function getPercentileForUrl($percentile, $url, $search = array()) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | public function getAvgsForUrl($url, $search = array()) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc} |
||
| 150 | */ |
||
| 151 | public function getAll($options = array()) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * {@inheritdoc} |
||
| 216 | */ |
||
| 217 | public function insert($profile) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritdoc} |
||
| 224 | */ |
||
| 225 | public function delete($id) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * {@inheritdoc} |
||
| 237 | */ |
||
| 238 | public function truncate() |
||
| 244 | } |
||
| 245 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.