| Total Complexity | 44 |
| Total Lines | 307 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PharInfo 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 PharInfo, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 83 | final class PharInfo |
||
| 84 | { |
||
| 85 | public const BOX_REQUIREMENTS = '.box/.requirements.php'; |
||
| 86 | |||
| 87 | private static array $ALGORITHMS; |
||
| 88 | private static string $stubfile; |
||
| 89 | |||
| 90 | private readonly PharMeta $meta; |
||
|
|
|||
| 91 | private readonly string $tmp; |
||
| 92 | private readonly string $file; |
||
| 93 | private readonly string $fileName; |
||
| 94 | private array $compressionCount; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var array<string, SplFileInfo> |
||
| 98 | */ |
||
| 99 | private readonly array $files; |
||
| 100 | |||
| 101 | public function __construct(string $file) |
||
| 102 | { |
||
| 103 | $file = Path::canonicalize($file); |
||
| 104 | |||
| 105 | if (!file_exists($file)) { |
||
| 106 | throw InvalidPhar::fileNotFound($file); |
||
| 107 | } |
||
| 108 | |||
| 109 | if (!is_readable($file)) { |
||
| 110 | throw InvalidPhar::fileNotReadable($file); |
||
| 111 | } |
||
| 112 | |||
| 113 | self::initAlgorithms(); |
||
| 114 | self::initStubFileName(); |
||
| 115 | |||
| 116 | $this->file = $file; |
||
| 117 | $this->fileName = basename($file); |
||
| 118 | |||
| 119 | $this->tmp = FS::makeTmpDir('HumbugBox', 'Pharaoh'); |
||
| 120 | |||
| 121 | self::dumpPhar($file, $this->tmp); |
||
| 122 | [ |
||
| 123 | $this->meta, |
||
| 124 | $this->files, |
||
| 125 | ] = self::loadDumpedPharFiles($this->tmp); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function __destruct() |
||
| 129 | { |
||
| 130 | unset($this->pharInfo); |
||
| 131 | |||
| 132 | if (isset($this->phar)) { |
||
| 133 | $path = $this->phar->getPath(); |
||
| 134 | unset($this->phar); |
||
| 135 | |||
| 136 | Phar::unlinkArchive($path); |
||
| 137 | } |
||
| 138 | |||
| 139 | if (isset($this->tmp)) { |
||
| 140 | FS::remove($this->tmp); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | public function getTmp(): string |
||
| 145 | { |
||
| 146 | return $this->tmp; |
||
| 147 | } |
||
| 148 | |||
| 149 | public function getFile(): string |
||
| 150 | { |
||
| 151 | return $this->file; |
||
| 152 | } |
||
| 153 | |||
| 154 | public function getPubKeyContent(): ?string |
||
| 155 | { |
||
| 156 | return $this->meta->pubKeyContent; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function hasPubKey(): bool |
||
| 160 | { |
||
| 161 | return null !== $this->getPubKeyContent(); |
||
| 162 | } |
||
| 163 | |||
| 164 | public function getFileName(): string |
||
| 165 | { |
||
| 166 | return $this->fileName; |
||
| 167 | } |
||
| 168 | |||
| 169 | public function equals(self $pharInfo): bool |
||
| 170 | { |
||
| 171 | return |
||
| 172 | $this->contentEquals($pharInfo) |
||
| 173 | && $this->getCompression() === $pharInfo->getCompression() |
||
| 174 | && $this->getNormalizedMetadata() === $pharInfo->getNormalizedMetadata(); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Checks if the content of the given PHAR equals the current one. Note that by content is meant |
||
| 179 | * the list of files and their content. The files compression or the PHAR metadata are not considered. |
||
| 180 | */ |
||
| 181 | private function contentEquals(self $pharInfo): bool |
||
| 182 | { |
||
| 183 | // The signature only checks if the contents are equal (same files, each files same content), but do |
||
| 184 | // not check the compression of the files. |
||
| 185 | // As a result, we also need to check the compression of each file. |
||
| 186 | if ($this->getSignature() != $pharInfo->getSignature()) { |
||
| 187 | return false; |
||
| 188 | } |
||
| 189 | |||
| 190 | foreach ($this->meta->filesMeta as $file => ['compression' => $compressionAlgorithm]) { |
||
| 191 | ['compression' => $otherCompressionAlgorithm] = $this->getFileMeta($file); |
||
| 192 | |||
| 193 | if ($otherCompressionAlgorithm !== $compressionAlgorithm) { |
||
| 194 | return false; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | return true; |
||
| 199 | } |
||
| 200 | |||
| 201 | public function getCompression(): CompressionAlgorithm |
||
| 202 | { |
||
| 203 | return $this->meta->compression; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @return array<string, positive-int|0> The number of files per compression algorithm label. |
||
| 208 | */ |
||
| 209 | public function getFilesCompressionCount(): array |
||
| 210 | { |
||
| 211 | if (!isset($this->compressionCount)) { |
||
| 212 | $this->compressionCount = self::calculateCompressionCount($this->meta->filesMeta); |
||
| 213 | } |
||
| 214 | |||
| 215 | return $this->compressionCount; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return array{'compression': CompressionAlgorithm, compressedSize: int} |
||
| 220 | */ |
||
| 221 | public function getFileMeta(string $path): array |
||
| 222 | { |
||
| 223 | $meta = $this->meta->filesMeta[$path] ?? null; |
||
| 224 | |||
| 225 | if (null === $meta) { |
||
| 226 | throw new OutOfBoundsException( |
||
| 227 | sprintf( |
||
| 228 | 'No metadata found for the file "%s".', |
||
| 229 | $path, |
||
| 230 | ), |
||
| 231 | ); |
||
| 232 | } |
||
| 233 | |||
| 234 | return $meta; |
||
| 235 | } |
||
| 236 | |||
| 237 | public function getVersion(): ?string |
||
| 238 | { |
||
| 239 | // TODO: review this fallback value |
||
| 240 | return $this->meta->version ?? 'No information found'; |
||
| 241 | } |
||
| 242 | |||
| 243 | public function getNormalizedMetadata(): ?string |
||
| 244 | { |
||
| 245 | return $this->meta->normalizedMetadata; |
||
| 246 | } |
||
| 247 | |||
| 248 | public function getTimestamp(): int |
||
| 249 | { |
||
| 250 | return $this->meta->timestamp; |
||
| 251 | } |
||
| 252 | |||
| 253 | public function getSignature(): ?array |
||
| 254 | { |
||
| 255 | return $this->meta->signature; |
||
| 256 | } |
||
| 257 | |||
| 258 | public function getStubPath(): string |
||
| 259 | { |
||
| 260 | return Extract::STUB_PATH; |
||
| 261 | } |
||
| 262 | |||
| 263 | public function getStubContent(): ?string |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @return array<string, SplFileInfo> |
||
| 270 | */ |
||
| 271 | public function getFiles(): array |
||
| 272 | { |
||
| 273 | return $this->files; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @throws NoRequirementsFound |
||
| 278 | * @throws InvalidRequirements |
||
| 279 | */ |
||
| 280 | public function getRequirements(): Requirements |
||
| 281 | { |
||
| 282 | $file = $this->getFileName(); |
||
| 283 | |||
| 284 | if (!array_key_exists(self::BOX_REQUIREMENTS, $this->files)) { |
||
| 285 | throw NoRequirementsFound::forFile($file); |
||
| 286 | } |
||
| 287 | |||
| 288 | $evaluatedRequirements = require $this->files[self::BOX_REQUIREMENTS]->getPathname(); |
||
| 289 | |||
| 290 | if (!is_array($evaluatedRequirements)) { |
||
| 291 | throw InvalidRequirements::forRequirements($file, $evaluatedRequirements); |
||
| 292 | } |
||
| 293 | |||
| 294 | try { |
||
| 295 | return new Requirements( |
||
| 296 | array_map( |
||
| 297 | Requirement::fromArray(...), |
||
| 298 | $evaluatedRequirements, |
||
| 299 | ), |
||
| 300 | ); |
||
| 301 | } catch (Throwable $throwable) { |
||
| 302 | throw new InvalidRequirements( |
||
| 303 | sprintf( |
||
| 304 | 'Could not interpret Box\'s RequirementChecker shipped in "%s": %s', |
||
| 305 | $file, |
||
| 306 | $throwable->getMessage(), |
||
| 307 | ), |
||
| 308 | previous: $throwable, |
||
| 309 | ); |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | private static function initAlgorithms(): void |
||
| 314 | { |
||
| 315 | if (!isset(self::$ALGORITHMS)) { |
||
| 316 | self::$ALGORITHMS = []; |
||
| 317 | |||
| 318 | foreach (CompressionAlgorithm::cases() as $compressionAlgorithm) { |
||
| 319 | self::$ALGORITHMS[$compressionAlgorithm->value] = $compressionAlgorithm->name; |
||
| 320 | } |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | private static function initStubFileName(): void |
||
| 325 | { |
||
| 326 | if (!isset(self::$stubfile)) { |
||
| 327 | self::$stubfile = bin2hex(random_bytes(12)).'.pharstub'; |
||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | private static function dumpPhar(string $file, string $tmp): void |
||
| 349 | ); |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @return array{PharMeta, array<string, SplFileInfo>} |
||
| 355 | */ |
||
| 356 | private static function loadDumpedPharFiles(string $tmp): array |
||
| 357 | { |
||
| 358 | $dumpedFiles = toArrayWithKeys( |
||
| 359 | mapKeys( |
||
| 360 | static fn (string $filePath) => Path::makeRelative($filePath, $tmp), |
||
| 361 | Finder::create() |
||
| 362 | ->files() |
||
| 363 | ->ignoreDotFiles(false) |
||
| 364 | ->exclude('.phar') |
||
| 365 | ->in($tmp), |
||
| 366 | ), |
||
| 367 | ); |
||
| 368 | |||
| 369 | $meta = PharMeta::fromJson(FS::getFileContents($tmp.DIRECTORY_SEPARATOR.Extract::PHAR_META_PATH)); |
||
| 370 | unset($dumpedFiles[Extract::PHAR_META_PATH]); |
||
| 371 | |||
| 372 | return [$meta, $dumpedFiles]; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param array<string, array{'compression': CompressionAlgorithm, compressedSize: int}> $filesMeta |
||
| 377 | */ |
||
| 378 | private static function calculateCompressionCount(array $filesMeta): array |
||
| 390 | } |
||
| 391 | } |
||
| 392 |
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