Complex classes like File 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 File, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class File extends AbstractNode implements IFile |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * History types. |
||
| 31 | */ |
||
| 32 | const HISTORY_CREATE = 0; |
||
| 33 | const HISTORY_EDIT = 1; |
||
| 34 | const HISTORY_RESTORE = 2; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Empty content hash (NULL). |
||
| 38 | */ |
||
| 39 | const EMPTY_CONTENT = 'd41d8cd98f00b204e9800998ecf8427e'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Temporary file patterns. |
||
| 43 | * |
||
| 44 | * @param array |
||
| 45 | **/ |
||
| 46 | protected $temp_files = [ |
||
| 47 | '/^\._(.*)$/', // OS/X resource forks |
||
| 48 | '/^.DS_Store$/', // OS/X custom folder settings |
||
| 49 | '/^desktop.ini$/', // Windows custom folder settings |
||
| 50 | '/^Thumbs.db$/', // Windows thumbnail cache |
||
| 51 | '/^.(.*).swpx$/', // ViM temporary files |
||
| 52 | '/^.(.*).swx$/', // ViM temporary files |
||
| 53 | '/^.(.*).swp$/', // ViM temporary files |
||
| 54 | '/^\.dat(.*)$/', // Smultron seems to create these |
||
| 55 | '/^~lock.(.*)#$/', // Windows 7 lockfiles |
||
| 56 | '/^\~\$/', // Temporary office files |
||
| 57 | ]; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * MD5 Hash of the content. |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $hash; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * File version. |
||
| 68 | * |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | protected $version = 0; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * History. |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $history = []; |
||
| 79 | |||
| 80 | 17 | /** |
|
| 81 | * Session factory. |
||
| 82 | 17 | * |
|
| 83 | 17 | * @var SessionFactory |
|
| 84 | 17 | */ |
|
| 85 | 17 | protected $_session_factory; |
|
| 86 | 17 | ||
| 87 | 17 | /** |
|
| 88 | 17 | * Initialize file node. |
|
| 89 | 17 | */ |
|
| 90 | public function __construct(array $attributes, Filesystem $fs, LoggerInterface $logger, Hook $hook, Acl $acl, Collection $parent, SessionFactory $session_factory) |
||
| 91 | 17 | { |
|
| 92 | 17 | $this->_fs = $fs; |
|
| 93 | $this->_server = $fs->getServer(); |
||
| 94 | $this->_db = $fs->getDatabase(); |
||
| 95 | 17 | $this->_user = $fs->getUser(); |
|
| 96 | 17 | $this->_logger = $logger; |
|
| 97 | $this->_hook = $hook; |
||
| 98 | $this->_acl = $acl; |
||
| 99 | $this->_parent = $parent; |
||
| 100 | $this->_session_factory = $session_factory; |
||
| 101 | |||
| 102 | foreach ($attributes as $attr => $value) { |
||
| 103 | $this->{$attr} = $value; |
||
| 104 | } |
||
| 105 | |||
| 106 | $this->raw_attributes = $attributes; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Read content and return ressource. |
||
| 111 | */ |
||
| 112 | public function get() |
||
| 113 | { |
||
| 114 | if (null === $this->storage) { |
||
| 115 | return null; |
||
| 116 | } |
||
| 117 | |||
| 118 | try { |
||
| 119 | return $this->_parent->getStorage()->openReadStream($this); |
||
| 120 | } catch (\Exception $e) { |
||
| 121 | throw new Exception\NotFound('storage blob is gone', Exception\NotFound::CONTENTS_NOT_FOUND, $e); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Copy node. |
||
| 127 | */ |
||
| 128 | public function copyTo(Collection $parent, int $conflict = NodeInterface::CONFLICT_NOACTION, ?string $recursion = null, bool $recursion_first = true, int $deleted = NodeInterface::DELETED_EXCLUDE): NodeInterface |
||
| 129 | { |
||
| 130 | $this->_hook->run( |
||
| 131 | 'preCopyFile', |
||
| 132 | [$this, $parent, &$conflict, &$recursion, &$recursion_first] |
||
| 133 | ); |
||
| 134 | |||
| 135 | if (NodeInterface::CONFLICT_RENAME === $conflict && $parent->childExists($this->name)) { |
||
| 136 | $name = $this->getDuplicateName(); |
||
| 137 | } else { |
||
| 138 | $name = $this->name; |
||
| 139 | } |
||
| 140 | |||
| 141 | if (NodeInterface::CONFLICT_MERGE === $conflict && $parent->childExists($this->name)) { |
||
| 142 | $result = $parent->getChild($this->name); |
||
| 143 | |||
| 144 | if ($result instanceof Collection) { |
||
| 145 | $result = $this->copyToCollection($result, $name); |
||
| 146 | } else { |
||
| 147 | $stream = $this->get(); |
||
| 148 | if ($stream !== null) { |
||
| 149 | $result->put($stream); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } else { |
||
| 153 | $result = $this->copyToCollection($parent, $name); |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->_hook->run( |
||
| 157 | 'postCopyFile', |
||
| 158 | [$this, $parent, $result, $conflict, $recursion, $recursion_first] |
||
| 159 | ); |
||
| 160 | |||
| 161 | return $result; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get history. |
||
| 166 | */ |
||
| 167 | public function getHistory(): array |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Restore content to some older version. |
||
| 174 | */ |
||
| 175 | public function restore(int $version): bool |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Delete node. |
||
| 254 | * |
||
| 255 | * Actually the node will not be deleted (Just set a delete flag), set $force=true to |
||
| 256 | * delete finally |
||
| 257 | */ |
||
| 258 | public function delete(bool $force = false, ?string $recursion = null, bool $recursion_first = true): bool |
||
| 288 | |||
| 289 | 1 | /** |
|
| 290 | * Check if file is temporary. |
||
| 291 | */ |
||
| 292 | public function isTemporaryFile(): bool |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Delete version. |
||
| 305 | */ |
||
| 306 | public function deleteVersion(int $version): bool |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Cleanup history. |
||
| 341 | */ |
||
| 342 | public function cleanHistory(): bool |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Get Attributes. |
||
| 353 | */ |
||
| 354 | public function getAttributes(): array |
||
| 381 | 1 | ||
| 382 | /** |
||
| 383 | * Get filename extension. |
||
| 384 | */ |
||
| 385 | public function getExtension(): string |
||
| 394 | |||
| 395 | /** |
||
| 396 | 1 | * Get file size. |
|
| 397 | */ |
||
| 398 | 1 | public function getSize(): int |
|
| 402 | |||
| 403 | /** |
||
| 404 | 1 | * Get md5 sum of the file content, |
|
| 405 | * actually the hash value comes from the database. |
||
| 406 | 1 | */ |
|
| 407 | public function getETag(): string |
||
| 411 | |||
| 412 | 1 | /** |
|
| 413 | * Get hash. |
||
| 414 | 1 | */ |
|
| 415 | public function getHash(): ?string |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Get version. |
||
| 422 | */ |
||
| 423 | public function getVersion(): int |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Change content (Sabe dav compatible method). |
||
| 430 | */ |
||
| 431 | public function put($content): int |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Set content (temporary file). |
||
| 444 | */ |
||
| 445 | public function setContent(SessionInterface $session, array $attributes = []): int |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Copy to collection. |
||
| 510 | */ |
||
| 511 | protected function copyToCollection(Collection $parent, string $name): NodeInterface |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Completly remove file. |
||
| 532 | */ |
||
| 533 | protected function _forceDelete(): bool |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Increase version. |
||
| 559 | */ |
||
| 560 | protected function increaseVersion(): int |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Pre content change checks. |
||
| 579 | */ |
||
| 580 | protected function prePutFile(ObjectId $session): bool |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Add new version. |
||
| 597 | */ |
||
| 598 | protected function addVersion(array $attributes = []): self |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Finalize put request. |
||
| 639 | */ |
||
| 640 | protected function postPutFile(SessionInterface $session): self |
||
| 672 | } |
||
| 673 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.