Conditions | 10 |
Paths | 4 |
Total Lines | 51 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 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 |
||
24 | function forward_request($req) |
||
25 | { |
||
26 | $encoder = new \PhpXmlRpc\Encoder(); |
||
27 | |||
28 | // create client |
||
29 | $timeout = 0; |
||
30 | $url = $encoder->decode($req->getParam(0)); |
||
31 | $client = new PhpXmlRpc\Client($url); |
||
32 | |||
33 | if ($req->getNumParams() > 3) { |
||
34 | // we have to set some options onto the client. |
||
35 | // Note that if we do not untaint the received values, warnings might be generated... |
||
36 | $options = $encoder->decode($req->getParam(3)); |
||
37 | foreach ($options as $key => $val) { |
||
38 | switch ($key) { |
||
39 | case 'Cookie': |
||
40 | break; |
||
41 | case 'Credentials': |
||
42 | break; |
||
43 | case 'RequestCompression': |
||
44 | $client->setRequestCompression($val); |
||
45 | break; |
||
46 | case 'SSLVerifyHost': |
||
47 | $client->setSSLVerifyHost($val); |
||
48 | break; |
||
49 | case 'SSLVerifyPeer': |
||
50 | $client->setSSLVerifyPeer($val); |
||
51 | break; |
||
52 | case 'Timeout': |
||
53 | $timeout = (integer)$val; |
||
54 | break; |
||
55 | } // switch |
||
56 | } |
||
57 | } |
||
58 | |||
59 | // build call for remote server |
||
60 | /// @todo find a way to forward client info (such as IP) to server, either |
||
61 | /// - as xml comments in the payload, or |
||
62 | /// - using std http header conventions, such as X-forwarded-for... |
||
63 | $reqMethod = $encoder->decode($req->getParam(1)); |
||
64 | $pars = $req->getParam(2); |
||
65 | $req = new PhpXmlRpc\Request($reqMethod); |
||
66 | foreach ($pars as $par) { |
||
67 | $req->addParam($par); |
||
68 | } |
||
69 | |||
70 | // add debug info into response we give back to caller |
||
71 | PhpXmlRpc\Server::xmlrpc_debugmsg("Sending to server $url the payload: " . $req->serialize()); |
||
72 | |||
73 | return $client->send($req, $timeout); |
||
74 | } |
||
75 | |||
89 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.