| Total Complexity | 51 |
| Total Lines | 228 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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.
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 |
||
| 52 | class File implements IEntity, IDisplayText, IUrl, IIcon, IContextPortation { |
||
| 53 | private const EVENT_NAMESPACE = '\OCP\Files::'; |
||
| 54 | |||
| 55 | /** @var IL10N */ |
||
| 56 | protected $l10n; |
||
| 57 | /** @var IURLGenerator */ |
||
| 58 | protected $urlGenerator; |
||
| 59 | /** @var IRootFolder */ |
||
| 60 | protected $root; |
||
| 61 | /** @var ILogger */ |
||
| 62 | protected $logger; |
||
| 63 | /** @var string */ |
||
| 64 | protected $eventName; |
||
| 65 | /** @var Event */ |
||
| 66 | protected $event; |
||
| 67 | /** @var ShareManager */ |
||
| 68 | private $shareManager; |
||
| 69 | /** @var IUserSession */ |
||
| 70 | private $userSession; |
||
| 71 | /** @var ISystemTagManager */ |
||
| 72 | private $tagManager; |
||
| 73 | /** @var ?Node */ |
||
| 74 | private $node; |
||
| 75 | /** @var ?IUser */ |
||
| 76 | private $actingUser = null; |
||
| 77 | /** @var IUserManager */ |
||
| 78 | private $userManager; |
||
| 79 | |||
| 80 | public function __construct( |
||
| 81 | IL10N $l10n, |
||
| 82 | IURLGenerator $urlGenerator, |
||
| 83 | IRootFolder $root, |
||
| 84 | ILogger $logger, |
||
| 85 | ShareManager $shareManager, |
||
| 86 | IUserSession $userSession, |
||
| 87 | ISystemTagManager $tagManager, |
||
| 88 | IUserManager $userManager |
||
| 89 | ) { |
||
| 90 | $this->l10n = $l10n; |
||
| 91 | $this->urlGenerator = $urlGenerator; |
||
| 92 | $this->root = $root; |
||
| 93 | $this->logger = $logger; |
||
| 94 | $this->shareManager = $shareManager; |
||
| 95 | $this->userSession = $userSession; |
||
| 96 | $this->tagManager = $tagManager; |
||
| 97 | $this->userManager = $userManager; |
||
| 98 | } |
||
| 99 | |||
| 100 | public function getName(): string { |
||
| 101 | return $this->l10n->t('File'); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function getIcon(): string { |
||
| 106 | } |
||
| 107 | |||
| 108 | public function getEvents(): array { |
||
| 109 | return [ |
||
| 110 | new GenericEntityEvent($this->l10n->t('File created'), self::EVENT_NAMESPACE . 'postCreate'), |
||
| 111 | new GenericEntityEvent($this->l10n->t('File updated'), self::EVENT_NAMESPACE . 'postWrite'), |
||
| 112 | new GenericEntityEvent($this->l10n->t('File renamed'), self::EVENT_NAMESPACE . 'postRename'), |
||
| 113 | new GenericEntityEvent($this->l10n->t('File deleted'), self::EVENT_NAMESPACE . 'postDelete'), |
||
| 114 | new GenericEntityEvent($this->l10n->t('File accessed'), self::EVENT_NAMESPACE . 'postTouch'), |
||
| 115 | new GenericEntityEvent($this->l10n->t('File copied'), self::EVENT_NAMESPACE . 'postCopy'), |
||
| 116 | new GenericEntityEvent($this->l10n->t('Tag assigned'), MapperEvent::EVENT_ASSIGN), |
||
| 117 | ]; |
||
| 118 | } |
||
| 119 | |||
| 120 | public function prepareRuleMatcher(IRuleMatcher $ruleMatcher, string $eventName, Event $event): void { |
||
| 121 | if (!$event instanceof GenericEvent && !$event instanceof MapperEvent) { |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | $this->eventName = $eventName; |
||
| 125 | $this->event = $event; |
||
| 126 | $this->actingUser = $this->actingUser ?? $this->userSession->getUser(); |
||
| 127 | try { |
||
| 128 | $node = $this->getNode(); |
||
| 129 | $ruleMatcher->setEntitySubject($this, $node); |
||
| 130 | $ruleMatcher->setFileInfo($node->getStorage(), $node->getInternalPath()); |
||
| 131 | } catch (NotFoundException $e) { |
||
| 132 | // pass |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | public function isLegitimatedForUserId(string $uid): bool { |
||
| 137 | try { |
||
| 138 | $node = $this->getNode(); |
||
| 139 | if ($node->getOwner()->getUID() === $uid) { |
||
| 140 | return true; |
||
| 141 | } |
||
| 142 | $acl = $this->shareManager->getAccessList($node, true, true); |
||
| 143 | return array_key_exists($uid, $acl['users']); |
||
| 144 | } catch (NotFoundException $e) { |
||
| 145 | return false; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @throws NotFoundException |
||
| 151 | */ |
||
| 152 | protected function getNode(): Node { |
||
| 153 | if ($this->node) { |
||
| 154 | return $this->node; |
||
| 155 | } |
||
| 156 | if (!$this->event instanceof GenericEvent && !$this->event instanceof MapperEvent) { |
||
| 157 | throw new NotFoundException(); |
||
| 158 | } |
||
| 159 | switch ($this->eventName) { |
||
| 160 | case self::EVENT_NAMESPACE . 'postCreate': |
||
| 161 | case self::EVENT_NAMESPACE . 'postWrite': |
||
| 162 | case self::EVENT_NAMESPACE . 'postDelete': |
||
| 163 | case self::EVENT_NAMESPACE . 'postTouch': |
||
| 164 | return $this->event->getSubject(); |
||
|
|
|||
| 165 | case self::EVENT_NAMESPACE . 'postRename': |
||
| 166 | case self::EVENT_NAMESPACE . 'postCopy': |
||
| 167 | return $this->event->getSubject()[1]; |
||
| 168 | case MapperEvent::EVENT_ASSIGN: |
||
| 169 | if (!$this->event instanceof MapperEvent || $this->event->getObjectType() !== 'files') { |
||
| 170 | throw new NotFoundException(); |
||
| 171 | } |
||
| 172 | $nodes = $this->root->getById((int)$this->event->getObjectId()); |
||
| 173 | if (is_array($nodes) && isset($nodes[0])) { |
||
| 174 | $this->node = $nodes[0]; |
||
| 175 | return $this->node; |
||
| 176 | } |
||
| 177 | break; |
||
| 178 | } |
||
| 179 | throw new NotFoundException(); |
||
| 180 | } |
||
| 181 | |||
| 182 | public function getDisplayText(int $verbosity = 0): string { |
||
| 183 | try { |
||
| 184 | $node = $this->getNode(); |
||
| 185 | } catch (NotFoundException $e) { |
||
| 186 | return ''; |
||
| 187 | } |
||
| 188 | |||
| 189 | $options = [ |
||
| 190 | $this->actingUser ? $this->actingUser->getDisplayName() : $this->l10n->t('Someone'), |
||
| 191 | $node->getName() |
||
| 192 | ]; |
||
| 193 | |||
| 194 | switch ($this->eventName) { |
||
| 195 | case self::EVENT_NAMESPACE . 'postCreate': |
||
| 196 | return $this->l10n->t('%s created %s', $options); |
||
| 197 | case self::EVENT_NAMESPACE . 'postWrite': |
||
| 198 | return $this->l10n->t('%s modified %s', $options); |
||
| 199 | case self::EVENT_NAMESPACE . 'postDelete': |
||
| 200 | return $this->l10n->t('%s deleted %s', $options); |
||
| 201 | case self::EVENT_NAMESPACE . 'postTouch': |
||
| 202 | return $this->l10n->t('%s accessed %s', $options); |
||
| 203 | case self::EVENT_NAMESPACE . 'postRename': |
||
| 204 | return $this->l10n->t('%s renamed %s', $options); |
||
| 205 | case self::EVENT_NAMESPACE . 'postCopy': |
||
| 206 | return $this->l10n->t('%s copied %s', $options); |
||
| 207 | case MapperEvent::EVENT_ASSIGN: |
||
| 208 | $tagNames = []; |
||
| 209 | if ($this->event instanceof MapperEvent) { |
||
| 210 | $tagIDs = $this->event->getTags(); |
||
| 211 | $tagObjects = $this->tagManager->getTagsByIds($tagIDs); |
||
| 212 | foreach ($tagObjects as $systemTag) { |
||
| 213 | /** @var ISystemTag $systemTag */ |
||
| 214 | if ($systemTag->isUserVisible()) { |
||
| 215 | $tagNames[] = $systemTag->getName(); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | } |
||
| 219 | $filename = array_pop($options); |
||
| 220 | $tagString = implode(', ', $tagNames); |
||
| 221 | if ($tagString === '') { |
||
| 222 | return ''; |
||
| 223 | } |
||
| 224 | array_push($options, $tagString, $filename); |
||
| 225 | return $this->l10n->t('%s assigned %s to %s', $options); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | public function getUrl(): string { |
||
| 230 | try { |
||
| 231 | return $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $this->getNode()->getId()]); |
||
| 232 | } catch (InvalidPathException $e) { |
||
| 233 | return ''; |
||
| 234 | } catch (NotFoundException $e) { |
||
| 235 | return ''; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @inheritDoc |
||
| 241 | */ |
||
| 242 | public function exportContextIDs(): array { |
||
| 255 | ]; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @inheritDoc |
||
| 260 | */ |
||
| 261 | public function importContextIDs(array $contextIDs): void { |
||
| 262 | $this->eventName = $contextIDs['eventName']; |
||
| 263 | if ($contextIDs['nodeOwnerId'] !== null) { |
||
| 264 | $userFolder = $this->root->getUserFolder($contextIDs['nodeOwnerId']); |
||
| 265 | $nodes = $userFolder->getById($contextIDs['nodeId']); |
||
| 266 | } else { |
||
| 267 | $nodes = $this->root->getById($contextIDs['nodeId']); |
||
| 268 | } |
||
| 269 | $this->node = $nodes[0] ?? null; |
||
| 270 | if ($contextIDs['actingUserId']) { |
||
| 271 | $this->actingUser = $this->userManager->get($contextIDs['actingUserId']); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @inheritDoc |
||
| 277 | */ |
||
| 278 | public function getIconUrl(): string { |
||
| 280 | } |
||
| 281 | } |
||
| 282 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.