Conditions | 13 |
Paths | 10 |
Total Lines | 73 |
Code Lines | 48 |
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 |
||
111 | public function updateAddress($data) |
||
112 | { |
||
113 | $data = filter_var_array($data, FILTER_SANITIZE_STRIPPED); |
||
114 | |||
115 | if (empty($data) || !isset($data['id']) || !filter_var($data['id'], FILTER_VALIDATE_INT)) { |
||
116 | $this->Message->message = 'Campos inválidos!'; |
||
117 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
118 | return; |
||
119 | } |
||
120 | |||
121 | $User = new User(); |
||
122 | $User = $User->findById($this->token['id'], 'id'); |
||
123 | |||
124 | if (!$User || $User == null) { |
||
125 | $this->Message->message = 'Usuário não encontrado!'; |
||
126 | (new Response())->setStatusCode(HTTP_NOT_FOUND)->send($this->Message); |
||
127 | return; |
||
128 | } |
||
129 | |||
130 | if (!Address::getBindedAddress($User->id, $data['id'])) { |
||
131 | $this->Message->message = 'Você não possui permissão para editar este endereço!'; |
||
132 | (new Response())->setStatusCode(HTTP_NOT_FOUND)->send($this->Message); |
||
133 | return; |
||
134 | } |
||
135 | |||
136 | if (empty($data['city_id'])) { |
||
137 | $this->Message->message = 'Cidade inválida!'; |
||
138 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
139 | return; |
||
140 | } |
||
141 | |||
142 | $City = new City(); |
||
143 | |||
144 | if (!$City->findById($data['city_id'])) { |
||
145 | $this->Message->message = 'Cidade inválida!'; |
||
146 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
147 | return; |
||
148 | } |
||
149 | |||
150 | if (empty($data['street'])) { |
||
151 | $this->Message->message = 'Nome da rua inválida!'; |
||
152 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
153 | return; |
||
154 | } |
||
155 | |||
156 | if (empty($data['cep'])) { |
||
157 | $this->Message->message = 'CEP inválido!'; |
||
158 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
159 | return; |
||
160 | } |
||
161 | |||
162 | if (empty($data['number'])) { |
||
163 | $this->Message->message = 'Número da casa inválido!'; |
||
164 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
165 | return; |
||
166 | } |
||
167 | |||
168 | /** @var Source\Models\Address $Address */ |
||
169 | $Address = new Address(); |
||
170 | $Address->city_id = $data['city_id']; |
||
171 | $Address->street = $data['street']; |
||
172 | $Address->cep = $data['cep']; |
||
173 | $Address->number = $data['number']; |
||
174 | |||
175 | if (!$Address->save()) { |
||
176 | $this->Message->message = $Address->message(); |
||
177 | (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message); |
||
178 | return; |
||
179 | } |
||
180 | |||
181 | $this->Message->message = 'Endereço salvo com sucesso!'; |
||
182 | (new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
||
183 | return; |
||
184 | } |
||
212 |
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.