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 Xhgui_Storage_PDO 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 Xhgui_Storage_PDO, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class Xhgui_Storage_PDO extends Xhgui_Storage_Abstract implements |
||
| 6 | \Xhgui_StorageInterface, |
||
| 7 | \Xhgui_WatchedFunctionsStorageInterface |
||
| 8 | { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @var \PDO |
||
| 12 | */ |
||
| 13 | protected $connection; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * PDO constructor. |
||
| 17 | * @param $config |
||
| 18 | */ |
||
| 19 | public function __construct($config) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @inheritDoc |
||
| 33 | * @param \Xhgui_Storage_Filter $filter |
||
| 34 | * @param bool $projections |
||
| 35 | * @return \Xhgui_Storage_ResultSet |
||
| 36 | */ |
||
| 37 | public function find(\Xhgui_Storage_Filter $filter, $projections = false) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Get query that is used for both list and count |
||
| 61 | * |
||
| 62 | * @param \Xhgui_Storage_Filter $filter |
||
| 63 | * @param bool $count |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | protected function getQuery(\Xhgui_Storage_Filter $filter, $count = false) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @inheritDoc |
||
| 213 | * @param Xhgui_Storage_Filter $filter |
||
| 214 | * @param $col |
||
| 215 | * @param int $percentile |
||
| 216 | * @return array |
||
| 217 | * @throws Exception |
||
| 218 | */ |
||
| 219 | public function aggregate(\Xhgui_Storage_Filter $filter, $col, $percentile = 1) |
||
| 220 | { |
||
| 221 | $stmt = $this->connection->prepare('select |
||
| 222 | * |
||
| 223 | from |
||
| 224 | profiles_info |
||
| 225 | where |
||
| 226 | simple_url = :simple_url OR url = :url |
||
| 227 | '); |
||
| 228 | $stmt->execute(['url'=> $filter->getUrl(), 'simple_url'=> $filter->getUrl()]); |
||
| 229 | $aggregatedData = []; |
||
| 230 | while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
||
| 231 | $date = new \DateTime($row['request_time']); |
||
| 232 | $formattedDate = $date->format('Y-m-d H:i'); |
||
| 233 | View Code Duplication | if (empty($aggregatedData[$formattedDate])) { |
|
| 234 | $aggregatedData[$formattedDate] = [ |
||
| 235 | 'wall_times' => [], |
||
| 236 | 'cpu_times' => [], |
||
| 237 | 'mu_times' => [], |
||
| 238 | 'pmu_times' => [], |
||
| 239 | 'row_count' => 0 |
||
| 240 | ]; |
||
| 241 | } |
||
| 242 | |||
| 243 | $aggregatedData[$formattedDate]['wall_times'][] = $row['main_wt']; |
||
| 244 | $aggregatedData[$formattedDate]['cpu_times'][] = $row['main_cpu']; |
||
| 245 | $aggregatedData[$formattedDate]['mu_times'][] = $row['main_mu']; |
||
| 246 | $aggregatedData[$formattedDate]['pmu_times'][] = $row['main_pmu']; |
||
| 247 | $aggregatedData[$formattedDate]['row_count']++; |
||
| 248 | $aggregatedData[$formattedDate]['_id'] = $date->format('Y-m-d H:i:s'); |
||
| 249 | $aggregatedData[$formattedDate]['raw_index'] = |
||
| 250 | $aggregatedData[$formattedDate]['row_count']*($percentile/100); |
||
| 251 | } |
||
| 252 | |||
| 253 | $return = [ |
||
| 254 | 'ok' => 1, |
||
| 255 | 'result'=> array_values($aggregatedData), |
||
| 256 | ]; |
||
| 257 | return $return; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @inheritDoc |
||
| 262 | * @@param Xhgui_Storage_Filter $filter |
||
| 263 | * @return int |
||
| 264 | */ |
||
| 265 | public function count(\Xhgui_Storage_Filter $filter) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @inheritDoc |
||
| 286 | * @param $id |
||
| 287 | * @return mixed |
||
| 288 | */ |
||
| 289 | public function findOne($id) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @inheritDoc |
||
| 313 | * @param $id |
||
| 314 | */ |
||
| 315 | public function remove($id) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @inheritDoc |
||
| 336 | * Remove all data from profile tables |
||
| 337 | */ |
||
| 338 | public function drop() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @inheritDoc |
||
| 347 | * @return array |
||
| 348 | */ |
||
| 349 | public function getWatchedFunctions() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @inheritDoc |
||
| 357 | * @param $name |
||
| 358 | * @return bool |
||
| 359 | */ |
||
| 360 | public function addWatchedFunction($name) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @inheritDoc |
||
| 373 | * @param $id |
||
| 374 | * @param $name |
||
| 375 | */ |
||
| 376 | public function updateWatchedFunction($id, $name) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @inheritDoc |
||
| 384 | * @param $id |
||
| 385 | */ |
||
| 386 | public function removeWatchedFunction($id) |
||
| 391 | } |
||
| 392 |
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.