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 |
||
6 | class RequestValidationHelper |
||
7 | { |
||
8 | private $banHelper; |
||
9 | private $request; |
||
10 | private $emailConfirmation; |
||
11 | |||
12 | /** |
||
13 | * Summary of __construct |
||
14 | * @param IBanHelper $banHelper |
||
15 | * @param Request $request |
||
16 | * @param string $emailConfirmation |
||
17 | */ |
||
18 | 1 | public function __construct(IBanHelper $banHelper, Request $request, $emailConfirmation) |
|
19 | { |
||
20 | 1 | $this->banHelper = $banHelper; |
|
21 | 1 | $this->request = $request; |
|
22 | 1 | $this->emailConfirmation = $emailConfirmation; |
|
23 | 1 | } |
|
24 | |||
25 | /** |
||
26 | * Summary of validateName |
||
27 | * @return ValidationError[] |
||
28 | */ |
||
29 | 1 | public function validateName() |
|
30 | { |
||
31 | 1 | $errorList = array(); |
|
32 | |||
33 | // ERRORS |
||
34 | // name is empty |
||
35 | 1 | if (trim($this->request->getName()) == "") { |
|
36 | $errorList[ValidationError::NAME_EMPTY] = new ValidationError(ValidationError::NAME_EMPTY); |
||
37 | } |
||
38 | |||
39 | // name is banned |
||
40 | 1 | $ban = $this->banHelper->nameIsBanned($this->request->getName()); |
|
41 | 1 | if ($ban != false) { |
|
|
|||
42 | $errorList[ValidationError::BANNED] = new ValidationError(ValidationError::BANNED); |
||
43 | } |
||
44 | |||
45 | // username already exists |
||
46 | // TODO: implement |
||
47 | 1 | if ($this->userExists()) { |
|
48 | $errorList[ValidationError::NAME_EXISTS] = new ValidationError(ValidationError::NAME_EXISTS); |
||
49 | } |
||
50 | |||
51 | // username part of SUL account |
||
52 | // TODO: implement |
||
53 | 1 | if ($this->userSulExists()) { |
|
54 | // using same error slot as name exists - it's the same sort of error, and we probably only want to show one. |
||
55 | $errorList[ValidationError::NAME_EXISTS] = new ValidationError(ValidationError::NAME_EXISTS_SUL); |
||
56 | } |
||
57 | |||
58 | // username is numbers |
||
59 | 1 | if (preg_match("/^[0-9]+$/", $this->request->getName()) === 1) { |
|
60 | $errorList[ValidationError::NAME_NUMONLY] = new ValidationError(ValidationError::NAME_NUMONLY); |
||
61 | } |
||
62 | |||
63 | // username can't contain #@/<>[]|{} |
||
64 | 1 | if (preg_match("/[" . preg_quote("#@/<>[]|{}", "/") . "]/", $this->request->getName()) === 1) { |
|
65 | $errorList[ValidationError::NAME_INVALIDCHAR] = new ValidationError(ValidationError::NAME_INVALIDCHAR); |
||
66 | } |
||
67 | |||
68 | // existing non-closed request for this name |
||
69 | // TODO: implement |
||
70 | 1 | if ($this->nameRequestExists()) { |
|
71 | $errorList[ValidationError::OPEN_REQUEST_NAME] = new ValidationError(ValidationError::OPEN_REQUEST_NAME); |
||
72 | } |
||
73 | |||
74 | // WARNINGS |
||
75 | // name has to be sanitised |
||
76 | // TODO: implement |
||
77 | 1 | if (false) { |
|
78 | $errorList[ValidationError::NAME_SANITISED] = new ValidationError(ValidationError::NAME_SANITISED, false); |
||
79 | } |
||
80 | |||
81 | 1 | return $errorList; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Summary of validateEmail |
||
86 | * @return ValidationError[] |
||
87 | */ |
||
88 | public function validateEmail() |
||
89 | { |
||
90 | $errorList = array(); |
||
91 | |||
92 | // ERRORS |
||
93 | |||
94 | // Email is banned |
||
95 | $ban = $this->banHelper->emailIsBanned($this->request->getEmail()); |
||
96 | if ($ban != false) { |
||
97 | $errorList[ValidationError::BANNED] = new ValidationError(ValidationError::BANNED); |
||
98 | } |
||
99 | |||
100 | // email addresses must match |
||
101 | if ($this->request->getEmail() != $this->emailConfirmation) { |
||
102 | $errorList[ValidationError::EMAIL_MISMATCH] = new ValidationError(ValidationError::EMAIL_MISMATCH); |
||
103 | } |
||
104 | |||
105 | // email address must be validly formed |
||
106 | if (trim($this->request->getEmail()) == "") { |
||
107 | $errorList[ValidationError::EMAIL_EMPTY] = new ValidationError(ValidationError::EMAIL_EMPTY); |
||
108 | } |
||
109 | |||
110 | // email address must be validly formed |
||
111 | if (!filter_var($this->request->getEmail(), FILTER_VALIDATE_EMAIL)) { |
||
112 | if (trim($this->request->getEmail()) != "") { |
||
113 | $errorList[ValidationError::EMAIL_INVALID] = new ValidationError(ValidationError::EMAIL_INVALID); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | // email address can't be wikimedia/wikipedia .com/org |
||
118 | if (preg_match('/.*@.*wiki(m.dia|p.dia)\.(org|com)/i', $this->request->getEmail()) === 1) { |
||
119 | $errorList[ValidationError::EMAIL_WIKIMEDIA] = new ValidationError(ValidationError::EMAIL_WIKIMEDIA); |
||
120 | } |
||
121 | |||
122 | // WARNINGS |
||
123 | |||
124 | return $errorList; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Summary of validateOther |
||
129 | * @return ValidationError[] |
||
130 | */ |
||
131 | public function validateOther() |
||
158 | } |
||
159 | |||
160 | private function checkAntiSpoof() |
||
161 | { |
||
162 | global $antispoofProvider; |
||
163 | try { |
||
164 | if (count($antispoofProvider->getSpoofs($this->request->getName())) > 0) { |
||
165 | // If there were spoofs an Admin should handle the request. |
||
166 | $this->request->setStatus("Flagged users"); |
||
167 | } |
||
168 | } |
||
169 | catch (Exception $ex) { |
||
170 | // hrm. |
||
171 | // TODO: log this? |
||
172 | } |
||
173 | } |
||
174 | |||
175 | private function checkTitleBlacklist() |
||
176 | { |
||
177 | global $enableTitleblacklist; |
||
178 | if ($enableTitleblacklist == 1) { |
||
179 | $apiResult = file_get_contents("https://en.wikipedia.org/w/api.php?action=titleblacklist&tbtitle=" . urlencode($this->request->getName()) . "&tbaction=new-account&tbnooverride&format=php"); |
||
180 | |||
181 | $data = unserialize($apiResult); |
||
182 | |||
183 | $requestIsOk = $data['titleblacklist']['result'] == "ok"; |
||
184 | |||
185 | if (!$requestIsOk) { |
||
186 | $this->request->setStatus("Flagged users"); |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | |||
191 | 1 | private function userExists() |
|
202 | } |
||
203 | |||
204 | 1 | private function userSulExists() |
|
216 | } |
||
217 | |||
218 | 1 | private function nameRequestExists() |
|
219 | { |
||
220 | 1 | $query = "SELECT COUNT(id) FROM request WHERE status != 'Closed' AND name = :name;"; |
|
229 | } |
||
230 | } |
||
231 |