| Conditions | 10 |
| Paths | 32 |
| Total Lines | 50 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 32 | public function addAddress($data) |
||
| 33 | { |
||
| 34 | $data = filter_var_array($data, FILTER_SANITIZE_STRIPPED); |
||
| 35 | |||
| 36 | if (empty($data)) { |
||
| 37 | $this->Message->message = 'Campos inválidos!'; |
||
| 38 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 39 | } |
||
| 40 | |||
| 41 | $User = new User(); |
||
| 42 | /** @var Source\Models\User $User */ |
||
| 43 | $User = $User->findById($this->token['id'], 'id'); |
||
| 44 | |||
| 45 | if (!$User || $User == null) { |
||
| 46 | $this->Message->message = 'Usuário não encontrado!'; |
||
| 47 | (new Response())->setStatusCode(HTTP_NOT_FOUND)->send($this->Message); |
||
| 48 | } |
||
| 49 | |||
| 50 | $city_id = filter_var($data['city_id'], FILTER_DEFAULT); |
||
| 51 | $street = filter_var($data['street'], FILTER_DEFAULT); |
||
| 52 | $complement = filter_var($data['complement'], FILTER_DEFAULT); |
||
| 53 | $cep = filter_var($data['cep'], FILTER_DEFAULT); |
||
| 54 | $number = filter_var($data['number'], FILTER_DEFAULT); |
||
| 55 | |||
| 56 | if (!$city_id || !$street || !$cep || !$number) { |
||
| 57 | $this->Message->message = 'Endereço inválido!'; |
||
| 58 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** @var Source\Models\Address $Address */ |
||
| 62 | $Address = new Address(); |
||
| 63 | |||
| 64 | $Address->city_id = $city_id; |
||
| 65 | $Address->street = $street; |
||
| 66 | $Address->complement = $complement; |
||
| 67 | $Address->cep = $cep; |
||
| 68 | $Address->number = $number; |
||
| 69 | |||
| 70 | if (!$Address->save()) { |
||
| 71 | $this->Message->message = $Address->message(); |
||
| 72 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 73 | } |
||
| 74 | |||
| 75 | if (!Address::bindAddress($User->id, $Address->id)) { |
||
|
|
|||
| 76 | $this->Message->message = 'Ocorreu algum erro ao cadastrar o endereço!'; |
||
| 77 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 78 | } |
||
| 79 | |||
| 80 | $this->Message->message = 'Endereço salvo com sucesso!'; |
||
| 81 | (new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
||
| 82 | } |
||
| 178 |
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.