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() |
|
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | public function query($conditions, $limit, $fields = []) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function get($id) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | */ |
||
| 129 | public function getForUrl($url, $options, $conditions = array()) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * {@inheritdoc} |
||
| 136 | */ |
||
| 137 | public function getPercentileForUrl($percentile, $url, $search = array()) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | public function getAvgsForUrl($url, $search = array()) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritdoc} |
||
| 152 | */ |
||
| 153 | public function getAll($options = array()) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * {@inheritdoc} |
||
| 218 | */ |
||
| 219 | public function insert($profile) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | public function delete($id) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritdoc} |
||
| 239 | */ |
||
| 240 | public function truncate() |
||
| 246 | } |
||
| 247 |
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.