Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 23 | class RequestValidationHelper |
||
| 24 | { |
||
| 25 | /** @var IBanHelper */ |
||
| 26 | private $banHelper; |
||
| 27 | /** @var Request */ |
||
| 28 | private $request; |
||
| 29 | private $emailConfirmation; |
||
| 30 | /** @var PdoDatabase */ |
||
| 31 | private $database; |
||
| 32 | /** @var IAntiSpoofProvider */ |
||
| 33 | private $antiSpoofProvider; |
||
| 34 | /** @var IXffTrustProvider */ |
||
| 35 | private $xffTrustProvider; |
||
| 36 | /** @var HttpHelper */ |
||
| 37 | private $httpHelper; |
||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $mediawikiApiEndpoint; |
||
| 42 | private $titleBlacklistEnabled; |
||
| 43 | /** |
||
| 44 | * @var TorExitProvider |
||
| 45 | */ |
||
| 46 | private $torExitProvider; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Summary of __construct |
||
| 50 | * |
||
| 51 | * @param IBanHelper $banHelper |
||
| 52 | * @param Request $request |
||
| 53 | * @param string $emailConfirmation |
||
| 54 | * @param PdoDatabase $database |
||
| 55 | * @param IAntiSpoofProvider $antiSpoofProvider |
||
| 56 | * @param IXffTrustProvider $xffTrustProvider |
||
| 57 | * @param HttpHelper $httpHelper |
||
| 58 | * @param string $mediawikiApiEndpoint |
||
| 59 | * @param boolean $titleBlacklistEnabled |
||
| 60 | * @param TorExitProvider $torExitProvider |
||
| 61 | */ |
||
| 62 | 1 | public function __construct( |
|
| 63 | IBanHelper $banHelper, |
||
| 64 | Request $request, |
||
| 65 | $emailConfirmation, |
||
| 66 | PdoDatabase $database, |
||
| 67 | IAntiSpoofProvider $antiSpoofProvider, |
||
| 68 | IXffTrustProvider $xffTrustProvider, |
||
| 69 | HttpHelper $httpHelper, |
||
| 70 | $mediawikiApiEndpoint, |
||
| 71 | $titleBlacklistEnabled, |
||
| 72 | TorExitProvider $torExitProvider |
||
| 73 | ) { |
||
| 74 | 1 | $this->banHelper = $banHelper; |
|
| 75 | 1 | $this->request = $request; |
|
| 76 | 1 | $this->emailConfirmation = $emailConfirmation; |
|
| 77 | 1 | $this->database = $database; |
|
| 78 | 1 | $this->antiSpoofProvider = $antiSpoofProvider; |
|
| 79 | 1 | $this->xffTrustProvider = $xffTrustProvider; |
|
| 80 | 1 | $this->httpHelper = $httpHelper; |
|
| 81 | 1 | $this->mediawikiApiEndpoint = $mediawikiApiEndpoint; |
|
| 82 | 1 | $this->titleBlacklistEnabled = $titleBlacklistEnabled; |
|
| 83 | 1 | $this->torExitProvider = $torExitProvider; |
|
| 84 | 1 | } |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Summary of validateName |
||
| 88 | * @return ValidationError[] |
||
| 89 | */ |
||
| 90 | 1 | public function validateName() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Summary of validateEmail |
||
| 137 | * @return ValidationError[] |
||
| 138 | */ |
||
| 139 | public function validateEmail() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Summary of validateOther |
||
| 180 | * @return ValidationError[] |
||
| 181 | */ |
||
| 182 | public function validateOther() |
||
| 183 | { |
||
| 184 | $errorList = array(); |
||
| 185 | |||
| 186 | $trustedIp = $this->xffTrustProvider->getTrustedClientIp($this->request->getIp(), |
||
| 187 | $this->request->getForwardedIp()); |
||
| 188 | |||
| 189 | // ERRORS |
||
| 190 | |||
| 191 | // TOR nodes |
||
| 192 | if ($this->torExitProvider->isTorExit($trustedIp)) { |
||
| 193 | $errorList[ValidationError::BANNED] = new ValidationError(ValidationError::BANNED_TOR); |
||
| 194 | } |
||
| 195 | |||
| 196 | // IP banned |
||
| 197 | $ban = $this->banHelper->ipIsBanned($trustedIp); |
||
| 198 | if ($ban != false) { |
||
| 199 | $errorList[ValidationError::BANNED] = new ValidationError(ValidationError::BANNED); |
||
| 200 | } |
||
| 201 | |||
| 202 | // WARNINGS |
||
| 203 | |||
| 204 | // Antispoof check |
||
| 205 | $this->checkAntiSpoof(); |
||
| 206 | |||
| 207 | // Blacklist check |
||
| 208 | $this->checkTitleBlacklist(); |
||
| 209 | |||
| 210 | return $errorList; |
||
| 211 | } |
||
| 212 | |||
| 213 | private function checkAntiSpoof() |
||
| 214 | { |
||
| 215 | try { |
||
| 216 | if (count($this->antiSpoofProvider->getSpoofs($this->request->getName())) > 0) { |
||
| 217 | // If there were spoofs an Admin should handle the request. |
||
| 218 | $this->request->setStatus("Flagged users"); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | catch (Exception $ex) { |
||
| 222 | // logme |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | private function checkTitleBlacklist() |
||
| 249 | |||
| 250 | 1 | private function userExists() |
|
| 269 | |||
| 270 | 1 | private function userSulExists() |
|
| 271 | { |
||
| 272 | 1 | $requestName = $this->request->getName(); |
|
| 273 | |||
| 274 | 1 | $userExists = $this->httpHelper->get( |
|
| 275 | 1 | $this->mediawikiApiEndpoint, |
|
| 276 | array( |
||
| 277 | 1 | 'action' => 'query', |
|
| 278 | 1 | 'meta' => 'globaluserinfo', |
|
| 279 | 1 | 'guiuser' => $requestName, |
|
| 280 | 1 | 'format' => 'php', |
|
| 281 | ) |
||
| 282 | ); |
||
| 283 | |||
| 284 | 1 | $ue = unserialize($userExists); |
|
| 285 | 1 | if (isset ($ue['query']['globaluserinfo']['id'])) { |
|
| 286 | return true; |
||
| 287 | } |
||
| 288 | |||
| 289 | 1 | return false; |
|
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Checks if a request with this name is currently open |
||
| 294 | * |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | 1 | View Code Duplication | private function nameRequestExists() |
| 309 | } |
||
| 310 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.