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 |
||
| 48 | class RestrictionService |
||
| 49 | { |
||
| 50 | /** |
||
| 51 | * @var boolean |
||
| 52 | */ |
||
| 53 | protected static $preventFailureCount = false; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var RestrictionIdentifierInterface |
||
| 57 | */ |
||
| 58 | protected $restrictionIdentifier; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $clientIdentifier; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var \Aoe\FeloginBruteforceProtection\System\Configuration |
||
| 67 | */ |
||
| 68 | protected $configuration; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var \Aoe\FeloginBruteforceProtection\Domain\Repository\EntryRepository |
||
| 72 | */ |
||
| 73 | protected $entryRepository; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager |
||
| 77 | */ |
||
| 78 | protected $persistenceManager; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface |
||
| 82 | */ |
||
| 83 | protected $objectManager; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var Entry |
||
| 87 | */ |
||
| 88 | protected $entry; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var boolean |
||
| 92 | */ |
||
| 93 | protected $clientRestricted; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var Logger |
||
| 97 | */ |
||
| 98 | protected $logger; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var FeLoginBruteForceApi |
||
| 102 | */ |
||
| 103 | protected $feLoginBruteForceApi; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param RestrictionIdentifierInterface $restrictionIdentifier |
||
| 107 | */ |
||
| 108 | 26 | public function __construct(RestrictionIdentifierInterface $restrictionIdentifier) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @param boolean $preventFailureCount |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | public static function setPreventFailureCount($preventFailureCount) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return boolean |
||
| 131 | */ |
||
| 132 | 16 | public function isClientRestricted() |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @return void |
||
| 146 | */ |
||
| 147 | 10 | public function removeEntry() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | public function checkAndHandleRestriction() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return void |
||
| 189 | */ |
||
| 190 | protected function restrictionLog() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param $message |
||
| 208 | * @param $severity |
||
| 209 | */ |
||
| 210 | 10 | private function log($message, $severity) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @return Logger |
||
| 234 | */ |
||
| 235 | 10 | private function getLogger() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @return void |
||
| 245 | */ |
||
| 246 | private function createEntry() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | private function saveEntry() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param Entry $entry |
||
| 276 | * @return boolean |
||
| 277 | */ |
||
| 278 | 16 | private function isRestricted(Entry $entry) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @return boolean |
||
| 290 | */ |
||
| 291 | 16 | public function hasEntry() |
|
| 295 | |||
| 296 | /** |
||
| 297 | * @return Entry|null |
||
| 298 | */ |
||
| 299 | 16 | public function getEntry() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @param Entry $entry |
||
| 315 | * @return boolean |
||
| 316 | */ |
||
| 317 | 16 | private function isOutdated(Entry $entry) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * @param Entry $entry |
||
| 327 | * @return boolean |
||
| 328 | */ |
||
| 329 | 8 | private function isResetTimeOver(Entry $entry) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * @param Entry $entry |
||
| 336 | * @return boolean |
||
| 337 | */ |
||
| 338 | 16 | private function hasMaximumNumberOfFailuresReached(Entry $entry) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * @param Entry $entry |
||
| 345 | * @return boolean |
||
| 346 | */ |
||
| 347 | 8 | private function isRestrictionTimeReached(Entry $entry) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Returns the client identifier based on the clients IP address. |
||
| 354 | * |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | 16 | private function getClientIdentifier() |
|
| 366 | |||
| 367 | /** |
||
| 368 | * @return FeLoginBruteForceApi |
||
| 369 | */ |
||
| 370 | protected function getFeLoginBruteForceApi() |
||
| 379 | } |
||
| 380 |
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: