| Conditions | 10 |
| Paths | 13 |
| Total Lines | 55 |
| 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 |
||
| 75 | public function save(): bool |
||
| 76 | { |
||
| 77 | if (!$this->required()) { |
||
| 78 | $this->error = "Nome, email e senha são obrigatórios"; |
||
| 79 | return false; |
||
| 80 | } |
||
| 81 | |||
| 82 | if (!is_email($this->email)) { |
||
| 83 | $this->error = "O e-mail informado não tem um formato válido"; |
||
| 84 | return false; |
||
| 85 | } |
||
| 86 | |||
| 87 | if (!is_passwd($this->password)) { |
||
| 88 | $min = CONF_PASSWD_MIN_LEN; |
||
| 89 | $max = CONF_PASSWD_MAX_LEN; |
||
| 90 | $this->error = "A senha deve ter entre {$min} e {$max} caracteres"; |
||
| 91 | return false; |
||
| 92 | } else { |
||
| 93 | $this->password = passwd($this->password); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** User Update */ |
||
| 97 | if (!empty($this->id)) { |
||
| 98 | $userId = $this->id; |
||
| 99 | |||
| 100 | if ($this->find("email = :e AND id != :i", "e={$this->email}&i={$userId}", "id")->fetch()) { |
||
| 101 | $this->error = "O e-mail informado já está cadastrado"; |
||
| 102 | return false; |
||
| 103 | } |
||
| 104 | |||
| 105 | $this->update($this->safe(), "id = :id", "id={$userId}"); |
||
| 106 | if ($this->fail()) { |
||
| 107 | error_log(($this->fail())->getMessage()); |
||
| 108 | $this->error = "Erro ao atualizar, verifique os dados"; |
||
| 109 | return false; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | /** User Create */ |
||
| 114 | if (empty($this->id)) { |
||
| 115 | if ($this->findByEmail($this->email, 'id')) { |
||
| 116 | $this->error = "O e-mail informado já está cadastrado"; |
||
| 117 | return false; |
||
| 118 | } |
||
| 119 | |||
| 120 | $userId = $this->create($this->safe()); |
||
| 121 | if ($this->fail()) { |
||
| 122 | error_log(($this->fail())->getMessage()); |
||
| 123 | $this->error = "Erro ao cadastrar, verifique os dados"; |
||
| 124 | return false; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | $this->data = ($this->findById($userId))->data(); |
||
| 129 | return true; |
||
| 130 | } |
||
| 132 |