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 |
||
| 2 | class Xhgui_Storage_PDO implements \Xhgui_StorageInterface, \Xhgui_WatchedFunctionsStorageInterface { |
||
| 3 | |||
| 4 | /** |
||
| 5 | * @var \PDO |
||
| 6 | */ |
||
| 7 | protected $connection; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * PDO constructor. |
||
| 11 | * @param $config |
||
| 12 | */ |
||
| 13 | public function __construct($config) |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param \Xhgui_Storage_Filter $filter |
||
| 26 | * @param bool $projections |
||
| 27 | * @return \Xhgui_Storage_ResultSet |
||
| 28 | */ |
||
| 29 | public function find(\Xhgui_Storage_Filter $filter, $projections = false) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param \Xhgui_Storage_Filter $filter |
||
| 58 | * @param bool $count |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | protected function getQuery(\Xhgui_Storage_Filter $filter, $count = false) { |
||
| 62 | $params = []; |
||
| 63 | |||
| 64 | if ($count === true) { |
||
| 65 | $columns = ' count(*) as c '; |
||
| 66 | } else { |
||
| 67 | $columns = ' p.*, i.*, m.*, p.profile_id as _id, main_wt as duration '; |
||
| 68 | } |
||
| 69 | |||
| 70 | $sql = " |
||
| 71 | select |
||
| 72 | $columns |
||
| 73 | from |
||
| 74 | profiles as p left join |
||
| 75 | profiles_info as i on (p.profile_id = i.id) LEFT JOIN |
||
| 76 | profiles_meta as m on (p.profile_id = m.profile_id) |
||
| 77 | "; |
||
| 78 | |||
| 79 | $where = []; |
||
| 80 | |||
| 81 | foreach([ |
||
| 82 | 'url' => 'url', |
||
| 83 | 'method' => 'method', |
||
| 84 | 'application' => 'application', |
||
| 85 | 'version' => 'version', |
||
| 86 | 'branch' => 'branch', |
||
| 87 | 'controller' => 'controller', |
||
| 88 | 'action' => 'action', |
||
| 89 | ] as $dbField => $field) { |
||
| 90 | |||
| 91 | $method = 'get'.ucfirst($field); |
||
| 92 | |||
| 93 | if ($filter->{$method}()) { |
||
| 94 | $where[] = ' '.$dbField.' = :'.$field.' '; |
||
| 95 | $params[$field] = $filter->{$method}(); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | if ($filter->getStartDate()) { |
||
| 100 | $where[] = ' request_time >= datetime(:startDate)'; |
||
| 101 | $params['startDate'] = $this->getDateTimeFromString($filter->getStartDate())->format('Y-m-d H:i:s'); |
||
|
|
|||
| 102 | } |
||
| 103 | |||
| 104 | if ($filter->getEndDate()) { |
||
| 105 | $where[] = ' request_time <= datetime(:endDate)'; |
||
| 106 | $params['endDate'] = $this->getDateTimeFromString($filter->getEndDate())->format('Y-m-d H:i:s'); |
||
| 107 | } |
||
| 108 | |||
| 109 | if (!empty($where)) { |
||
| 110 | $sql .= ' WHERE '.join(' AND ', $where); |
||
| 111 | } |
||
| 112 | |||
| 113 | if ($count === true) { |
||
| 114 | return [$sql, $params]; |
||
| 115 | } |
||
| 116 | |||
| 117 | switch ($filter->getSort()) { |
||
| 118 | case 'ct': |
||
| 119 | $sql .= ' order by main_ct'; |
||
| 120 | break; |
||
| 121 | |||
| 122 | case 'wt': |
||
| 123 | $sql .= ' order by main_wt'; |
||
| 124 | break; |
||
| 125 | |||
| 126 | case 'cpu': |
||
| 127 | $sql .= ' order by main_cpu'; |
||
| 128 | break; |
||
| 129 | |||
| 130 | case 'mu': |
||
| 131 | $sql .= ' order by main_mu'; |
||
| 132 | break; |
||
| 133 | |||
| 134 | case 'pmu': |
||
| 135 | $sql .= ' order by main_pmu'; |
||
| 136 | break; |
||
| 137 | |||
| 138 | case 'controller': |
||
| 139 | $sql .= ' order by controller'; |
||
| 140 | break; |
||
| 141 | |||
| 142 | case 'action': |
||
| 143 | $sql .= ' order by action'; |
||
| 144 | break; |
||
| 145 | |||
| 146 | case 'application': |
||
| 147 | $sql .= ' order by application'; |
||
| 148 | break; |
||
| 149 | |||
| 150 | case 'branch': |
||
| 151 | $sql .= ' order by branch'; |
||
| 152 | break; |
||
| 153 | |||
| 154 | case 'version': |
||
| 155 | $sql .= ' order by version'; |
||
| 156 | break; |
||
| 157 | |||
| 158 | case 'time': |
||
| 159 | default: |
||
| 160 | $sql .= ' order by request_time'; |
||
| 161 | break; |
||
| 162 | } |
||
| 163 | |||
| 164 | switch ($filter->getDirection()) { |
||
| 165 | case 'asc': |
||
| 166 | $sql .= ' asc '; |
||
| 167 | break; |
||
| 168 | |||
| 169 | default: |
||
| 170 | case 'desc': |
||
| 171 | $sql .= ' desc '; |
||
| 172 | break; |
||
| 173 | } |
||
| 174 | |||
| 175 | if ($filter->getPerPage()) { |
||
| 176 | $sql .= ' LIMIT :limit '; |
||
| 177 | $params['limit'] = $filter->getPerPage(); |
||
| 178 | } |
||
| 179 | |||
| 180 | if ($filter->getPage()) { |
||
| 181 | $sql .= ' OFFSET :offset '; |
||
| 182 | $params['offset'] = ($filter->getPerPage()*($filter->getPage()-1)); |
||
| 183 | } |
||
| 184 | |||
| 185 | return [$sql, $params]; |
||
| 186 | } |
||
| 187 | |||
| 188 | public function aggregate(\Xhgui_Storage_Filter $filter, $col, $percentile = 1) |
||
| 227 | |||
| 228 | |||
| 229 | public function count(\Xhgui_Storage_Filter $filter) { |
||
| 247 | |||
| 248 | public function findOne($id) |
||
| 269 | |||
| 270 | public function remove($id) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * |
||
| 291 | */ |
||
| 292 | public function drop() |
||
| 298 | |||
| 299 | public function getWatchedFunctions() |
||
| 304 | |||
| 305 | public function addWatchedFunction($name) |
||
| 315 | |||
| 316 | public function updateWatchedFunction($id, $name) |
||
| 321 | |||
| 322 | public function removeWatchedFunction($id) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param string|int $date |
||
| 330 | * @return \DateTime |
||
| 331 | */ |
||
| 332 | protected function getDateTimeFromString($date) { |
||
| 352 | |||
| 353 | public function insert(array $data) { |
||
| 356 | |||
| 357 | public function update($_id, array $data) { |
||
| 360 | } |
||
| 361 |
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: