Complex classes like RestrictionService 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 RestrictionService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class RestrictionService |
||
| 48 | { |
||
| 49 | /** |
||
| 50 | * @var boolean |
||
| 51 | */ |
||
| 52 | protected static $preventFailureCount = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var RestrictionIdentifierInterface |
||
| 56 | */ |
||
| 57 | protected $restrictionIdentifier; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $clientIdentifier; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var \Aoe\FeloginBruteforceProtection\System\Configuration |
||
| 66 | */ |
||
| 67 | protected $configuration; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var \Aoe\FeloginBruteforceProtection\Domain\Repository\EntryRepository |
||
| 71 | */ |
||
| 72 | protected $entryRepository; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager |
||
| 76 | */ |
||
| 77 | protected $persistenceManager; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface |
||
| 81 | */ |
||
| 82 | protected $objectManager; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var Entry |
||
| 86 | */ |
||
| 87 | protected $entry; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var boolean |
||
| 91 | */ |
||
| 92 | protected $clientRestricted; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var Logger |
||
| 96 | */ |
||
| 97 | protected $logger; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var FeLoginBruteForceApi |
||
| 101 | */ |
||
| 102 | protected $feLoginBruteForceApi; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param RestrictionIdentifierInterface $restrictionIdentifier |
||
| 106 | */ |
||
| 107 | 26 | public function __construct(RestrictionIdentifierInterface $restrictionIdentifier) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @param boolean $preventFailureCount |
||
| 121 | * @return void |
||
| 122 | */ |
||
| 123 | public static function setPreventFailureCount($preventFailureCount) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return boolean |
||
| 130 | */ |
||
| 131 | 16 | public function isClientRestricted() |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @return void |
||
| 145 | */ |
||
| 146 | 10 | public function removeEntry() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | public function checkAndHandleRestriction() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return void |
||
| 188 | */ |
||
| 189 | protected function restrictionLog() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param $message |
||
| 207 | * @param $severity |
||
| 208 | */ |
||
| 209 | 10 | private function log($message, $severity) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @return Logger |
||
| 233 | */ |
||
| 234 | 10 | private function getLogger() |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @return void |
||
| 244 | */ |
||
| 245 | private function createEntry() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @return void |
||
| 260 | */ |
||
| 261 | private function saveEntry() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param Entry $entry |
||
| 275 | * @return boolean |
||
| 276 | */ |
||
| 277 | 16 | private function isRestricted(Entry $entry) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @return boolean |
||
| 289 | */ |
||
| 290 | 16 | public function hasEntry() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @return Entry|null |
||
| 297 | */ |
||
| 298 | 16 | public function getEntry() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param Entry $entry |
||
| 314 | * @return boolean |
||
| 315 | */ |
||
| 316 | 16 | private function isOutdated(Entry $entry) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @param Entry $entry |
||
| 326 | * @return boolean |
||
| 327 | */ |
||
| 328 | 8 | private function isResetTimeOver(Entry $entry) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @param Entry $entry |
||
| 335 | * @return boolean |
||
| 336 | */ |
||
| 337 | 16 | private function hasMaximumNumberOfFailuresReached(Entry $entry) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * @param Entry $entry |
||
| 344 | * @return boolean |
||
| 345 | */ |
||
| 346 | 8 | private function isRestrictionTimeReached(Entry $entry) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Returns the client identifier based on the clients IP address. |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | 16 | private function getClientIdentifier() |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @return FeLoginBruteForceApi |
||
| 368 | */ |
||
| 369 | protected function getFeLoginBruteForceApi() |
||
| 378 | } |
||
| 379 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: