| Total Complexity | 66 |
| Total Lines | 351 |
| Duplicated Lines | 0 % |
| Changes | 13 | ||
| Bugs | 1 | Features | 0 |
Complex classes like OneFileInfo 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 OneFileInfo, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class OneFileInfo implements FileInfo |
||
| 27 | { |
||
| 28 | use FilesystemRelatedTraits; |
||
| 29 | use Injectable; |
||
| 30 | use Configurable; |
||
| 31 | use Cacher; |
||
| 32 | use FlushNow; |
||
| 33 | |||
| 34 | protected static array $cached_inst = []; |
||
| 35 | |||
| 36 | public static function inst(string $path): OneFileInfo |
||
| 37 | { |
||
| 38 | if (!isset(self::$cached_inst[$path])) { |
||
| 39 | self::$cached_inst[$path] = new OneFileInfo($path); |
||
| 40 | } |
||
| 41 | return self::$cached_inst[$path]; |
||
| 42 | } |
||
| 43 | |||
| 44 | protected bool $debug = false; |
||
| 45 | protected bool $noCache = false; |
||
| 46 | |||
| 47 | public function setDebug(bool $b): static |
||
| 48 | { |
||
| 49 | $this->debug = $b; |
||
| 50 | return $this; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function setNoCache(bool $b): static |
||
| 54 | { |
||
| 55 | $this->noCache = $b; |
||
| 56 | return $this; |
||
| 57 | } |
||
| 58 | |||
| 59 | protected array $errorFields = [ |
||
| 60 | 'ErrorDBNotPresent', |
||
| 61 | 'ErrorDBNotPresentStaging', |
||
| 62 | 'ErrorExtensionMisMatch', |
||
| 63 | 'ErrorFindingFolder', |
||
| 64 | 'ErrorInFilename', |
||
| 65 | 'ErrorInSs3Ss4Comparison', |
||
| 66 | 'ErrorParentID', |
||
| 67 | 'ErrorNotInDraft', |
||
| 68 | ]; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected string $pathHash = ''; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected string $path = ''; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | protected array $intel = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var bool |
||
| 87 | */ |
||
| 88 | protected bool $physicalFileExists = false; |
||
| 89 | protected ?File $file; |
||
| 90 | |||
| 91 | public function __construct(string $location) |
||
| 92 | { |
||
| 93 | $this->path = $location; |
||
| 94 | $this->intel['Path'] = $this->path; |
||
| 95 | $this->intel['AbsolutePath'] = Controller::join_links(ASSETS_PATH, $this->intel['Path']); |
||
| 96 | $this->intel['IsProtected'] = false; |
||
| 97 | $this->pathHash = md5($this->path); |
||
| 98 | $this->physicalFileExists = file_exists($this->intel['AbsolutePath']); |
||
| 99 | $this->file = DataObject::get_one(File::class, ['FileFilename' => $this->path]); |
||
| 100 | if (!$this->physicalFileExists) { |
||
| 101 | if ($this->file && $this->file->exists()) { |
||
| 102 | $this->physicalFileExists = true; |
||
| 103 | $this->intel['IsProtected'] = true; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | public function toArray(): array |
||
| 109 | { |
||
| 110 | $cachekey = $this->getCacheKey(); |
||
| 111 | if (! $this->hasCacheKey($cachekey) || $this->noCache) { |
||
| 112 | $this->collateIntel(); |
||
| 113 | if ($this->debug) { |
||
| 114 | if ($this->intel['ErrorHasAnyError']) { |
||
| 115 | echo PHP_EOL . 'x ' . $this->path . ' ' . PHP_EOL; |
||
| 116 | } else { |
||
| 117 | echo '✓ '; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | $this->setCacheValue($cachekey, $this->intel); |
||
| 121 | } else { |
||
| 122 | $this->intel = $this->getCacheValue($cachekey); |
||
| 123 | } |
||
| 124 | |||
| 125 | return $this->intel; |
||
| 126 | } |
||
| 127 | |||
| 128 | protected function collateIntel(): void |
||
| 129 | { |
||
| 130 | |||
| 131 | $this->addFileSystemDetails(); |
||
| 132 | $this->addImageDetails(); |
||
| 133 | |||
| 134 | $obj = AllFilesInfo::inst(); |
||
| 135 | $dbFileData = $obj->getAnyData($this->intel['Path']); |
||
| 136 | $this->addFolderDetails($dbFileData); |
||
| 137 | $this->addDBDetails($dbFileData); |
||
| 138 | $this->addCalculatedValues(); |
||
| 139 | $this->addHumanValues(); |
||
| 140 | $this->file = null; |
||
| 141 | ksort($this->intel); |
||
| 142 | } |
||
| 143 | |||
| 144 | protected function isRegularImage(string $extension): bool |
||
| 145 | { |
||
| 146 | return in_array( |
||
| 147 | strtolower($extension), |
||
| 148 | ['jpg', 'gif', 'png', 'webp', 'jpeg', 'bmp', 'tiff', 'svg'], |
||
| 149 | true |
||
| 150 | ); |
||
| 151 | } |
||
| 152 | |||
| 153 | protected function isImage(): bool |
||
| 154 | { |
||
| 155 | return $this->file && $this->file instanceof Image; |
||
| 156 | } |
||
| 157 | |||
| 158 | protected function addFileSystemDetails() |
||
| 159 | { |
||
| 160 | //get path parts |
||
| 161 | $pathParts = []; |
||
| 162 | if ($this->physicalFileExists) { |
||
| 163 | $pathParts = pathinfo($this->intel['AbsolutePath']); |
||
| 164 | } |
||
| 165 | |||
| 166 | $pathParts['extension'] = $pathParts['extension'] ?? ''; |
||
| 167 | $pathParts['filename'] = $pathParts['filename'] ?? ''; |
||
| 168 | $pathParts['dirname'] = $pathParts['dirname'] ?? ''; |
||
| 169 | |||
| 170 | //basics! |
||
| 171 | $this->intel['InfoLink'] = Config::inst()->get(View::class, 'url_segment') . '/jsonone/?path=' . $this->path; |
||
| 172 | $this->intel['PathFolderPath'] = $pathParts['dirname'] ?: dirname($this->intel['Path']); |
||
| 173 | //file name |
||
| 174 | $this->intel['PathFileName'] = $pathParts['filename'] ?: basename($this->intel['AbsolutePath']); |
||
| 175 | $this->intel['PathFileNameFirstLetter'] = strtoupper(substr((string) $this->intel['PathFileName'], 0, 1)); |
||
| 176 | |||
| 177 | //defaults |
||
| 178 | $this->intel['ErrorIsInFileSystem'] = true; |
||
| 179 | $this->intel['PathFileSize'] = 0; |
||
| 180 | $this->intel['IsDir'] = is_dir($this->intel['AbsolutePath']); |
||
| 181 | |||
| 182 | //in file details |
||
| 183 | if ($this->physicalFileExists) { |
||
| 184 | $this->intel['ErrorIsInFileSystem'] = false; |
||
| 185 | $this->intel['PathFileSize'] = $this->file?->getAbsoluteSize() ?: 0; |
||
| 186 | } |
||
| 187 | |||
| 188 | //path |
||
| 189 | $this->intel['PathFromPublicRoot'] = trim(str_replace($this->getPublicBaseFolder(), '', (string) $this->intel['AbsolutePath']), DIRECTORY_SEPARATOR); |
||
| 190 | $this->intel['Path'] = trim($this->path, '/'); |
||
| 191 | $this->intel['PathFolderFromAssets'] = dirname($this->path); |
||
| 192 | if ('.' === $this->intel['PathFolderFromAssets']) { |
||
| 193 | $this->intel['PathFolderFromAssets'] = '--in-root-folder--'; |
||
| 194 | } |
||
| 195 | |||
| 196 | //folder |
||
| 197 | // $relativeDirFromAssetsFolder = str_replace($this->getAssetsBaseFolder(), '', (string) $this->intel['PathFolderPath']); |
||
| 198 | |||
| 199 | //extension |
||
| 200 | $this->intel['PathExtension'] = $pathParts['extension'] ?: $this->getExtension($this->path); |
||
| 201 | $this->intel['PathExtensionAsLower'] = (string) strtolower($this->intel['PathExtension']); |
||
| 202 | $this->intel['ErrorExtensionMisMatch'] = $this->intel['PathExtension'] !== $this->intel['PathExtensionAsLower']; |
||
| 203 | $pathExtensionWithDot = '.' . $this->intel['PathExtension']; |
||
| 204 | $extensionLength = strlen((string) $pathExtensionWithDot); |
||
| 205 | $pathLength = strlen((string) $this->intel['PathFileName']); |
||
| 206 | if (substr((string) $this->intel['PathFileName'], (-1 * $extensionLength)) === $pathExtensionWithDot) { |
||
| 207 | $this->intel['PathFileName'] = substr((string) $this->intel['PathFileName'], 0, ($pathLength - $extensionLength)); |
||
| 208 | } |
||
| 209 | |||
| 210 | $this->intel['ErrorInvalidExtension'] = false; |
||
| 211 | if (false !== $this->intel['IsDir']) { |
||
| 212 | $test = Injector::inst()->get(DBFile::class); |
||
| 213 | $validationResult = Injector::inst()->get(ValidationResult::class); |
||
| 214 | $this->intel['ErrorInvalidExtension'] = (bool) $test->validate( |
||
| 215 | $validationResult, |
||
| 216 | $this->intel['PathFileName'] |
||
| 217 | ); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | protected function addImageDetails() |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | protected function addFolderDetails($dbFileData) |
||
| 248 | { |
||
| 249 | $folder = []; |
||
| 250 | if (! empty($dbFileData['ParentID'])) { |
||
| 251 | $sql = 'SELECT ID FROM "File" WHERE "ID" = ' . $dbFileData['ParentID'] . ' LIMIT 1'; |
||
| 252 | $rows = DB::query($sql); |
||
| 253 | foreach ($rows as $folder) { |
||
| 254 | $hasFolder = true; |
||
| 255 | break; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | if ($hasFolder) { |
||
|
|
|||
| 260 | $this->intel['ErrorFindingFolder'] = false; |
||
| 261 | $this->intel['FolderID'] = $folder['ID']; |
||
| 262 | $this->intel['FolderCMSEditLink'] = '/admin/assets/show/' . $this->intel['FolderID'] . '/'; |
||
| 263 | } else { |
||
| 264 | $this->intel['ErrorFindingFolder'] = ! empty($dbFileData['ParentID']); |
||
| 265 | $this->intel['FolderID'] = 0; |
||
| 266 | $this->intel['FolderCMSEditLink'] = '/admin/assets'; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | protected function addDBDetails($dbFileData) |
||
| 271 | { |
||
| 272 | $time = 0; |
||
| 273 | if (empty($dbFileData)) { |
||
| 274 | $this->intel['ErrorParentID'] = false; |
||
| 275 | $this->intel['DBID'] = 0; |
||
| 276 | $this->intel['DBClassName'] = 'Not in database'; |
||
| 277 | $this->intel['DBParentID'] = 0; |
||
| 278 | $this->intel['DBPath'] = ''; |
||
| 279 | $this->intel['DBFilenameSS3'] = ''; |
||
| 280 | $this->intel['DBFilenameSS4'] = ''; |
||
| 281 | $this->intel['ErrorDBNotPresentStaging'] = true; |
||
| 282 | $this->intel['ErrorDBNotPresentLive'] = true; |
||
| 283 | $this->intel['ErrorInDraftOnly'] = false; |
||
| 284 | $this->intel['ErrorNotInDraft'] = false; |
||
| 285 | $this->intel['DBCMSEditLink'] = '/admin/assets/'; |
||
| 286 | $this->intel['DBTitle'] = '-- no title set in database'; |
||
| 287 | $this->intel['ErrorInFilename'] = false; |
||
| 288 | $this->intel['ErrorInSs3Ss4Comparison'] = false; |
||
| 289 | $time = time(); |
||
| 290 | if ($this->physicalFileExists && !$this->intel['IsProtected'] && $this->intel['AbsolutePath']) { |
||
| 291 | if (file_exists($this->intel['AbsolutePath'])) { |
||
| 292 | $time = filemtime($this->intel['AbsolutePath']); |
||
| 293 | } |
||
| 294 | } |
||
| 295 | } else { |
||
| 296 | $obj = AllFilesInfo::inst(); |
||
| 297 | if (!$this->intel['IsDir']) { |
||
| 298 | $this->intel['IsDir'] = is_a($dbFileData['ClassName'], Folder::class, true); |
||
| 299 | } |
||
| 300 | $dbFileData['Filename'] = $dbFileData['Filename'] ?? ''; |
||
| 301 | $this->intel['DBID'] = $dbFileData['ID']; |
||
| 302 | $this->intel['DBClassName'] = $dbFileData['ClassName']; |
||
| 303 | $this->intel['DBParentID'] = $dbFileData['ParentID']; |
||
| 304 | $this->intel['DBPath'] = $dbFileData['FileFilename'] ?? $dbFileData['Filename'] ?? ''; |
||
| 305 | $this->intel['DBFilename'] = $dbFileData['Name'] ?: basename($this->intel['DBPath']); |
||
| 306 | $existsOnStaging = $obj->existsOnStaging($this->intel['DBID']); |
||
| 307 | $existsOnLive = $obj->existsOnLive($this->intel['DBID']); |
||
| 308 | $this->intel['ErrorDBNotPresentStaging'] = ! $existsOnStaging; |
||
| 309 | $this->intel['ErrorDBNotPresentLive'] = ! $existsOnLive; |
||
| 310 | $this->intel['ErrorInDraftOnly'] = $existsOnStaging && ! $existsOnLive; |
||
| 311 | $this->intel['ErrorNotInDraft'] = ! $existsOnStaging && $existsOnLive; |
||
| 312 | $this->intel['DBCMSEditLink'] = '/admin/assets/EditForm/field/File/item/' . $this->intel['DBID'] . '/edit'; |
||
| 313 | $this->intel['DBTitle'] = $dbFileData['Title']; |
||
| 314 | $this->intel['DBFilenameSS4'] = $dbFileData['FileFilename'] ?? 'none'; |
||
| 315 | $this->intel['DBFilenameSS3'] = $dbFileData['Filename'] ?? 'none';; |
||
| 316 | $this->intel['ErrorInFilename'] = $this->intel['Path'] !== $this->intel['DBPath']; |
||
| 317 | $ss3FileName = $dbFileData['Filename'] ?? ''; |
||
| 318 | if ('assets/' === substr((string) $ss3FileName, 0, strlen('assets/'))) { |
||
| 319 | $ss3FileName = substr((string) $ss3FileName, strlen('assets/')); |
||
| 320 | } |
||
| 321 | |||
| 322 | $this->intel['ErrorInSs3Ss4Comparison'] = $this->intel['DBFilenameSS3'] && $this->intel['DBFilenameSS4'] !== $this->intel['DBFilenameSS3']; |
||
| 323 | $time = strtotime((string) $dbFileData['LastEdited']); |
||
| 324 | $this->intel['ErrorParentID'] = true; |
||
| 325 | if (0 === (int) $this->intel['FolderID']) { |
||
| 326 | $this->intel['ErrorParentID'] = (bool) (int) $dbFileData['ParentID']; |
||
| 327 | } elseif ($this->intel['FolderID']) { |
||
| 328 | $this->intel['ErrorParentID'] = (int) $this->intel['FolderID'] !== (int) $dbFileData['ParentID']; |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | $this->intel['ErrorDBNotPresent'] = $this->intel['ErrorDBNotPresentLive'] && $this->intel['ErrorDBNotPresentStaging']; |
||
| 333 | |||
| 334 | $this->intel['DBLastEditedTS'] = $time; |
||
| 335 | $this->intel['DBLastEdited'] = DBDate::create_field(DBDatetime::class, $time)->Ago(); |
||
| 336 | |||
| 337 | $this->intel['DBTitleFirstLetter'] = strtoupper(substr((string) $this->intel['DBTitle'], 0, 1)); |
||
| 338 | } |
||
| 339 | |||
| 340 | protected function addHumanValues() |
||
| 341 | { |
||
| 342 | $this->intel['HumanImageDimensions'] = $this->intel['ImageWidth'] . 'px wide by ' . $this->intel['ImageHeight'] . 'px high'; |
||
| 343 | $this->intel['HumanImageIsImage'] = $this->intel['IsImage'] ? 'Is image' : 'Is not an image'; |
||
| 344 | $this->intel['HumanErrorExtensionMisMatch'] = $this->intel['ErrorExtensionMisMatch'] ? |
||
| 345 | 'irregular extension' : 'normal extension'; |
||
| 346 | |||
| 347 | //file size |
||
| 348 | $this->intel['HumanFileSize'] = $this->humanFileSize($this->intel['PathFileSize']); |
||
| 349 | $this->intel['HumanFileSizeRounded'] = '~ ' . $this->humanFileSize(round($this->intel['PathFileSize'] / 204800) * 204800); |
||
| 350 | $this->intel['HumanErrorIsInFileSystem'] = $this->intel['ErrorIsInFileSystem'] ? 'File does not exist' : 'File exists'; |
||
| 351 | |||
| 352 | $this->intel['HumanFolderIsInOrder'] = $this->intel['FolderID'] ? 'In sub-folder' : 'In root folder'; |
||
| 353 | |||
| 354 | $this->intel['HumanErrorInFilename'] = $this->intel['ErrorInFilename'] ? 'Error in filename case' : 'No error in filename case'; |
||
| 355 | $this->intel['HumanErrorParentID'] = $this->intel['ErrorParentID'] ? 'Error in folder ID' : 'Perfect folder ID'; |
||
| 356 | $stageDBStatus = $this->intel['ErrorDBNotPresentStaging'] ? 'Is not on draft site' : ' Is on draft site'; |
||
| 357 | $liveDBStatus = $this->intel['ErrorDBNotPresentLive'] ? 'Is not on live site' : ' Is on live site'; |
||
| 358 | $this->intel['HumanErrorDBNotPresent'] = $stageDBStatus . ', ' . $liveDBStatus; |
||
| 359 | $this->intel['HumanErrorInSs3Ss4Comparison'] = $this->intel['ErrorInSs3Ss4Comparison'] ? |
||
| 360 | 'Filename and FileFilename do not match' : 'Filename and FileFilename match'; |
||
| 361 | $this->intel['HumanIcon'] = File::get_icon_for_extension($this->intel['PathExtensionAsLower']); |
||
| 362 | } |
||
| 363 | |||
| 364 | protected function addCalculatedValues() |
||
| 365 | { |
||
| 366 | $this->intel['ErrorHasAnyError'] = false; |
||
| 367 | foreach ($this->errorFields as $field) { |
||
| 368 | if ($this->intel[$field]) { |
||
| 369 | $this->intel['ErrorHasAnyError'] = true; |
||
| 370 | } |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | protected function getCacheKey(): string |
||
| 377 | } |
||
| 378 | } |
||
| 379 |