| Total Complexity | 53 |
| Total Lines | 473 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ClearCacheTemplate 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.
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 ClearCacheTemplate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class ClearCacheTemplate extends AdminTemplate |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Cache service. |
||
| 28 | * |
||
| 29 | * @var \Stash\Pool |
||
| 30 | */ |
||
| 31 | private $cache; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Summary of cache. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private $cacheInfo; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Cache service config. |
||
| 42 | * |
||
| 43 | * @var \Charcoal\App\Config\CacheConfig |
||
|
|
|||
| 44 | */ |
||
| 45 | private $cacheConfig; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Driver Name => Class Name. |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private $availableCacheDrivers; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Regular expression pattern to match a Stash / APC cache key. |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $apcCacheKeyPattern; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Retrieve the title of the page. |
||
| 63 | * |
||
| 64 | * @return \Charcoal\Translator\Translation|string|null |
||
| 65 | */ |
||
| 66 | public function title() |
||
| 67 | { |
||
| 68 | if ($this->title === null) { |
||
| 69 | $this->setTitle($this->translator()->translation('Cache information')); |
||
| 70 | } |
||
| 71 | |||
| 72 | return $this->title; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return \Charcoal\Admin\Widget\SidemenuWidgetInterface|null |
||
| 77 | */ |
||
| 78 | public function sidemenu() |
||
| 79 | { |
||
| 80 | if ($this->sidemenu === null) { |
||
| 81 | $this->sidemenu = $this->createSidemenu('system'); |
||
| 82 | } |
||
| 83 | |||
| 84 | return $this->sidemenu; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param boolean $force Whether to reload cache information. |
||
| 89 | * @return array |
||
| 90 | */ |
||
| 91 | public function cacheInfo($force = false) |
||
| 92 | { |
||
| 93 | if ($this->cacheInfo === null || $force === true) { |
||
| 94 | $flip = array_flip($this->availableCacheDrivers); |
||
| 95 | $driver = get_class($this->cache->getDriver()); |
||
| 96 | $cacheType = isset($flip['\\'.$driver]) ? $flip['\\'.$driver] : $driver; |
||
| 97 | |||
| 98 | $globalItems = $this->globalCacheItems(); |
||
| 99 | $this->cacheInfo = [ |
||
| 100 | 'type' => $cacheType, |
||
| 101 | 'active' => $this->cacheConfig['active'], |
||
| 102 | 'namespace' => $this->getCacheNamespace(), |
||
| 103 | 'global' => $this->globalCacheInfo(), |
||
| 104 | 'pages' => $this->pagesCacheInfo(), |
||
| 105 | 'objects' => $this->objectsCacheInfo(), |
||
| 106 | 'global_items' => $globalItems, |
||
| 107 | 'has_global_items' => !empty($globalItems), |
||
| 108 | ]; |
||
| 109 | } |
||
| 110 | |||
| 111 | return $this->cacheInfo; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | private function getCacheNamespace() |
||
| 118 | { |
||
| 119 | return $this->cache->getNamespace(); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | private function getApcNamespace() |
||
| 126 | { |
||
| 127 | return $this->cacheConfig['prefix']; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | private function getGlobalCacheKey() |
||
| 134 | { |
||
| 135 | return '/::'.$this->getCacheNamespace().'::/'; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | private function globalCacheInfo() |
||
| 142 | { |
||
| 143 | if ($this->isApc()) { |
||
| 144 | $cacheKey = $this->getGlobalCacheKey(); |
||
| 145 | return $this->apcCacheInfo($cacheKey); |
||
| 146 | } else { |
||
| 147 | return [ |
||
| 148 | 'num_entries' => 0, |
||
| 149 | 'total_size' => 0, |
||
| 150 | 'average_size' => 0, |
||
| 151 | 'total_hits' => 0, |
||
| 152 | 'average_hits' => 0, |
||
| 153 | ]; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | private function globalCacheItems() |
||
| 161 | { |
||
| 162 | if ($this->isApc()) { |
||
| 163 | $cacheKey = $this->getGlobalCacheKey(); |
||
| 164 | return $this->apcCacheItems($cacheKey); |
||
| 165 | } else { |
||
| 166 | return []; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | private function getPagesCacheKey() |
||
| 174 | { |
||
| 175 | return '/::'.$this->getCacheNamespace().'::request::|::'.$this->getCacheNamespace().'::template::/'; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | private function pagesCacheInfo() |
||
| 182 | { |
||
| 183 | if ($this->isApc()) { |
||
| 184 | $cacheKey = $this->getPagesCacheKey(); |
||
| 185 | return $this->apcCacheInfo($cacheKey); |
||
| 186 | } else { |
||
| 187 | return [ |
||
| 188 | 'num_entries' => 0, |
||
| 189 | 'total_size' => 0, |
||
| 190 | 'average_size' => 0, |
||
| 191 | 'total_hits' => 0, |
||
| 192 | 'average_hits' => 0, |
||
| 193 | ]; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | private function pagesCacheItems() |
||
| 201 | { |
||
| 202 | if ($this->isApc()) { |
||
| 203 | $cacheKey = $this->getPagesCacheKey(); |
||
| 204 | return $this->apcCacheItems($cacheKey); |
||
| 205 | } else { |
||
| 206 | return []; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | private function getObjectsCacheKey() |
||
| 214 | { |
||
| 215 | return '/::'.$this->getCacheNamespace().'::object::|::'.$this->getCacheNamespace().'::metadata::/'; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | private function objectsCacheInfo() |
||
| 222 | { |
||
| 223 | if ($this->isApc()) { |
||
| 224 | $cacheKey = $this->getObjectsCacheKey(); |
||
| 225 | return $this->apcCacheInfo($cacheKey); |
||
| 226 | } else { |
||
| 227 | return [ |
||
| 228 | 'num_entries' => 0, |
||
| 229 | 'total_size' => 0, |
||
| 230 | 'average_size' => 0, |
||
| 231 | 'total_hits' => 0, |
||
| 232 | 'average_hits' => 0, |
||
| 233 | ]; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | private function objectsCacheItems() |
||
| 241 | { |
||
| 242 | if ($this->isApc()) { |
||
| 243 | $cacheKey = $this->getObjectsCacheKey(); |
||
| 244 | return $this->apcCacheItems($cacheKey); |
||
| 245 | } else { |
||
| 246 | return []; |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param string $key The cache key to look at. |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | private function apcCacheInfo($key) |
||
| 255 | { |
||
| 256 | $iter = $this->createApcIterator($key); |
||
| 257 | |||
| 258 | $numEntries = 0; |
||
| 259 | $sizeTotal = 0; |
||
| 260 | $hitsTotal = 0; |
||
| 261 | $ttlTotal = 0; |
||
| 262 | foreach ($iter as $item) { |
||
| 263 | $numEntries++; |
||
| 264 | $sizeTotal += $item['mem_size']; |
||
| 265 | $hitsTotal += $item['num_hits']; |
||
| 266 | $ttlTotal += $item['ttl']; |
||
| 267 | } |
||
| 268 | $sizeAvg = $numEntries ? ($sizeTotal / $numEntries) : 0; |
||
| 269 | $hitsAvg = $numEntries ? ($hitsTotal / $numEntries) : 0; |
||
| 270 | return [ |
||
| 271 | 'num_entries' => $numEntries, |
||
| 272 | 'total_size' => $this->formatBytes($sizeTotal), |
||
| 273 | 'average_size' => $this->formatBytes($sizeAvg), |
||
| 274 | 'total_hits' => $hitsTotal, |
||
| 275 | 'average_hits' => $hitsAvg, |
||
| 276 | ]; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $key The cache key to look at. |
||
| 281 | * @return array|\Generator |
||
| 282 | */ |
||
| 283 | private function apcCacheItems($key) |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param string $key The cache item key to load. |
||
| 305 | * @throws RuntimeException If the APC Iterator class is missing. |
||
| 306 | * @return \APCIterator|\APCUIterator|null |
||
| 307 | */ |
||
| 308 | private function createApcIterator($key) |
||
| 309 | { |
||
| 310 | if (class_exists('\\APCUIterator', false)) { |
||
| 311 | return new \APCUIterator($key); |
||
| 312 | } elseif (class_exists('\\APCIterator', false)) { |
||
| 313 | return new \APCIterator('user', $key); |
||
| 314 | } else { |
||
| 315 | throw new RuntimeException('Cache uses APC but no iterator could be found.'); |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Determine if Charcoal has cache statistics. |
||
| 321 | * |
||
| 322 | * @return boolean |
||
| 323 | */ |
||
| 324 | public function hasStats() |
||
| 325 | { |
||
| 326 | return $this->isApc(); |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Determine if Charcoal is using the APC driver. |
||
| 331 | * |
||
| 332 | * @return boolean |
||
| 333 | */ |
||
| 334 | public function isApc() |
||
| 335 | { |
||
| 336 | return is_a($this->cache->getDriver(), Apc::class); |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Determine if Charcoal is using the Memcache driver. |
||
| 341 | * |
||
| 342 | * @return boolean |
||
| 343 | */ |
||
| 344 | public function isMemcache() |
||
| 345 | { |
||
| 346 | return is_a($this->cache->getDriver(), Memcache::class); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Determine if Charcoal is using the Ephemeral driver. |
||
| 351 | * |
||
| 352 | * @return boolean |
||
| 353 | */ |
||
| 354 | public function isMemory() |
||
| 355 | { |
||
| 356 | return is_a($this->cache->getDriver(), Ephemeral::class); |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Get the RegExp pattern to match a Stash / APC cache key. |
||
| 361 | * |
||
| 362 | * Breakdown: |
||
| 363 | * - `apcID`: Installation ID |
||
| 364 | * - `apcNS`: Optional. Application Key or Installation ID |
||
| 365 | * - `stashNS`: Stash Segment |
||
| 366 | * - `poolNS`: Optional. Application Key |
||
| 367 | * - `appKey`: Data Segment |
||
| 368 | * |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | private function getApcCacheKeyPattern() |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Human-readable identifier format. |
||
| 388 | * |
||
| 389 | * @param string $key The cache item key to format. |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | private function formatApcCacheKey($key) |
||
| 393 | { |
||
| 394 | $pattern = $this->getApcCacheKeyPattern(); |
||
| 395 | if (preg_match($pattern, $key, $matches)) { |
||
| 396 | $sns = $matches['stashNS']; |
||
| 397 | $iid = trim($matches['itemID'], ':'); |
||
| 398 | $iid = preg_replace([ '/:+/', '/\.+/' ], [ '⇒', '/' ], $iid); |
||
| 399 | $key = $matches['stashNS'] . '⇒' . $iid; |
||
| 400 | } |
||
| 401 | |||
| 402 | return $key; |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Human-readable bytes format. |
||
| 407 | * |
||
| 408 | * @param integer $bytes The number of bytes to format. |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | private function formatBytes($bytes) |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Human-readable time difference. |
||
| 431 | * |
||
| 432 | * Note: Adapted from CakePHP\Chronos. |
||
| 433 | * |
||
| 434 | * @see https://github.com/cakephp/chronos/blob/1.1.4/LICENSE |
||
| 435 | * |
||
| 436 | * @param DateTimeInterface $date1 The datetime to start with. |
||
| 437 | * @param DateTimeInterface|null $date2 The datetime to compare against. |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | private function formatTimeDiff(DateTimeInterface $date1, DateTimeInterface $date2 = null) |
||
| 441 | { |
||
| 442 | $isNow = $date2 === null; |
||
| 443 | if ($isNow) { |
||
| 444 | $date2 = new DateTime('now', $date1->getTimezone()); |
||
| 445 | } |
||
| 446 | $interval = $date1->diff($date2); |
||
| 447 | |||
| 448 | $translator = $this->translator(); |
||
| 449 | |||
| 450 | switch (true) { |
||
| 451 | case ($interval->y > 0): |
||
| 452 | $unit = 'time.year'; |
||
| 453 | $count = $interval->y; |
||
| 454 | break; |
||
| 455 | case ($interval->m > 0): |
||
| 456 | $unit = 'time.month'; |
||
| 457 | $count = $interval->m; |
||
| 458 | break; |
||
| 459 | case ($interval->d > 0): |
||
| 460 | $unit = 'time.day'; |
||
| 461 | $count = $interval->d; |
||
| 462 | if ($count >= 7) { |
||
| 463 | $unit = 'time.week'; |
||
| 464 | $count = (int)($count / 7); |
||
| 465 | } |
||
| 466 | break; |
||
| 467 | case ($interval->h > 0): |
||
| 468 | $unit = 'time.hour'; |
||
| 469 | $count = $interval->h; |
||
| 470 | break; |
||
| 471 | case ($interval->i > 0): |
||
| 472 | $unit = 'time.minute'; |
||
| 473 | $count = $interval->i; |
||
| 474 | break; |
||
| 475 | default: |
||
| 476 | $count = $interval->s; |
||
| 477 | $unit = 'time.second'; |
||
| 478 | break; |
||
| 479 | } |
||
| 480 | |||
| 481 | $time = $translator->transChoice($unit, $count, [ '{{ count }}' => $count ]); |
||
| 482 | |||
| 483 | return $time; |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param Container $container Pimple DI Container. |
||
| 488 | * @return void |
||
| 489 | */ |
||
| 490 | protected function setDependencies(Container $container) |
||
| 497 | } |
||
| 498 | } |
||
| 499 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths