| Conditions | 8 |
| Paths | 7 |
| Total Lines | 53 |
| Code Lines | 34 |
| 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 |
||
| 78 | public function updatePhone($data) |
||
| 79 | { |
||
| 80 | $data = filter_var_array($data, FILTER_SANITIZE_STRIPPED); |
||
| 81 | |||
| 82 | if (empty($data)) { |
||
| 83 | $this->Message->message = 'Campos inválidos!'; |
||
| 84 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 85 | return; |
||
| 86 | } |
||
| 87 | |||
| 88 | $User = new User(); |
||
| 89 | $User = $User->findById($this->token['id'], 'id'); |
||
| 90 | |||
| 91 | if (!$User || $User == null) { |
||
| 92 | $this->Message->message = 'Usuário não encontrado!'; |
||
| 93 | (new Response())->setStatusCode(HTTP_NOT_FOUND)->send($this->Message); |
||
| 94 | return; |
||
| 95 | } |
||
| 96 | |||
| 97 | if (!filter_var($data['id'], FILTER_VALIDATE_INT)) { |
||
| 98 | $this->Message->message = 'Número de telefone inválido!'; |
||
| 99 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 100 | return; |
||
| 101 | } |
||
| 102 | |||
| 103 | $Phone = new Phone(); |
||
| 104 | /** @var Source\Models\Phone $Phone */ |
||
| 105 | $Phone = $Phone->findById($data['id'], 'id, user_id'); |
||
| 106 | |||
| 107 | if ($User->id != $Phone->user_id) { |
||
|
|
|||
| 108 | $this->Message->message = 'Você não possui permisão para alterar este telefone!'; |
||
| 109 | (new Response())->setStatusCode(HTTP_NOT_FOUND)->send($this->Message); |
||
| 110 | return; |
||
| 111 | } |
||
| 112 | |||
| 113 | if (empty($data['number'])) { |
||
| 114 | $this->Message->message = 'Número de telefone inválido!'; |
||
| 115 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 116 | return; |
||
| 117 | } |
||
| 118 | |||
| 119 | $Phone->phone_type_id = 1; |
||
| 120 | $Phone->number = $data['number']; |
||
| 121 | |||
| 122 | if (!$Phone->save()) { |
||
| 123 | $this->Message->message = $Phone->message(); |
||
| 124 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
| 125 | return; |
||
| 126 | } |
||
| 127 | |||
| 128 | $this->Message->message = 'Alteração realizada com sucesso!'; |
||
| 129 | (new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
||
| 130 | return; |
||
| 131 | } |
||
| 133 |