Conditions | 23 |
Paths | 454 |
Total Lines | 122 |
Code Lines | 72 |
Lines | 6 |
Ratio | 4.92 % |
Changes | 2 | ||
Bugs | 2 | Features | 1 |
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 |
||
36 | public function perform() |
||
37 | { |
||
38 | //------------------------------------------------------// |
||
39 | // アプリケーションクラスをロードし実行する |
||
40 | //------------------------------------------------------// |
||
41 | // クラスインスタンスを生成し、実行する |
||
42 | try { |
||
43 | // 呼び出すクラスを決定する |
||
44 | $call = $this->findCallClass(); |
||
45 | |||
46 | // インスタンスの生成 |
||
47 | $targetInstance = new $call[ 'load' ]; |
||
48 | |||
49 | // イニシャライズメソッドをコール |
||
50 | View Code Duplication | if (method_exists( $targetInstance, 'init' )) { |
|
|
|||
51 | $targetInstance->init( $call[ 'param' ] ); |
||
52 | } else { |
||
53 | // メソッドが存在しなければ例外をThrow |
||
54 | throw new \Exception( $this->coreError( 'error', 'notfound', 'Init()' ) ); |
||
55 | } |
||
56 | |||
57 | // HTTPのメソッドに応じて適切なコントローラをコール |
||
58 | switch ($_SERVER[ 'REQUEST_METHOD' ]) { |
||
59 | // GETの場合 |
||
60 | case 'GET': |
||
61 | if (method_exists( $targetInstance, 'playGet' )) { |
||
62 | $targetInstance->playGet(); |
||
63 | } else { |
||
64 | $this->playFuncCall( $targetInstance ); |
||
65 | } |
||
66 | break; |
||
67 | |||
68 | // POSTの場合 |
||
69 | case 'POST': |
||
70 | if (method_exists( $targetInstance, 'playPost' )) { |
||
71 | $targetInstance->playPost(); |
||
72 | } else { |
||
73 | $this->playFuncCall( $targetInstance ); |
||
74 | } |
||
75 | break; |
||
76 | |||
77 | // PUTの場合 |
||
78 | case 'PUT': |
||
79 | if (method_exists( $targetInstance, 'playPut' )) { |
||
80 | $targetInstance->playPut(); |
||
81 | } else { |
||
82 | $this->playFuncCall( $targetInstance ); |
||
83 | } |
||
84 | break; |
||
85 | |||
86 | // DELETEの場合 |
||
87 | case 'DELETE': |
||
88 | if (method_exists( $targetInstance, 'playDelete' )) { |
||
89 | $targetInstance->playDelete(); |
||
90 | } else { |
||
91 | $this->playFuncCall( $targetInstance ); |
||
92 | } |
||
93 | break; |
||
94 | |||
95 | // OPTIONの場合 |
||
96 | case 'OPTION': |
||
97 | if (method_exists( $targetInstance, 'playOption' )) { |
||
98 | $targetInstance->playOption(); |
||
99 | } else { |
||
100 | $this->playFuncCall( $targetInstance ); |
||
101 | } |
||
102 | break; |
||
103 | |||
104 | // HEADの場合 |
||
105 | case 'HEAD': |
||
106 | if (method_exists( $targetInstance, 'playHead' )) { |
||
107 | $targetInstance->playHead(); |
||
108 | } else { |
||
109 | $this->playFuncCall( $targetInstance ); |
||
110 | } |
||
111 | break; |
||
112 | |||
113 | // TRACEの場合 |
||
114 | case 'TRACE': |
||
115 | if (method_exists( $targetInstance, 'playTrace' )) { |
||
116 | $targetInstance->playTrace(); |
||
117 | } else { |
||
118 | $this->playFuncCall( $targetInstance ); |
||
119 | } |
||
120 | break; |
||
121 | |||
122 | // CONNECTの場合 |
||
123 | case 'CONNECT': |
||
124 | if (method_exists( $targetInstance, 'playConnect' )) { |
||
125 | $targetInstance->playConnect(); |
||
126 | } else { |
||
127 | $this->playFuncCall( $targetInstance ); |
||
128 | } |
||
129 | break; |
||
130 | |||
131 | // デフォルトの場合 |
||
132 | default: |
||
133 | $this->playFuncCall( $targetInstance ); |
||
134 | break; |
||
135 | } |
||
136 | } catch ( \Exception $e ) { |
||
137 | // エラーハンドリングメソッドをコール |
||
138 | if (!empty( $targetInstance )) { |
||
139 | if (method_exists( $targetInstance, 'error' )) { |
||
140 | $targetInstance->error( $e ); |
||
141 | } else { |
||
142 | // メソッドが存在しなければ強制終了 |
||
143 | die( $this->coreError( 'error', 'notfound', 'Error()' ) ); |
||
144 | } |
||
145 | } |
||
146 | } finally { |
||
147 | // クリーニングメソッドをコール |
||
148 | if (!empty( $targetInstance )) { |
||
149 | if (method_exists( $targetInstance, 'clean' )) { |
||
150 | $targetInstance->clean(); |
||
151 | } else { |
||
152 | // メソッドが存在しなければ強制終了 |
||
153 | die( $this->coreError( 'error', 'notfound', 'Clean()' ) ); |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | |||
367 |
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.