Complex classes like BotPassword 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 BotPassword, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class BotPassword implements IDBAccessObject { |
||
| 28 | |||
| 29 | const APPID_MAXLENGTH = 32; |
||
| 30 | |||
| 31 | /** @var bool */ |
||
| 32 | private $isSaved; |
||
| 33 | |||
| 34 | /** @var int */ |
||
| 35 | private $centralId; |
||
| 36 | |||
| 37 | /** @var string */ |
||
| 38 | private $appId; |
||
| 39 | |||
| 40 | /** @var string */ |
||
| 41 | private $token; |
||
| 42 | |||
| 43 | /** @var MWRestrictions */ |
||
| 44 | private $restrictions; |
||
| 45 | |||
| 46 | /** @var string[] */ |
||
| 47 | private $grants; |
||
| 48 | |||
| 49 | /** @var int */ |
||
| 50 | private $flags = self::READ_NORMAL; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param object $row bot_passwords database row |
||
| 54 | * @param bool $isSaved Whether the bot password was read from the database |
||
| 55 | * @param int $flags IDBAccessObject read flags |
||
| 56 | */ |
||
| 57 | protected function __construct( $row, $isSaved, $flags = self::READ_NORMAL ) { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Get a database connection for the bot passwords database |
||
| 70 | * @param int $db Index of the connection to get, e.g. DB_MASTER or DB_REPLICA. |
||
| 71 | * @return Database |
||
| 72 | */ |
||
| 73 | public static function getDB( $db ) { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Load a BotPassword from the database |
||
| 84 | * @param User $user |
||
| 85 | * @param string $appId |
||
| 86 | * @param int $flags IDBAccessObject read flags |
||
| 87 | * @return BotPassword|null |
||
| 88 | */ |
||
| 89 | public static function newFromUser( User $user, $appId, $flags = self::READ_NORMAL ) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Load a BotPassword from the database |
||
| 98 | * @param int $centralId from CentralIdLookup |
||
| 99 | * @param string $appId |
||
| 100 | * @param int $flags IDBAccessObject read flags |
||
| 101 | * @return BotPassword|null |
||
| 102 | */ |
||
| 103 | public static function newFromCentralId( $centralId, $appId, $flags = self::READ_NORMAL ) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Create an unsaved BotPassword |
||
| 124 | * @param array $data Data to use to create the bot password. Keys are: |
||
| 125 | * - user: (User) User object to create the password for. Overrides username and centralId. |
||
| 126 | * - username: (string) Username to create the password for. Overrides centralId. |
||
| 127 | * - centralId: (int) User central ID to create the password for. |
||
| 128 | * - appId: (string) App ID for the password. |
||
| 129 | * - restrictions: (MWRestrictions, optional) Restrictions. |
||
| 130 | * - grants: (string[], optional) Grants. |
||
| 131 | * @param int $flags IDBAccessObject read flags |
||
| 132 | * @return BotPassword|null |
||
| 133 | */ |
||
| 134 | public static function newUnsaved( array $data, $flags = self::READ_NORMAL ) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Indicate whether this is known to be saved |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | public function isSaved() { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get the central user ID |
||
| 187 | * @return int |
||
| 188 | */ |
||
| 189 | public function getUserCentralId() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get the app ID |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public function getAppId() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get the token |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public function getToken() { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Get the restrictions |
||
| 211 | * @return MWRestrictions |
||
| 212 | */ |
||
| 213 | public function getRestrictions() { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Get the grants |
||
| 219 | * @return string[] |
||
| 220 | */ |
||
| 221 | public function getGrants() { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get the separator for combined user name + app ID |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public static function getSeparator() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get the password |
||
| 236 | * @return Password |
||
| 237 | */ |
||
| 238 | protected function getPassword() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Save the BotPassword to the database |
||
| 263 | * @param string $operation 'update' or 'insert' |
||
| 264 | * @param Password|null $password Password to set. |
||
| 265 | * @return bool Success |
||
| 266 | */ |
||
| 267 | public function save( $operation, Password $password = null ) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Delete the BotPassword from the database |
||
| 307 | * @return bool Success |
||
| 308 | */ |
||
| 309 | public function delete() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Invalidate all passwords for a user, by name |
||
| 326 | * @param string $username User name |
||
| 327 | * @return bool Whether any passwords were invalidated |
||
| 328 | */ |
||
| 329 | public static function invalidateAllPasswordsForUser( $username ) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Invalidate all passwords for a user, by central ID |
||
| 338 | * @param int $centralId |
||
| 339 | * @return bool Whether any passwords were invalidated |
||
| 340 | */ |
||
| 341 | public static function invalidateAllPasswordsForCentralId( $centralId ) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Remove all passwords for a user, by name |
||
| 360 | * @param string $username User name |
||
| 361 | * @return bool Whether any passwords were removed |
||
| 362 | */ |
||
| 363 | public static function removeAllPasswordsForUser( $username ) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Remove all passwords for a user, by central ID |
||
| 372 | * @param int $centralId |
||
| 373 | * @return bool Whether any passwords were removed |
||
| 374 | */ |
||
| 375 | public static function removeAllPasswordsForCentralId( $centralId ) { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Returns a (raw, unhashed) random password string. |
||
| 393 | * @param Config $config |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | public static function generatePassword( $config ) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * There are two ways to login with a bot password: "username@appId", "password" and |
||
| 403 | * "username", "appId@password". Transform it so it is always in the first form. |
||
| 404 | * Returns [bot username, bot password, could be normal password?] where the last one is a flag |
||
| 405 | * meaning this could either be a bot password or a normal password, it cannot be decided for |
||
| 406 | * certain (although in such cases it almost always will be a bot password). |
||
| 407 | * If this cannot be a bot password login just return false. |
||
| 408 | * @param string $username |
||
| 409 | * @param string $password |
||
| 410 | * @return array|false |
||
| 411 | */ |
||
| 412 | public static function canonicalizeLoginData( $username, $password ) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Try to log the user in |
||
| 433 | * @param string $username Combined user name and app ID |
||
| 434 | * @param string $password Supplied password |
||
| 435 | * @param WebRequest $request |
||
| 436 | * @return Status On success, the good status's value is the new Session object |
||
| 437 | */ |
||
| 438 | public static function login( $username, $password, WebRequest $request ) { |
||
| 484 | } |
||
| 485 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..