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