| Total Complexity | 56 |
| Total Lines | 467 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 0 |
Complex classes like MODXPackages 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 MODXPackages, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class MODXPackages |
||
| 15 | { |
||
| 16 | /** @var string */ |
||
| 17 | protected $date_format = ''; |
||
| 18 | |||
| 19 | /** @var \modX */ |
||
| 20 | protected $modx; |
||
| 21 | |||
| 22 | /** @var array $providerCache */ |
||
| 23 | protected $providerCache = array(); |
||
| 24 | |||
| 25 | protected $provider_map = []; |
||
| 26 | |||
| 27 | /** @var int $updates_cache_expire */ |
||
| 28 | protected $updates_cache_expire = 300; |
||
| 29 | |||
| 30 | /** @var array */ |
||
| 31 | protected $possible_package_signatures = []; |
||
| 32 | |||
| 33 | /** @var \LCI\MODX\Console\Helpers\UserInteractionHandler */ |
||
| 34 | protected $userInteractionHandler; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * MODXPackages constructor. |
||
| 38 | * @param modX $modx |
||
| 39 | * @param UserInteractionHandler $userInteractionHandler |
||
| 40 | */ |
||
| 41 | public function __construct(modX $modx, UserInteractionHandler $userInteractionHandler) |
||
| 48 | } |
||
| 49 | |||
| 50 | // Code ideas for MODX packages from: core/model/modx/processors/workspace/packages/getlist.class.php and |
||
| 51 | // https://github.com/modmore/SiteDashClient/blob/master/core/components/sitedashclient/src/Package/Update.php |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get basic version information about the package |
||
| 55 | * |
||
| 56 | * @param string $signature |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | public static function getVersionInfo($signature) |
||
| 66 | ]; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param int $limit |
||
| 71 | * @param int $start |
||
| 72 | * @param string $search |
||
| 73 | * @return array |
||
| 74 | */ |
||
| 75 | public function getList($limit=25, $start=0, $search='') { |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * This is only needed if TransportNotFoundException is caught |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | public function getPossiblePackageSignatures(): array |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $signature - ex: ace-1.8.0-pl |
||
| 134 | * @param bool $latest_version |
||
| 135 | * @param string $provider_name |
||
| 136 | * @return bool |
||
| 137 | * @throws TransportException |
||
| 138 | * @throws TransportNotFoundException |
||
| 139 | */ |
||
| 140 | public function requirePackage($signature, $latest_version=true, $provider_name='modx.com') |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param string $signature - ex: ace-1.8.0-pl |
||
| 207 | * @param bool $force |
||
| 208 | * @return bool |
||
| 209 | * @throws TransportException |
||
| 210 | */ |
||
| 211 | public function removePackage($signature, $force=true) |
||
| 212 | { |
||
| 213 | /** @var modTransportPackage $package */ |
||
| 214 | $package = $this->modx->getObject('transport.modTransportPackage', [ |
||
| 215 | 'signature' => $signature, |
||
| 216 | ]); |
||
| 217 | |||
| 218 | if ($package instanceof \modTransportPackage) { |
||
| 219 | $package->getTransport(); |
||
| 220 | |||
| 221 | if (!$success = $package->removePackage($force)) { |
||
| 222 | throw new TransportException('Error Package did not uninstall.'); |
||
| 223 | } |
||
| 224 | |||
| 225 | MODXPackagesConfig::removePackageConfig($signature); |
||
| 226 | |||
| 227 | $this->userInteractionHandler->tellUser('Extra '.$signature.' has been removed', userInteractionHandler::MASSAGE_SUCCESS); |
||
| 228 | |||
| 229 | $this->modx->cacheManager->refresh([ |
||
| 230 | $this->modx->getOption('cache_packages_key', null, 'packages') => [] |
||
| 231 | ]); |
||
| 232 | $this->modx->cacheManager->refresh(); |
||
| 233 | |||
| 234 | } else { |
||
| 235 | throw new TransportException('Package with the signature: '.$signature.' does not seem to be installed. '. |
||
| 236 | 'Run the extra command to see list of installed extras with proper signatures. You can only remove packages that are installed.'); |
||
| 237 | } |
||
| 238 | |||
| 239 | return $success; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param string $signature - ex: ace-1.8.0-pl |
||
| 244 | * @param int $preexisting_mode, see xPDOTransport |
||
| 245 | * xPDOTransport::PRESERVE_PREEXISTING = 0; |
||
| 246 | xPDOTransport::REMOVE_PREEXISTING = 1; |
||
| 247 | xPDOTransport::RESTORE_PREEXISTING = 2; |
||
| 248 | * @return bool |
||
| 249 | * @throws TransportException |
||
| 250 | */ |
||
| 251 | public function unInstallPackage($signature, $preexisting_mode=null) |
||
| 252 | { |
||
| 253 | /** @var modTransportPackage $package */ |
||
| 254 | $package = $this->modx->getObject('transport.modTransportPackage', [ |
||
| 255 | 'signature' => $signature, |
||
| 256 | ]); |
||
| 257 | |||
| 258 | if ($package instanceof \modTransportPackage) { |
||
| 259 | $package->getTransport(); |
||
| 260 | /* uninstall package */ |
||
| 261 | $options = array( |
||
| 262 | xPDOTransport::PREEXISTING_MODE => is_null($preexisting_mode) ? xPDOTransport::REMOVE_PREEXISTING : $preexisting_mode, |
||
| 263 | ); |
||
| 264 | |||
| 265 | if (!$success = $package->uninstall($options)) { |
||
| 266 | throw new TransportException('Error Package did not uninstall.'); |
||
| 267 | } |
||
| 268 | |||
| 269 | MODXPackagesConfig::removePackageConfig($signature); |
||
| 270 | |||
| 271 | $this->userInteractionHandler->tellUser('Extra '.$signature.' has been uninstalled', userInteractionHandler::MASSAGE_SUCCESS); |
||
| 272 | |||
| 273 | $this->modx->cacheManager->refresh([ |
||
| 274 | $this->modx->getOption('cache_packages_key', null, 'packages') => [] |
||
| 275 | ]); |
||
| 276 | $this->modx->cacheManager->refresh(); |
||
| 277 | |||
| 278 | } else { |
||
| 279 | throw new TransportException('Package with the signature: '.$signature.' does not seem to be installed. '. |
||
| 280 | 'Run the extra command to see list of installed extras with proper signatures. You can only remove packages that are installed.'); |
||
| 281 | } |
||
| 282 | |||
| 283 | return $success; |
||
| 284 | } |
||
| 285 | |||
| 286 | // @TODO local packages |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param string $signature - ex: ace-1.8.0-pl |
||
| 290 | * @param modTransportProvider $provider |
||
| 291 | * @param bool $latest |
||
| 292 | * @throws TransportNotFoundException |
||
| 293 | * @return bool|modTransportPackage |
||
| 294 | */ |
||
| 295 | protected function downloadPackageFiles($signature, modTransportProvider $provider, $latest=true) |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param modTransportPackage $package |
||
| 329 | * @return bool |
||
| 330 | * @throws TransportException |
||
| 331 | */ |
||
| 332 | protected function runPackageInstallUpdate(modTransportPackage $package) |
||
| 333 | { |
||
| 334 | $installed = $package->install([]); |
||
| 335 | $this->modx->cacheManager->refresh([ |
||
| 336 | $this->modx->getOption('cache_packages_key', null, 'packages') => [] |
||
| 337 | ]); |
||
| 338 | $this->modx->cacheManager->refresh(); |
||
| 339 | |||
| 340 | if (!$installed) { |
||
| 341 | throw new TransportException('Failed to install package ' . $package->signature); |
||
| 342 | } |
||
| 343 | |||
| 344 | $this->userInteractionHandler->tellUser('Extra '.$package->get('signature').' has been installed!', userInteractionHandler::MASSAGE_SUCCESS); |
||
| 345 | |||
| 346 | $this->modx->invokeEvent('OnPackageInstall', array( |
||
| 347 | 'package' => $package, |
||
| 348 | 'action' => $package->previousVersionInstalled() ? \xPDOTransport::ACTION_UPGRADE : \xPDOTransport::ACTION_INSTALL |
||
| 349 | )); |
||
| 350 | |||
| 351 | return $installed; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Format installed, created and updated dates |
||
| 356 | * @param array $packageArray |
||
| 357 | * @return array |
||
| 358 | */ |
||
| 359 | protected function formatDates(array $packageArray) |
||
| 360 | { |
||
| 361 | if ($packageArray['updated'] != '0000-00-00 00:00:00' && $packageArray['updated'] != null) { |
||
| 362 | $packageArray['updated'] = utf8_encode(date($this->date_format, strtotime($packageArray['updated']))); |
||
| 363 | } else { |
||
| 364 | $packageArray['updated'] = ''; |
||
| 365 | } |
||
| 366 | |||
| 367 | $packageArray['created']= utf8_encode(date($this->date_format, strtotime($packageArray['created']))); |
||
| 368 | |||
| 369 | if ($packageArray['installed'] == null || $packageArray['installed'] == '0000-00-00 00:00:00') { |
||
| 370 | $packageArray['installed'] = null; |
||
| 371 | } else { |
||
| 372 | $packageArray['installed'] = utf8_encode(date($this->date_format, strtotime($packageArray['installed']))); |
||
| 373 | } |
||
| 374 | return $packageArray; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param modTransportPackage $package |
||
| 379 | * @return array |
||
| 380 | */ |
||
| 381 | protected function getAvailableUpdateInfo(modTransportPackage $package) |
||
| 382 | { |
||
| 383 | $updates = [ |
||
| 384 | 'count' => 0, |
||
| 385 | 'versions' => [] |
||
| 386 | ]; |
||
| 387 | if ($package->get('provider') > 0 && $this->modx->getOption('auto_check_pkg_updates',null,false)) { |
||
| 388 | $updateCacheKey = 'mgr/providers/updates/'.$package->get('provider').'/'.$package->get('signature'); |
||
| 389 | $updateCacheOptions = array( |
||
| 390 | xPDO::OPT_CACHE_KEY => $this->modx->cacheManager->getOption('cache_packages_key', null, 'packages'), |
||
| 391 | xPDO::OPT_CACHE_HANDLER => $this->modx->cacheManager->getOption('cache_packages_handler', null, $this->modx->cacheManager->getOption(xPDO::OPT_CACHE_HANDLER)), |
||
| 392 | ); |
||
| 393 | $updates = $this->modx->cacheManager->get($updateCacheKey, $updateCacheOptions); |
||
| 394 | |||
| 395 | if (empty($updates)) { |
||
| 396 | /* cache providers to speed up load time */ |
||
| 397 | /** @var modTransportProvider $provider */ |
||
| 398 | $provider = $this->getPackageProvider($package); |
||
| 399 | |||
| 400 | if ($provider) { |
||
| 401 | $options = $this->getPackageLatestVersions($provider, $package->get('signature')); |
||
| 402 | |||
| 403 | $updates['count'] = count($options); |
||
| 404 | $updates['versions'] = $options; |
||
| 405 | |||
| 406 | $this->modx->cacheManager->set($updateCacheKey, $updates, $this->updates_cache_expire, $updateCacheOptions); |
||
| 407 | } |
||
| 408 | } |
||
| 409 | } |
||
| 410 | |||
| 411 | return $updates; |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @param modTransportProvider $provider |
||
| 416 | * @param string $signature |
||
| 417 | * @return array - only returns values if the extra has a version that is more recent then the signature |
||
| 418 | */ |
||
| 419 | protected function getPackageLatestVersions(modTransportProvider $provider, $signature) |
||
| 420 | { |
||
| 421 | $package_versions = $provider->latest($signature); |
||
| 422 | |||
| 423 | $options = []; |
||
| 424 | /** @var \SimpleXMLElement $package */ |
||
| 425 | foreach ($package_versions as $package_version) { |
||
| 426 | $version_info = array_merge([ |
||
| 427 | 'location' => (string)$package_version['location'], |
||
| 428 | 'signature' => (string)$package_version['signature'], |
||
| 429 | 'package_name' => (string)$package_version['package_name'], |
||
| 430 | 'release' => '', |
||
| 431 | 'version' => '' |
||
| 432 | ], |
||
| 433 | self::getVersionInfo((string)$package_version['signature']) |
||
| 434 | ); |
||
| 435 | |||
| 436 | $options[$version_info['signature']] = $version_info; |
||
| 437 | } |
||
| 438 | |||
| 439 | return $options; |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @param modTransportPackage $package |
||
| 444 | * @return modTransportProvider|bool |
||
| 445 | */ |
||
| 446 | protected function getPackageProvider(modTransportPackage $package) |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @param string $name |
||
| 464 | * @return bool|modTransportProvider |
||
| 465 | */ |
||
| 466 | protected function getProvider($name='modx.com') |
||
| 481 | } |
||
| 482 | } |
||
| 483 |
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