Conditions | 28 |
Paths | 16 |
Total Lines | 101 |
Code Lines | 65 |
Lines | 27 |
Ratio | 26.73 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
152 | public function validate(EnvelopeInterface $envelope) |
||
153 | { |
||
154 | $details = []; |
||
155 | |||
156 | // Checking sequenceId |
||
157 | if (strlen($envelope->getSequenceId()) < 6 || strlen($envelope->getSequenceId()) > 40) { |
||
158 | $details[] = sprintf( |
||
159 | 'Invalid SequenceID length. It must be in range [6, 40], but %d received.', |
||
160 | strlen($envelope->getSequenceId()) |
||
161 | ); |
||
162 | } |
||
163 | |||
164 | // Checking identity nodes |
||
165 | if (count($envelope->getIdentities()) === 0) { |
||
166 | $details[] = 'At least one Identity must be supplied'; |
||
167 | } |
||
168 | |||
169 | // Checking envelope type |
||
170 | if (!isset(self::$types[$envelope->getType()])) { |
||
171 | $details[] = sprintf('Envelope type "%s" not supported by this client version', $envelope->getType()); |
||
172 | } else { |
||
173 | $typeInfo = self::$types[$envelope->getType()]; |
||
174 | // Mandatory fields check |
||
175 | foreach ($typeInfo['mandatory'] as $name) { |
||
176 | if (!isset($envelope[$name]) || empty($envelope[$name])) { |
||
177 | $details[] = sprintf( |
||
178 | 'Field "%s" is mandatory for "%s", but not provided', |
||
179 | $name, |
||
180 | $envelope->getType() |
||
181 | ); |
||
182 | } |
||
183 | } |
||
184 | |||
185 | // Per field check |
||
186 | $fields = array_merge($typeInfo['mandatory'], $typeInfo['optional']); |
||
187 | $customCount = 0; |
||
188 | foreach ($envelope as $key => $value) { |
||
189 | // Is custom? |
||
190 | if (strlen($key) >= 7 && substr($key, 0, 7) === 'custom_') { |
||
191 | $customCount++; |
||
192 | if (!is_string($value)) { |
||
193 | $details[] = sprintf( |
||
194 | 'All custom values must be string, but for "%s" %s was provided', |
||
195 | $key, |
||
196 | $value === null ? 'null' : gettype($value) |
||
197 | ); |
||
198 | } |
||
199 | } else { |
||
200 | if (!in_array($key, $fields)) { |
||
201 | $details[] = sprintf('Field "%s" not found in "%s"', $key, $envelope->getType()); |
||
202 | } else { |
||
203 | // Checking type |
||
204 | switch (self::$dataTypes[$key]) { |
||
205 | case 'string': |
||
206 | if (!is_string($value)) { |
||
207 | $details[] = sprintf( |
||
208 | 'Field "%s" must be string, but %s provided', |
||
209 | $key, |
||
210 | $value === null ? 'null' : gettype($value) |
||
211 | ); |
||
212 | } |
||
213 | break; |
||
214 | View Code Duplication | case 'int': |
|
|
|||
215 | if (!is_int($value)) { |
||
216 | $details[] = sprintf( |
||
217 | 'Field "%s" must be int, but %s provided', |
||
218 | $key, |
||
219 | $value === null ? 'null' : gettype($value) |
||
220 | ); |
||
221 | } |
||
222 | break; |
||
223 | View Code Duplication | case 'float': |
|
224 | if (!is_float($value) && !is_int($value)) { |
||
225 | $details[] = sprintf( |
||
226 | 'Field "%s" must be float/double, but %s provided', |
||
227 | $key, |
||
228 | $value === null ? 'null' : gettype($value) |
||
229 | ); |
||
230 | } |
||
231 | break; |
||
232 | View Code Duplication | case 'bool': |
|
233 | if (!is_bool($value)) { |
||
234 | $details[] = sprintf( |
||
235 | 'Field "%s" must be boolean, but %s provided', |
||
236 | $key, |
||
237 | $value === null ? 'null' : gettype($value) |
||
238 | ); |
||
239 | } |
||
240 | break; |
||
241 | default: |
||
242 | $details[] = sprintf('Unknown type for "%s"', $key); |
||
243 | } |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | } |
||
248 | |||
249 | if (count($details) > 0) { |
||
250 | throw new EnvelopeValidationException($details); |
||
251 | } |
||
252 | } |
||
253 | } |
||
254 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.