Conditions | 10 |
Paths | 14 |
Total Lines | 30 |
Code Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
101 | public function save() |
||
102 | { |
||
103 | foreach ($this->getAllProperties() as $property => $value) { |
||
104 | if ($property === 'password' || $property === 'newpassword') { |
||
105 | // update password only if new is set and length >= 3 |
||
106 | if ($this->newpassword && Str::length($this->newpassword) >= 3) { |
||
107 | $this->_user->password = Crypt::passwordHash($this->newpassword); |
||
108 | } |
||
109 | } elseif ($property === 'approved') { |
||
110 | if ($this->approved) { |
||
111 | $this->_user->approve_token = null; |
||
112 | } else { |
||
113 | $this->_user->approve_token = $this->approve_token ?? Crypt::randomString(mt_rand(32, 128)); |
||
114 | } |
||
115 | } elseif ($property === 'approve_token') { |
||
116 | continue; |
||
117 | } else { |
||
118 | $this->_user->{$property} = $value; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | // get user id before save to determine "add" action |
||
123 | $id = $this->_user->id; |
||
124 | // safe user row |
||
125 | $this->_user->save(); |
||
126 | // if new user - add profile link |
||
127 | if ($id < 1) { |
||
128 | $profile = new Profile(); |
||
129 | $profile->user_id = $this->_user->id; |
||
130 | $profile->save(); |
||
131 | } |
||
168 |