| Conditions | 13 |
| Paths | 49 |
| Total Lines | 77 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 65 | public function update($data) |
||
| 66 | { |
||
| 67 | $data = filter_var_array($data, FILTER_SANITIZE_STRIPPED); |
||
| 68 | |||
| 69 | if (empty($data)) { |
||
| 70 | $this->Message->message = 'Campos inválidos!'; |
||
| 71 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | |||
| 75 | $User = new User(); |
||
| 76 | |||
| 77 | /** @var \Source\Models\User $User */ |
||
| 78 | $User = $User->findById($this->token['id'], 'id'); |
||
| 79 | |||
| 80 | if (!$User || $User == null) { |
||
| 81 | $this->Message->message = 'Usuário não encontrado!'; |
||
| 82 | (new Response())->setStatusCode(HTTP_NOT_FOUND)->send($this->Message); |
||
| 83 | return; |
||
| 84 | } |
||
| 85 | |||
| 86 | if (isset($data['first_name'])) { |
||
| 87 | $first_name = filter_var($data["first_name"], FILTER_DEFAULT); |
||
| 88 | |||
| 89 | if (!$first_name) { |
||
| 90 | $this->Message->message = 'Nome inválido'; |
||
| 91 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 92 | return; |
||
| 93 | } |
||
| 94 | |||
| 95 | $User->first_name = $first_name; |
||
| 96 | } |
||
| 97 | |||
| 98 | if (isset($data['last_name'])) { |
||
| 99 | $last_name = filter_var($data["last_name"], FILTER_DEFAULT); |
||
| 100 | |||
| 101 | if (!$last_name) { |
||
| 102 | $this->Message->message = 'Nome inválido'; |
||
| 103 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 104 | return; |
||
| 105 | } |
||
| 106 | |||
| 107 | $User->last_name = $last_name; |
||
| 108 | } |
||
| 109 | |||
| 110 | if (isset($data['birth_date'])) { |
||
| 111 | $birth_date = filter_var($data["birth_date"], FILTER_DEFAULT); |
||
| 112 | |||
| 113 | if (!$birth_date) { |
||
| 114 | $this->Message->message = 'Data de nascimento inválida'; |
||
| 115 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 116 | return; |
||
| 117 | } |
||
| 118 | |||
| 119 | $User->birth_date = $birth_date; |
||
| 120 | } |
||
| 121 | |||
| 122 | if (isset($data['cpf'])) { |
||
| 123 | $cpf = filter_var($data["cpf"], FILTER_DEFAULT); |
||
| 124 | |||
| 125 | if (!$cpf) { |
||
| 126 | $this->Message->message = 'CPF inválido'; |
||
| 127 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 128 | return; |
||
| 129 | } |
||
| 130 | |||
| 131 | $User->cpf = $cpf; |
||
| 132 | } |
||
| 133 | |||
| 134 | if (!$User->updateUser()) { |
||
| 135 | $this->Message->message = $User->message(); |
||
| 136 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 137 | return; |
||
| 138 | } |
||
| 139 | |||
| 140 | $this->Message->message = 'Alteração realizada com sucesso'; |
||
| 141 | (new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
||
| 142 | } |
||
| 145 |