| Conditions | 10 |
| Paths | 9 |
| Total Lines | 74 |
| Code Lines | 49 |
| 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 |
||
| 35 | public function addAddress($data) |
||
| 36 | { |
||
| 37 | $data = filter_var_array($data, FILTER_SANITIZE_STRIPPED); |
||
| 38 | |||
| 39 | if (empty($data)) { |
||
| 40 | $this->Message->message = 'Campos inválidos!'; |
||
| 41 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 42 | return; |
||
| 43 | } |
||
| 44 | |||
| 45 | $User = new User(); |
||
| 46 | /** @var Source\Models\User $User */ |
||
| 47 | $User = $User->findById($this->token['id'], 'id'); |
||
| 48 | |||
| 49 | if (!$User || $User == null) { |
||
| 50 | $this->Message->message = 'Usuário não encontrado!'; |
||
| 51 | (new Response())->setStatusCode(HTTP_NOT_FOUND)->send($this->Message); |
||
| 52 | return; |
||
| 53 | } |
||
| 54 | |||
| 55 | $city_id = filter_var($data['city_id'], FILTER_DEFAULT); |
||
| 56 | $street = filter_var($data['street'], FILTER_DEFAULT); |
||
| 57 | $complement = filter_var($data['complement'], FILTER_DEFAULT); |
||
| 58 | $cep = filter_var($data['cep'], FILTER_DEFAULT); |
||
| 59 | $number = filter_var($data['number'], FILTER_DEFAULT); |
||
| 60 | |||
| 61 | if (!$city_id) { |
||
| 62 | $this->Message->message = 'Cidade inválida!'; |
||
| 63 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 64 | return; |
||
| 65 | } |
||
| 66 | |||
| 67 | if (!$street) { |
||
| 68 | $this->Message->message = 'Nome da rua inválida!'; |
||
| 69 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 70 | return; |
||
| 71 | } |
||
| 72 | |||
| 73 | if (!$cep) { |
||
| 74 | $this->Message->message = 'CEP inválido!'; |
||
| 75 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 76 | return; |
||
| 77 | } |
||
| 78 | |||
| 79 | if (!$number) { |
||
| 80 | $this->Message->message = 'Número da casa inválido!'; |
||
| 81 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 82 | return; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** @var Source\Models\Address $Address */ |
||
| 86 | $Address = new Address(); |
||
| 87 | |||
| 88 | $Address->city_id = $city_id; |
||
| 89 | $Address->street = $street; |
||
| 90 | $Address->complement = $complement; |
||
| 91 | $Address->cep = $cep; |
||
| 92 | $Address->number = $number; |
||
| 93 | |||
| 94 | if (!$Address->save()) { |
||
| 95 | $this->Message->message = $Address->message(); |
||
| 96 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 97 | return; |
||
| 98 | } |
||
| 99 | |||
| 100 | if (!Address::bindAddress($User->id, $Address->id)) { |
||
|
|
|||
| 101 | $this->Message->message = 'Ocorreu algum erro ao cadastrar o endereço!'; |
||
| 102 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 103 | return; |
||
| 104 | } |
||
| 105 | |||
| 106 | $this->Message->message = 'Endereço salvo com sucesso!'; |
||
| 107 | (new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
||
| 108 | return; |
||
| 109 | } |
||
| 212 |
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.