Complex classes like Report 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 Report, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Report |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Hook class instance |
||
| 26 | * @var \gplcart\core\Hook $hook |
||
| 27 | */ |
||
| 28 | protected $hook; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Cache class instance |
||
| 32 | * @var \gplcart\core\Cache $cache |
||
| 33 | */ |
||
| 34 | protected $cache; |
||
| 35 | |||
| 36 | /** Module class instance |
||
| 37 | * @var \gplcart\core\Module $module |
||
| 38 | */ |
||
| 39 | protected $module; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param Hook $hook |
||
| 43 | * @param Cache $cache |
||
| 44 | * @param Module $module |
||
| 45 | */ |
||
| 46 | public function __construct(Hook $hook, Cache $cache, Module $module) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Returns an array of parsed reporting data |
||
| 55 | * @param array $handler |
||
| 56 | * @param array $settings |
||
| 57 | * @return array |
||
| 58 | * @throws OutOfRangeException |
||
| 59 | */ |
||
| 60 | public function get(array $handler, array $settings) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Clear cached report data |
||
| 102 | * @param string|null $handler_id |
||
| 103 | * @param integer|string|null $store_id |
||
| 104 | */ |
||
| 105 | public function clearCache($handler_id = null, $store_id = null) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns a handler data |
||
| 122 | * @param string $handler_id |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | public function getHandler($handler_id) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns an array of handlers |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | public function getHandlers() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Returns Google Analytics Reporting class instance |
||
| 155 | * @param array $settings |
||
| 156 | * @return \Google_Service_AnalyticsReporting |
||
| 157 | */ |
||
| 158 | protected function getService(array $settings) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Returns an object of Google Analytics service response |
||
| 166 | * @param array $settings |
||
| 167 | * @param array $handler |
||
| 168 | * @return \Google_Service_AnalyticsReporting_GetReportsResponse |
||
| 169 | * @throws OutOfRangeException |
||
| 170 | */ |
||
| 171 | public function request(array $settings, array $handler) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Sets request date range |
||
| 195 | * @param \Google_Service_AnalyticsReporting_ReportRequest $request |
||
| 196 | * @param array $handler |
||
| 197 | */ |
||
| 198 | protected function setRequestDate($request, array $handler) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Sets request metrics from a handler |
||
| 214 | * @param \Google_Service_AnalyticsReporting_ReportRequest $request |
||
| 215 | * @param array $handler |
||
| 216 | */ |
||
| 217 | protected function setRequestMetrics($request, array $handler) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Sets request sorting from a handler |
||
| 236 | * @param \Google_Service_AnalyticsReporting_ReportRequest $request |
||
| 237 | * @param array $handler |
||
| 238 | */ |
||
| 239 | protected function setRequestSorting($request, array $handler) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Sets request dimensions from a handler |
||
| 265 | * @param \Google_Service_AnalyticsReporting_ReportRequest $request |
||
| 266 | * @param array $handler |
||
| 267 | */ |
||
| 268 | protected function setRequestDimensions($request, array $handler) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Returns an array of results from the response object |
||
| 286 | * @param \Google_Service_AnalyticsReporting_GetReportsResponse $response |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | public function getResults($response) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Returns Google Client class instance |
||
| 327 | * @param array $settings |
||
| 328 | * @return \Google_Client |
||
| 329 | * @throws OutOfRangeException |
||
| 330 | */ |
||
| 331 | protected function getClient(array $settings) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Returns Google Api module instance |
||
| 346 | * @return \gplcart\modules\gapi\Main |
||
| 347 | */ |
||
| 348 | protected function getApiModule() |
||
| 354 | |||
| 355 | |||
| 356 | } |
||
| 357 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: