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 string|array $settings |
||
| 57 | * @return array |
||
| 58 | * @throws OutOfRangeException |
||
| 59 | */ |
||
| 60 | public function get($handler, $settings) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Clear cached report data |
||
| 101 | * @param string|null $handler_id |
||
| 102 | * @param integer|string|null $store_id |
||
| 103 | */ |
||
| 104 | public function clearCache($handler_id = null, $store_id = null) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Returns a handler data |
||
| 121 | * @param string $handler_id |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | public function getHandler($handler_id) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns an array of handlers |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | public function getHandlers() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Returns Google Analytics Reporting class instance |
||
| 154 | * @param array $settings |
||
| 155 | * @return \Google_Service_AnalyticsReporting |
||
| 156 | */ |
||
| 157 | protected function getService(array $settings) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Returns an object of Google Analytics service response |
||
| 165 | * @param array $settings |
||
| 166 | * @param array $handler |
||
| 167 | * @return \Google_Service_AnalyticsReporting_GetReportsResponse |
||
| 168 | * @throws OutOfRangeException |
||
| 169 | */ |
||
| 170 | public function request(array $settings, array $handler) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Sets request date range |
||
| 194 | * @param \Google_Service_AnalyticsReporting_ReportRequest $request |
||
| 195 | * @param array $handler |
||
| 196 | */ |
||
| 197 | protected function setRequestDate($request, array $handler) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Sets request metrics from a handler |
||
| 213 | * @param \Google_Service_AnalyticsReporting_ReportRequest $request |
||
| 214 | * @param array $handler |
||
| 215 | */ |
||
| 216 | protected function setRequestMetrics($request, array $handler) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Sets request sorting from a handler |
||
| 235 | * @param \Google_Service_AnalyticsReporting_ReportRequest $request |
||
| 236 | * @param array $handler |
||
| 237 | */ |
||
| 238 | protected function setRequestSorting($request, array $handler) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Sets request dimensions from a handler |
||
| 264 | * @param \Google_Service_AnalyticsReporting_ReportRequest $request |
||
| 265 | * @param array $handler |
||
| 266 | */ |
||
| 267 | protected function setRequestDimensions($request, array $handler) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns an array of results from the response object |
||
| 285 | * @param \Google_Service_AnalyticsReporting_GetReportsResponse $response |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public function getResults($response) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Returns Google Client class instance |
||
| 326 | * @param array $settings |
||
| 327 | * @return \Google_Client |
||
| 328 | * @throws OutOfRangeException |
||
| 329 | */ |
||
| 330 | protected function getClient(array $settings) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Returns Google Api module instance |
||
| 345 | * @return \gplcart\modules\gapi\Main |
||
| 346 | */ |
||
| 347 | protected function getApiModule() |
||
| 353 | |||
| 354 | |||
| 355 | } |
||
| 356 |
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: