Conditions | 20 |
Paths | 192 |
Total Lines | 74 |
Lines | 27 |
Ratio | 36.49 % |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
143 | function build_remote_method_wrapper_code($client, $methodName, $xmlrpcFuncName, |
||
144 | $mSig, $mDesc = '', $timeout = 0, $protocol = '', $clientCopyMode = 0, $prefix = 'xmlrpc', |
||
145 | $decodePhpObjects = false, $encodePhpObjects = false, $decodeFault = false, |
||
146 | $faultResponse = '', $namespace = '\\PhpXmlRpc\\') |
||
147 | { |
||
148 | $code = "function $xmlrpcFuncName ("; |
||
149 | if ($clientCopyMode < 2) { |
||
150 | // client copy mode 0 or 1 == partial / full client copy in emitted code |
||
151 | $innerCode = build_client_wrapper_code($client, $clientCopyMode, $prefix, $namespace); |
||
152 | $innerCode .= "\$client->setDebug(\$debug);\n"; |
||
153 | $this_ = ''; |
||
154 | } else { |
||
155 | // client copy mode 2 == no client copy in emitted code |
||
156 | $innerCode = ''; |
||
157 | $this_ = 'this->'; |
||
158 | } |
||
159 | $innerCode .= "\$req = new {$namespace}Request('$methodName');\n"; |
||
160 | |||
161 | if ($mDesc != '') { |
||
162 | // take care that PHP comment is not terminated unwillingly by method description |
||
163 | $mDesc = "/**\n* " . str_replace('*/', '* /', $mDesc) . "\n"; |
||
164 | } else { |
||
165 | $mDesc = "/**\nFunction $xmlrpcFuncName\n"; |
||
166 | } |
||
167 | |||
168 | // param parsing |
||
169 | $innerCode .= "\$encoder = new {$namespace}Encoder();\n"; |
||
170 | $plist = array(); |
||
171 | $pCount = count($mSig); |
||
172 | for ($i = 1; $i < $pCount; $i++) { |
||
173 | $plist[] = "\$p$i"; |
||
174 | $pType = $mSig[$i]; |
||
175 | if ($pType == 'i4' || $pType == 'i8' || $pType == 'int' || $pType == 'boolean' || $pType == 'double' || |
||
176 | $pType == 'string' || $pType == 'dateTime.iso8601' || $pType == 'base64' || $pType == 'null' |
||
177 | ) { |
||
178 | // only build directly xmlrpc values when type is known and scalar |
||
179 | $innerCode .= "\$p$i = new {$namespace}Value(\$p$i, '$pType');\n"; |
||
180 | } else { |
||
181 | if ($encodePhpObjects) { |
||
182 | $innerCode .= "\$p$i = \$encoder->encode(\$p$i, array('encode_php_objs'));\n"; |
||
183 | } else { |
||
184 | $innerCode .= "\$p$i = \$encoder->encode(\$p$i);\n"; |
||
185 | } |
||
186 | } |
||
187 | $innerCode .= "\$req->addparam(\$p$i);\n"; |
||
188 | $mDesc .= '* @param ' . xmlrpc_2_php_type($pType) . " \$p$i\n"; |
||
189 | } |
||
190 | if ($clientCopyMode < 2) { |
||
191 | $plist[] = '$debug=0'; |
||
192 | $mDesc .= "* @param int \$debug when 1 (or 2) will enable debugging of the underlying {$prefix} call (defaults to 0)\n"; |
||
193 | } |
||
194 | $plist = implode(', ', $plist); |
||
195 | $mDesc .= '* @return ' . xmlrpc_2_php_type($mSig[0]) . " (or an {$namespace}Response obj instance if call fails)\n*/\n"; |
||
196 | |||
197 | $innerCode .= "\$res = \${$this_}client->send(\$req, $timeout, '$protocol');\n"; |
||
198 | if ($decodeFault) { |
||
199 | if (is_string($faultResponse) && ((strpos($faultResponse, '%faultCode%') !== false) || (strpos($faultResponse, '%faultString%') !== false))) { |
||
200 | $respCode = "str_replace(array('%faultCode%', '%faultString%'), array(\$res->faultCode(), \$res->faultString()), '" . str_replace("'", "''", $faultResponse) . "')"; |
||
201 | } else { |
||
202 | $respCode = var_export($faultResponse, true); |
||
203 | } |
||
204 | } else { |
||
205 | $respCode = '$res'; |
||
206 | } |
||
207 | if ($decodePhpObjects) { |
||
208 | $innerCode .= "if (\$res->faultcode()) return $respCode; else return \$encoder->decode(\$res->value(), array('decode_php_objs'));"; |
||
209 | } else { |
||
210 | $innerCode .= "if (\$res->faultcode()) return $respCode; else return \$encoder->decode(\$res->value());"; |
||
211 | } |
||
212 | |||
213 | $code = $code . $plist . ") {\n" . $innerCode . "\n}\n"; |
||
214 | |||
215 | return array('source' => $code, 'docstring' => $mDesc); |
||
216 | } |
||
217 | |||
244 |