Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Auth often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Auth, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Auth |
||
22 | { |
||
23 | //------------------------------------------------------// |
||
24 | // クラスメソッド定義 |
||
25 | //------------------------------------------------------// |
||
26 | /** |
||
27 | * __construct() |
||
28 | * |
||
29 | * コンストラクタ |
||
30 | */ |
||
31 | private function __construct() |
||
34 | |||
35 | /** |
||
36 | * getEncPass($target) |
||
37 | * |
||
38 | * 引数で与えられた文字列をパスワード用にハッシュ化する |
||
39 | * |
||
40 | * @access private |
||
41 | * |
||
42 | * @param string $target パスワード文字列 |
||
43 | * |
||
44 | * @return string ハッシュ化したパスワード文字列 |
||
45 | */ |
||
46 | private static function getEncPass( $target ) |
||
50 | |||
51 | /** |
||
52 | * getProvider() |
||
53 | * |
||
54 | * 認証プロバイダの情報を取得する |
||
55 | * |
||
56 | * @access private |
||
57 | * |
||
58 | * @param void |
||
59 | * |
||
60 | * @return object 認証プロバイダのインスタンス |
||
61 | */ |
||
62 | private static function getProvider() |
||
75 | |||
76 | /** |
||
77 | * getBasicAuthDialog($realm, $message) |
||
78 | * |
||
79 | * BASIC認証のダイアログ表示用ヘッダを出力する |
||
80 | * |
||
81 | * @access private |
||
82 | * |
||
83 | * @param string $realm レルム |
||
84 | * @param string $message ブラウザ側に出力するメッセージ |
||
85 | * |
||
86 | * @return void なし |
||
87 | */ |
||
88 | private static function getBasicAuthDialog( $realm, $message ) |
||
96 | |||
97 | /** |
||
98 | * getBasicAuthDialog($realm, $message) |
||
99 | * |
||
100 | * BASIC認証を行う |
||
101 | * |
||
102 | * @access public |
||
103 | * |
||
104 | * @param string $realm レルム(省略可、デフォルトは'Authorization Required') |
||
105 | * @param string $message ブラウザ側に出力するメッセージ(省略可、デフォルトは'Authorization Required') |
||
106 | * |
||
107 | * @return mixed ユーザ情報:認証成功時/false: 認証失敗時 |
||
108 | */ |
||
109 | public static function basicAuth( $realm = 'Authorization Required', $message = 'Authorization Required' ) |
||
139 | |||
140 | /** |
||
141 | * callProviderMethod() |
||
142 | * |
||
143 | * 認証プロバイダのメソッドをコールする |
||
144 | * |
||
145 | * @access public |
||
146 | * |
||
147 | * @param string $operation オペレーション識別子(init/addUser/addGroup/modUser/modGroup/delUser/delGroup/showUser/showGroup/showUserAll/showGroupAll) |
||
148 | * @param array $option オプション情報(省略可) |
||
149 | * |
||
150 | * @return mixed trueまたは取得内容:成功/false:失敗 |
||
151 | */ |
||
152 | public static function callProviderMethod( $operation, array $option = [ ] ) |
||
281 | } |
||
282 |
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.