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_File 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_File, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class Xhgui_Storage_File extends Xhgui_Storage_Abstract implements Xhgui_StorageInterface, |
||
| 4 | Xhgui_WatchedFunctionsStorageInterface |
||
| 5 | { |
||
| 6 | |||
| 7 | /** |
||
| 8 | * @var string |
||
| 9 | */ |
||
| 10 | protected $path = '../data/'; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | protected $prefix = 'xhgui.data'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var bool|mixed |
||
| 19 | */ |
||
| 20 | protected $separateMeta = true; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var mixed |
||
| 24 | */ |
||
| 25 | protected $dataSerializer; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var mixed |
||
| 29 | */ |
||
| 30 | protected $metaSerializer; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $watchedFunctionsPathPrefix = '../watched_functions/'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int[] |
||
| 39 | */ |
||
| 40 | protected $countCache; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var Xhgui_Storage_Filter |
||
| 44 | */ |
||
| 45 | private $filter; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Xhgui_Storage_File constructor. |
||
| 49 | * @param $config |
||
| 50 | */ |
||
| 51 | public function __construct($config) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @inheritDoc |
||
| 67 | * @param Xhgui_Storage_Filter $filter |
||
| 68 | * @param bool $projections |
||
| 69 | * @return Xhgui_Storage_ResultSet |
||
| 70 | */ |
||
| 71 | public function find(Xhgui_Storage_Filter $filter, $projections = false) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @inheritDoc |
||
| 154 | * @param Xhgui_Storage_Filter $filter |
||
| 155 | * @return int |
||
| 156 | */ |
||
| 157 | public function count(Xhgui_Storage_Filter $filter) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @inheritDoc |
||
| 168 | * @param $id |
||
| 169 | * @return mixed |
||
| 170 | */ |
||
| 171 | public function findOne($id) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @inheritDoc |
||
| 181 | * @param $id |
||
| 182 | * @return bool |
||
| 183 | */ |
||
| 184 | public function remove($id) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @inheritDoc |
||
| 199 | */ |
||
| 200 | public function drop() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @inheritDoc |
||
| 208 | * @param $match |
||
| 209 | * @param $col |
||
| 210 | * @param int $percentile |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | public function aggregate(Xhgui_Storage_Filter $filter, $col, $percentile = 1) |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * Column sorter |
||
| 246 | * |
||
| 247 | * @param $a |
||
| 248 | * @param $b |
||
| 249 | * @return int |
||
| 250 | */ |
||
| 251 | public function sortByColumn($a, $b) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Generate meta profile name from profile file name. |
||
| 309 | * |
||
| 310 | * In most cases just add .meta extension |
||
| 311 | * |
||
| 312 | * @param $file |
||
| 313 | * @return mixed |
||
| 314 | */ |
||
| 315 | protected function getMetafileNameFromProfileName($file) |
||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * @inheritDoc |
||
| 324 | * @param $data |
||
| 325 | */ |
||
| 326 | public function insert(array $data) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @inheritDoc |
||
| 338 | * @param $id |
||
| 339 | * @param $data |
||
| 340 | */ |
||
| 341 | public function update($id, array $data) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Load profile file from disk, prepare it and return parsed array |
||
| 348 | * |
||
| 349 | * @param $path |
||
| 350 | * @param bool $meta |
||
| 351 | * @return mixed |
||
| 352 | */ |
||
| 353 | protected function importFile($path, $meta = false) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @inheritDoc |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | public function getWatchedFunctions() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @inheritDoc |
||
| 407 | * @param $name |
||
| 408 | * @return bool |
||
| 409 | */ |
||
| 410 | View Code Duplication | public function addWatchedFunction($name) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @inheritDoc |
||
| 426 | * @param $id |
||
| 427 | * @param $name |
||
| 428 | * @return bool |
||
| 429 | */ |
||
| 430 | View Code Duplication | public function updateWatchedFunction($id, $name) |
|
| 443 | |||
| 444 | /** |
||
| 445 | * @inheritDoc |
||
| 446 | * @param $id |
||
| 447 | */ |
||
| 448 | public function removeWatchedFunction($id) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Parse filename and try to get request time from filename |
||
| 457 | * |
||
| 458 | * @param $fileName |
||
| 459 | * @return bool|DateTime |
||
| 460 | */ |
||
| 461 | public function getRequestTimeFromFilename($fileName) |
||
| 473 | } |
||
| 474 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: