| Total Complexity | 41 |
| Total Lines | 298 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PackagistApiServices 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 PackagistApiServices, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class PackagistApiServices |
||
| 9 | { |
||
| 10 | use PackagistApiTrait; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Gets the package downloads. |
||
| 14 | * |
||
| 15 | * @param string $vendorAndPackage The vendor and package |
||
| 16 | * |
||
| 17 | * @return int || string || array The package total downloads. |
||
| 18 | */ |
||
| 19 | public static function getPackageDownloads($vendorAndPackage = null, $type = null) |
||
| 20 | { |
||
| 21 | $downloads = self::getSpecificPackageDetail($vendorAndPackage, 'downloads'); |
||
| 22 | |||
| 23 | if (!is_array($downloads)) { |
||
| 24 | return $downloads; |
||
| 25 | } |
||
| 26 | |||
| 27 | if ($type) { |
||
| 28 | return $downloads[$type]; |
||
| 29 | } |
||
| 30 | |||
| 31 | return $downloads; |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Gets the package daily downloads. |
||
| 36 | * |
||
| 37 | * @param string $vendorAndPackage The vendor and package |
||
| 38 | * |
||
| 39 | * @return int || string The package daily downloads. |
||
| 40 | */ |
||
| 41 | public static function getPackageDailyDownloads($vendorAndPackage = null) |
||
| 42 | { |
||
| 43 | return self::getPackageDownloads($vendorAndPackage, 'daily'); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Gets the package monthly downloads. |
||
| 48 | * |
||
| 49 | * @param string $vendorAndPackage The vendor and package |
||
| 50 | * |
||
| 51 | * @return int || string The package monthly downloads. |
||
| 52 | */ |
||
| 53 | public static function getPackageMonthlyDownloads($vendorAndPackage = null) |
||
| 54 | { |
||
| 55 | return self::getPackageDownloads($vendorAndPackage, 'monthly'); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Gets the package total downloads. |
||
| 60 | * |
||
| 61 | * @param string $vendorAndPackage The vendor and package |
||
| 62 | * |
||
| 63 | * @return int || string The package total downloads. |
||
| 64 | */ |
||
| 65 | public static function getPackageTotalDownloads($vendorAndPackage = null) |
||
| 66 | { |
||
| 67 | return self::getPackageDownloads($vendorAndPackage, 'total'); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Gets the package total forks. |
||
| 72 | * |
||
| 73 | * @param string $vendorAndPackage The vendor and package |
||
| 74 | * |
||
| 75 | * @return int || string The package total forks. |
||
| 76 | */ |
||
| 77 | public static function getPackageTotalForks($vendorAndPackage = null) |
||
| 78 | { |
||
| 79 | return self::getSpecificPackageDetail($vendorAndPackage, 'github_forks'); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Gets the package total open issues. |
||
| 84 | * |
||
| 85 | * @param string $vendorAndPackage The vendor and package |
||
| 86 | * |
||
| 87 | * @return string The package open issues. |
||
| 88 | */ |
||
| 89 | public static function getPackageTotalOpenIssues($vendorAndPackage = null) |
||
| 90 | { |
||
| 91 | return self::getSpecificPackageDetail($vendorAndPackage, 'github_open_issues'); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Gets the package repository. |
||
| 96 | * |
||
| 97 | * @param string $vendorAndPackage The vendor and package |
||
| 98 | * |
||
| 99 | * @return string The package repository. |
||
| 100 | */ |
||
| 101 | public static function getPackageTotalRepo($vendorAndPackage = null) |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Gets the package total stars. |
||
| 108 | * |
||
| 109 | * @param string $vendorAndPackage The vendor and package |
||
| 110 | * |
||
| 111 | * @return int || string The package total stars. |
||
| 112 | */ |
||
| 113 | public static function getPackageTotalStars($vendorAndPackage = null) |
||
| 114 | { |
||
| 115 | return self::getSpecificPackageDetail($vendorAndPackage, 'github_stars'); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Gets the package total watchers. |
||
| 120 | * |
||
| 121 | * @param string $vendorAndPackage The vendor and package |
||
| 122 | * |
||
| 123 | * @return string The package watchers. |
||
| 124 | */ |
||
| 125 | public static function getPackageTotalWatchers($vendorAndPackage = null) |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Gets the packagist vendor repositories list. |
||
| 132 | * |
||
| 133 | * @param string $vendor The vendor |
||
| 134 | * |
||
| 135 | * @return collection The packagist vendor repositories list. |
||
| 136 | */ |
||
| 137 | public static function getPackagistVendorRepositoriesList($vendor = null) |
||
| 138 | { |
||
| 139 | if (!$vendor) { |
||
| 140 | $vendor = config('laravelpackagist.vendor.default'); |
||
| 141 | } |
||
| 142 | |||
| 143 | $cachingEnabled = config('laravelpackagist.caching.enabled'); |
||
| 144 | |||
| 145 | if ($cachingEnabled) { |
||
| 146 | $vendorKey = self::assignVendorCacheKey($vendor); |
||
| 147 | if (self::checkIfItemIsCached($vendorKey)) { |
||
| 148 | return Cache::get($vendorKey); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | $baseUrl = config('laravelpackagist.urls.vendorBase').$vendor; |
||
| 153 | $response = self::curlPackagist($baseUrl); |
||
| 154 | $list = collect(json_decode($response)->packageNames); |
||
| 155 | |||
| 156 | if ($cachingEnabled) { |
||
| 157 | Cache::put($vendorKey, $list, self::getVendorListCacheTime()); |
||
| 158 | } |
||
| 159 | |||
| 160 | return $list; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Gets the vendor packages count. |
||
| 165 | * |
||
| 166 | * @param string $vendor The vendor |
||
| 167 | * |
||
| 168 | * @return int The vendor packages count. |
||
| 169 | */ |
||
| 170 | public static function getVendorPackagesCount($vendor = null) |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Gets the vendors packages details. |
||
| 181 | * |
||
| 182 | * @param string $vendor The vendor |
||
| 183 | * |
||
| 184 | * @return collection The vendors packages details. |
||
| 185 | */ |
||
| 186 | public static function getVendorsPackagesDetails($vendor = null) |
||
| 187 | { |
||
| 188 | if (!$vendor) { |
||
| 189 | $vendor = config('laravelpackagist.vendor.default'); |
||
| 190 | } |
||
| 191 | |||
| 192 | $projects = self::getPackagistVendorRepositoriesList($vendor); |
||
| 193 | $vendorProjects = collect([]); |
||
| 194 | $cachingEnabled = config('laravelpackagist.caching.enabled'); |
||
| 195 | |||
| 196 | foreach ($projects as $project) { |
||
| 197 | if (self::checkIfItemIsCached($project) && $cachingEnabled) { |
||
| 198 | $item = Cache::get($project); |
||
| 199 | } else { |
||
| 200 | $baseUrl = config('laravelpackagist.urls.projectPreFix').$project.config('laravelpackagist.urls.projectPostFix'); |
||
| 201 | $item = json_decode(self::curlPackagist($baseUrl))->package; |
||
| 202 | if ($cachingEnabled) { |
||
| 203 | Cache::put($project, $item, self::getVendorListCacheTime()); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | $vendorProjects[] = $item; |
||
| 207 | } |
||
| 208 | |||
| 209 | return $vendorProjects; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Gets the vendors package details. |
||
| 214 | * |
||
| 215 | * @param string $vendorAndPackage The vendor and package as 'vendor/package' |
||
| 216 | * @param bool $object Return as object |
||
| 217 | * |
||
| 218 | * @return object || Array The vendors package details. |
||
| 219 | */ |
||
| 220 | public static function getVendorsPackageDetails($vendorAndPackage = null, $object = false) |
||
| 221 | { |
||
| 222 | if (!$vendorAndPackage) { |
||
| 223 | return trans('laravelpackagist::laravelpackagist.missing-vendor-package'); |
||
| 224 | } |
||
| 225 | |||
| 226 | if ((!strstr($vendorAndPackage, '/')) || (count(explode('/', $vendorAndPackage)) != 2)) { |
||
| 227 | return trans('laravelpackagist::laravelpackagist.malformed-vendor-package'); |
||
| 228 | } |
||
| 229 | |||
| 230 | $decode = true; |
||
| 231 | if ($object === true) { |
||
| 232 | $decode = false; |
||
| 233 | } |
||
| 234 | |||
| 235 | $packageFound = false; |
||
| 236 | $packageDetails = trans('laravelpackagist::laravelpackagist.package-not-found'); |
||
| 237 | $cachingEnabled = config('laravelpackagist.caching.enabled'); |
||
| 238 | |||
| 239 | if (self::checkIfItemIsCached($vendorAndPackage) && $cachingEnabled) { |
||
| 240 | $item = Cache::get($vendorAndPackage); |
||
| 241 | $packageFound = true; |
||
| 242 | } else { |
||
| 243 | $baseUrl = config('laravelpackagist.urls.projectPreFix').$vendorAndPackage.config('laravelpackagist.urls.projectPostFix'); |
||
| 244 | $item = self::curlPackagist($baseUrl); |
||
| 245 | |||
| 246 | if ($item != '{"status":"error","message":"Package not found"}') { |
||
| 247 | $packageFound = true; |
||
| 248 | } |
||
| 249 | |||
| 250 | if ($packageFound && $cachingEnabled) { |
||
| 251 | Cache::put($vendorAndPackage, $item, self::getVendorListCacheTime()); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | if ($packageFound) { |
||
| 256 | $packageDetails = json_decode(json_encode(json_decode($item)->package), $decode); |
||
| 257 | } |
||
| 258 | |||
| 259 | return $packageDetails; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Gets the vendors total downloads. |
||
| 264 | * |
||
| 265 | * @param string $vendor The vendor |
||
| 266 | * |
||
| 267 | * @return int The vendors total downloads. |
||
| 268 | */ |
||
| 269 | public static function getVendorsTotalDownloads($vendor = null) |
||
| 270 | { |
||
| 271 | if (!$vendor) { |
||
| 272 | $vendor = config('laravelpackagist.vendor.default'); |
||
| 273 | } |
||
| 274 | |||
| 275 | $totalDownloads = 0; |
||
| 276 | $vendorProjects = self::getVendorsPackagesDetails($vendor); |
||
| 277 | |||
| 278 | foreach ($vendorProjects as $vendorProject) { |
||
| 279 | $totalDownloads += $vendorProject->downloads->total; |
||
| 280 | } |
||
| 281 | |||
| 282 | return $totalDownloads; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Gets the vendors total stars. |
||
| 287 | * |
||
| 288 | * @param string $vendor The vendor |
||
| 289 | * |
||
| 290 | * @return int The vendors total stars. |
||
| 291 | */ |
||
| 292 | public static function getVendorsTotalStars($vendor = null) |
||
| 306 | } |
||
| 307 | } |
||
| 308 |
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