| Total Complexity | 65 |
| Total Lines | 411 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like UserRights 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 UserRights, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class UserRights extends Model |
||
| 17 | { |
||
| 18 | protected I18nHelper $i18n; |
||
| 19 | |||
| 20 | /** @var string[] Rights changes, keyed by timestamp then 'added' and 'removed'. */ |
||
| 21 | protected array $rightsChanges; |
||
| 22 | |||
| 23 | /** @var string[] Localized names of the rights. */ |
||
| 24 | protected array $rightsNames; |
||
| 25 | |||
| 26 | /** @var string[] Global rights changes (log), keyed by timestamp then 'added' and 'removed'. */ |
||
| 27 | protected array $globalRightsChanges; |
||
| 28 | |||
| 29 | /** @var array The current and former rights of the user. */ |
||
| 30 | protected array $rightsStates = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param UserRightsRepository $repository |
||
| 34 | * @param User $user |
||
| 35 | */ |
||
| 36 | public function __construct(UserRightsRepository $repository, Project $project, User $user, I18nHelper $i18n) |
||
| 37 | { |
||
| 38 | $this->repository = $repository; |
||
| 39 | $this->project = $project; |
||
| 40 | $this->user = $user; |
||
| 41 | $this->i18n = $i18n; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Get user rights changes of the given user. |
||
| 46 | * @param int|null $limit |
||
| 47 | * @return string[] Keyed by timestamp then 'added' and 'removed'. |
||
| 48 | */ |
||
| 49 | public function getRightsChanges(?int $limit = null): array |
||
| 50 | { |
||
| 51 | if (!isset($this->rightsChanges)) { |
||
| 52 | $logData = $this->repository->getRightsChanges($this->project, $this->user); |
||
|
|
|||
| 53 | |||
| 54 | $this->rightsChanges = $this->processRightsChanges($logData); |
||
| 55 | |||
| 56 | $acDate = $this->getAutoconfirmedTimestamp(); |
||
| 57 | if (false !== $acDate) { |
||
| 58 | $this->rightsChanges[$acDate] = [ |
||
| 59 | 'logId' => null, |
||
| 60 | 'performer' => null, |
||
| 61 | 'comment' => null, |
||
| 62 | 'added' => ['autoconfirmed'], |
||
| 63 | 'removed' => [], |
||
| 64 | 'grantType' => strtotime($acDate) > time() ? 'pending' : 'automatic', |
||
| 65 | 'type' => 'local', |
||
| 66 | ]; |
||
| 67 | krsort($this->rightsChanges); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | return array_slice($this->rightsChanges, 0, $limit, true); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Checks the user rights log to see whether the user is an admin or used to be one. |
||
| 76 | * @return string|false One of false (never an admin), 'current' or 'former'. |
||
| 77 | */ |
||
| 78 | public function getAdminStatus() |
||
| 79 | { |
||
| 80 | $rightsStates = $this->getRightsStates(); |
||
| 81 | |||
| 82 | if (in_array('sysop', $rightsStates['local']['current'])) { |
||
| 83 | return 'current'; |
||
| 84 | } elseif (in_array('sysop', $rightsStates['local']['former'])) { |
||
| 85 | return 'former'; |
||
| 86 | } else { |
||
| 87 | return false; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get a list of the current and former rights of the user. |
||
| 93 | * @return array With keys 'local' and 'global', each with keys 'current' and 'former'. |
||
| 94 | */ |
||
| 95 | public function getRightsStates(): array |
||
| 96 | { |
||
| 97 | if (count($this->rightsStates) > 0) { |
||
| 98 | return $this->rightsStates; |
||
| 99 | } |
||
| 100 | |||
| 101 | foreach (['local', 'global'] as $type) { |
||
| 102 | [$currentRights, $rightsChanges] = $this->getCurrentRightsAndChanges($type); |
||
| 103 | |||
| 104 | $former = []; |
||
| 105 | |||
| 106 | // We'll keep track of added rights, which we'll later compare with the |
||
| 107 | // current rights to ensure the list of former rights is complete. |
||
| 108 | // This is because sometimes rights were removed but there mysteriously |
||
| 109 | // is no log entry of it. |
||
| 110 | $added = []; |
||
| 111 | |||
| 112 | foreach ($rightsChanges as $change) { |
||
| 113 | $former = array_diff( |
||
| 114 | array_merge($former, $change['removed']), |
||
| 115 | $change['added'] |
||
| 116 | ); |
||
| 117 | |||
| 118 | $added = array_unique(array_merge($added, $change['added'])); |
||
| 119 | } |
||
| 120 | |||
| 121 | // Also tag on rights that were previously added but mysteriously |
||
| 122 | // don't have a log entry for when they were removed. |
||
| 123 | $former = array_merge( |
||
| 124 | array_diff($added, $currentRights), |
||
| 125 | $former |
||
| 126 | ); |
||
| 127 | |||
| 128 | // Remove the current rights for good measure. Autoconfirmed is a special case -- it can never be former, |
||
| 129 | // but will end up in $former from the above code. |
||
| 130 | $former = array_diff(array_unique($former), $currentRights, ['autoconfirmed']); |
||
| 131 | |||
| 132 | $this->rightsStates[$type] = [ |
||
| 133 | 'current' => $currentRights, |
||
| 134 | 'former' => $former, |
||
| 135 | ]; |
||
| 136 | } |
||
| 137 | |||
| 138 | return $this->rightsStates; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get a list of the current rights (of given type) and the log. |
||
| 143 | * @param string $type 'local' or 'global' |
||
| 144 | * @return array [string[] current rights, array rights changes]. |
||
| 145 | */ |
||
| 146 | private function getCurrentRightsAndChanges(string $type): array |
||
| 147 | { |
||
| 148 | // Current rights are not fetched from the log because really old |
||
| 149 | // log entries contained little or no metadata, and the rights |
||
| 150 | // changes may be undetectable. |
||
| 151 | if ('local' === $type) { |
||
| 152 | $currentRights = $this->user->getUserRights($this->project); |
||
| 153 | $rightsChanges = $this->getRightsChanges(); |
||
| 154 | |||
| 155 | $acDate = $this->getAutoconfirmedTimestamp(); |
||
| 156 | if (false !== $acDate && strtotime($acDate) <= time()) { |
||
| 157 | $currentRights[] = 'autoconfirmed'; |
||
| 158 | } |
||
| 159 | } else { |
||
| 160 | $currentRights = $this->user->getGlobalUserRights($this->project); |
||
| 161 | $rightsChanges = $this->getGlobalRightsChanges(); |
||
| 162 | } |
||
| 163 | |||
| 164 | return [$currentRights, $rightsChanges]; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get a list of the current and former global rights of the user. |
||
| 169 | * @return array With keys 'current' and 'former'. |
||
| 170 | */ |
||
| 171 | public function getGlobalRightsStates(): array |
||
| 172 | { |
||
| 173 | return $this->getRightsStates()['global']; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get global user rights changes of the given user. |
||
| 178 | * @param int|null $limit |
||
| 179 | * @return string[] Keyed by timestamp then 'added' and 'removed'. |
||
| 180 | */ |
||
| 181 | public function getGlobalRightsChanges(?int $limit = null): array |
||
| 182 | { |
||
| 183 | if (!isset($this->globalRightsChanges)) { |
||
| 184 | $logData = $this->repository->getGlobalRightsChanges($this->project, $this->user); |
||
| 185 | $this->globalRightsChanges = $this->processRightsChanges($logData); |
||
| 186 | } |
||
| 187 | |||
| 188 | return array_slice($this->globalRightsChanges, 0, $limit, true); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get the localized names for the user groups, fetched from on-wiki system messages. |
||
| 193 | * @return string[] Localized names keyed by database value. |
||
| 194 | */ |
||
| 195 | public function getRightsNames(): array |
||
| 196 | { |
||
| 197 | if (isset($this->rightsNames)) { |
||
| 198 | return $this->rightsNames; |
||
| 199 | } |
||
| 200 | |||
| 201 | $this->rightsNames = $this->repository->getRightsNames($this->project, $this->i18n->getLang()); |
||
| 202 | |||
| 203 | return $this->rightsNames; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get the localized translation for the given user right. |
||
| 208 | * @param string $name The name of the right, such as 'sysop'. |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function getRightsName(string $name): string |
||
| 212 | { |
||
| 213 | return $this->getRightsNames()[$name] ?? $name; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Process the given rights changes, sorting an putting in a human-readable format. |
||
| 218 | * @param array $logData As fetched with EditCounterRepository::getRightsChanges. |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | private function processRightsChanges(array $logData): array |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Check the given log entry for rights changes that are set to automatically expire, |
||
| 315 | * and add entries to $rightsChanges accordingly. |
||
| 316 | * @param array $rightsChanges |
||
| 317 | * @param array $row Log entry row from database. |
||
| 318 | * @param array $params Unserialized log params. |
||
| 319 | * @param string[] $added List of added user rights. |
||
| 320 | */ |
||
| 321 | private function setAutoRemovals(array &$rightsChanges, array $row, array $params, array $added): void |
||
| 353 | } |
||
| 354 | |||
| 355 | private function unsetAutoRemoval(array &$rightsChanges, array $removed): void |
||
| 356 | { |
||
| 357 | foreach ($rightsChanges as $timestamp => $change) { |
||
| 362 | } |
||
| 363 | } |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Get the timestamp of when the user became autoconfirmed. |
||
| 369 | * @return string|false YmdHis format, or false if date is in the future or if AC status could not be determined. |
||
| 370 | */ |
||
| 371 | private function getAutoconfirmedTimestamp() |
||
| 429 |