Conditions | 19 |
Paths | 810 |
Total Lines | 117 |
Code Lines | 66 |
Lines | 80 |
Ratio | 68.38 % |
Changes | 2 | ||
Bugs | 1 | Features | 2 |
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 |
||
106 | public function checkEnteredUserData( $target, $csrf_token, $selfno = '' ) |
||
107 | { |
||
108 | // 戻り値を初期化 |
||
109 | $retval = [ ]; |
||
110 | $retval[ 'entered' ] = [ ]; |
||
111 | $retval[ 'error' ][ 'msg' ] = [ ]; |
||
112 | $retval[ 'error' ][ 'form_crit' ] = [ ]; |
||
113 | |||
114 | //--- ユーザIDのチェック |
||
115 | $dup_master = \Risoluto\Auth::callProviderMethod( 'showUser', [ 'userid' => $target[ 'userid' ] ] ); |
||
116 | $retval[ 'entered' ][ 'userid' ] = htmlentities( $target[ 'userid' ], ENT_QUOTES, 'UTF-8' ); |
||
117 | View Code Duplication | if (isset( $target[ 'userid' ] ) and !empty( $target[ 'userid' ] )) { |
|
|
|||
118 | if (!empty( $selfno )) { |
||
119 | // 自分自身のユーザnoがセットされている場合は、重複データにそれが含まれていないかを確認する |
||
120 | $retval[ 'entered' ][ 'no' ] = $selfno; |
||
121 | $dups = [ ]; |
||
122 | foreach ($dup_master as $dat) { |
||
123 | if ($dat[ 'no' ] != $selfno) { |
||
124 | $dups[ ] = $dat; |
||
125 | } |
||
126 | } |
||
127 | } else { |
||
128 | // セットされていない場合は取得したものをそのまま使う |
||
129 | $dups = $dup_master; |
||
130 | } |
||
131 | |||
132 | // フォーマットチェック |
||
133 | if (!preg_match( '/[[:alnum:]\_\-\@\.]{1,255}/', $target[ 'userid' ] ) or count( $dups ) > 0) { |
||
134 | // フォーマットにそぐわない場合はエラーにする |
||
135 | $retval[ 'error' ][ 'msg' ][ ] = 'invalid_userid'; |
||
136 | $retval[ 'error' ][ 'form_crit' ][ ] = 'userid'; |
||
137 | } |
||
138 | } else { |
||
139 | // 未入力の場合はエラーにする |
||
140 | $retval[ 'entered' ][ 'userid' ] = ''; |
||
141 | $retval[ 'error' ][ 'msg' ][ ] = 'empty_userid'; |
||
142 | $retval[ 'error' ][ 'form_crit' ][ ] = 'userid'; |
||
143 | } |
||
144 | |||
145 | //--- ユーザ名のチェック |
||
146 | $retval[ 'entered' ][ 'username' ] = htmlentities( $target[ 'username' ], ENT_QUOTES, 'UTF-8' ); |
||
147 | View Code Duplication | if (isset( $target[ 'username' ] ) and !empty( $target[ 'username' ] )) { |
|
148 | // フォーマットチェック |
||
149 | if (strlen( $target[ 'username' ] ) > 255) { |
||
150 | // フォーマットにそぐわない場合はエラーにする |
||
151 | $retval[ 'error' ][ 'msg' ][ ] = 'invalid_username'; |
||
152 | $retval[ 'error' ][ 'form_crit' ][ ] = 'username'; |
||
153 | } |
||
154 | } else { |
||
155 | // 未入力の場合はエラーにする |
||
156 | $retval[ 'entered' ][ 'username' ] = ''; |
||
157 | $retval[ 'error' ][ 'msg' ][ ] = 'empty_username'; |
||
158 | $retval[ 'error' ][ 'form_crit' ][ ] = 'username'; |
||
159 | } |
||
160 | |||
161 | //--- パスワードのチェック |
||
162 | $retval[ 'entered' ][ 'password' ] = htmlentities( $target[ 'password' ], ENT_QUOTES, 'UTF-8' ); |
||
163 | $retval[ 'entered' ][ 'password_confirm' ] = $retval[ 'entered' ][ 'password' ]; |
||
164 | View Code Duplication | if (isset( $target[ 'password' ] ) and !empty( $target[ 'password' ] )) { |
|
165 | // フォーマットチェック |
||
166 | if ($target[ 'password' ] != $target[ 'password_confirm' ]) { |
||
167 | // フォーマットにそぐわない場合はエラーにする |
||
168 | $retval[ 'error' ][ 'msg' ][ ] = 'invalid_password'; |
||
169 | $retval[ 'error' ][ 'form_crit' ][ ] = 'password'; |
||
170 | } |
||
171 | } else { |
||
172 | // 未入力の場合はエラーにする |
||
173 | $retval[ 'entered' ][ 'password' ] = ''; |
||
174 | $retval[ 'entered' ][ 'password_confirm' ] = ''; |
||
175 | $retval[ 'error' ][ 'msg' ][ ] = 'empty_password'; |
||
176 | $retval[ 'error' ][ 'form_crit' ][ ] = 'password'; |
||
177 | } |
||
178 | |||
179 | //--- 所属グループのチェック |
||
180 | $retval[ 'entered' ][ 'groupno' ] = htmlentities( $target[ 'groupno' ], ENT_QUOTES, 'UTF-8' ); |
||
181 | View Code Duplication | if (isset( $target[ 'groupno' ] )) { |
|
182 | // フォーマットチェック |
||
183 | if (!preg_match( '/\d{1,}/', $target[ 'groupno' ] )) { |
||
184 | // フォーマットにそぐわない場合はエラーにする |
||
185 | $retval[ 'error' ][ 'msg' ][ ] = 'invalid_groupno'; |
||
186 | $retval[ 'error' ][ 'form_crit' ][ ] = 'groupno'; |
||
187 | } |
||
188 | } else { |
||
189 | // 未入力の場合はエラーにする |
||
190 | $retval[ 'entered' ][ 'groupno' ] = ''; |
||
191 | $retval[ 'error' ][ 'msg' ][ ] = 'empty_groupno'; |
||
192 | $retval[ 'error' ][ 'form_crit' ][ ] = 'groupno'; |
||
193 | } |
||
194 | |||
195 | //--- ステータスのチェック |
||
196 | $retval[ 'entered' ][ 'status' ] = htmlentities( $target[ 'status' ], ENT_QUOTES, 'UTF-8' ); |
||
197 | View Code Duplication | if (isset( $target[ 'status' ] )) { |
|
198 | // フォーマットチェック |
||
199 | if (!preg_match( '/\d{1,}/', $target[ 'status' ] )) { |
||
200 | // フォーマットにそぐわない場合はエラーにする |
||
201 | $retval[ 'error' ][ 'msg' ][ ] = 'invalid_status'; |
||
202 | $retval[ 'error' ][ 'form_crit' ][ ] = 'status'; |
||
203 | } |
||
204 | } else { |
||
205 | // 未入力の場合はエラーにする |
||
206 | $retval[ 'entered' ][ 'status' ] = ''; |
||
207 | $retval[ 'error' ][ 'msg' ][ ] = 'empty_status'; |
||
208 | $retval[ 'error' ][ 'form_crit' ][ ] = 'status'; |
||
209 | } |
||
210 | |||
211 | //--- CSRFトークンのチェック |
||
212 | if ($target[ 'csrf_token' ] != $csrf_token) { |
||
213 | throw new \Exception( 'CSRF Check Error' ); |
||
214 | } |
||
215 | |||
216 | // エラー関係の配列から重複を排除する |
||
217 | $retval[ 'error' ][ 'msg' ] = array_unique( $retval[ 'error' ][ 'msg' ] ); |
||
218 | $retval[ 'error' ][ 'form_crit' ] = array_unique( $retval[ 'error' ][ 'form_crit' ] ); |
||
219 | |||
220 | // 処理結果を返却する |
||
221 | return $retval; |
||
222 | } |
||
223 | |||
391 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.