Conditions | 13 |
Paths | 98 |
Total Lines | 85 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 182 |
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 |
||
40 | public static function getMyProxyCredential( |
||
41 | $username, |
||
42 | $passphrase = '', |
||
43 | $server = MYPROXY_HOST, |
||
|
|||
44 | $port = MYPROXY_PORT, |
||
45 | $lifetime = MYPROXY_LIFETIME, |
||
46 | $usercert = '', |
||
47 | $userkey = '', |
||
48 | $certreq = '', |
||
49 | $env = '' |
||
50 | ) { |
||
51 | $retstr = ''; |
||
52 | |||
53 | // Verify the myproxy-logon binary has been configured |
||
54 | if ((!defined('MYPROXY_LOGON')) || (empty(MYPROXY_LOGON))) { |
||
55 | Util::sendErrorAlert( |
||
56 | 'getMyProxyCredential Error', |
||
57 | 'MyProxy Error = myproxy-logon binary not configured' |
||
58 | ); |
||
59 | return $retstr; |
||
60 | } |
||
61 | |||
62 | // Make sure the username passed in is not empty |
||
63 | if (strlen($username) == 0) { |
||
64 | Util::sendErrorAlert( |
||
65 | 'getMyProxyCredential Error', |
||
66 | 'MyProxy Error = Missing MyProxy username' |
||
67 | ); |
||
68 | return $retstr; |
||
69 | } |
||
70 | |||
71 | // Don't allow weird port numbers, i.e. negative or over 65535 |
||
72 | if (($port < 0) || ($port > 65535)) { |
||
73 | $port = MYPROXY_PORT; |
||
74 | } |
||
75 | |||
76 | // Don't allow weird lifetimes, i.e. negative or over 5 years |
||
77 | if (($lifetime < 0) || ($lifetime > 43800)) { |
||
78 | $lifetime = MYPROXY_LIFETIME; |
||
79 | } |
||
80 | |||
81 | // If the usercert (X509_USER_CERT) is specified, check to see if |
||
82 | // the userkey (X509_USER_KEY) was as well. If not, set userkey to |
||
83 | // usercert, in effect making usercert act like X509_USER_PROXY. Then, |
||
84 | // set the USER_CERT_ENV variable to bundle the two parameters into a |
||
85 | // single variable holding the two X509_USER_* environment variables. |
||
86 | $USER_CERT_ENV = ''; |
||
87 | if (strlen($usercert) > 0) { |
||
88 | if (strlen($userkey) == 0) { |
||
89 | $userkey = $usercert; |
||
90 | } |
||
91 | $USER_CERT_ENV = 'X509_USER_CERT=' . escapeshellarg($usercert) . |
||
92 | ' ' . |
||
93 | 'X509_USER_KEY=' . escapeshellarg($userkey); |
||
94 | } |
||
95 | |||
96 | // Run the myproxy-logon command and capture the output and any error |
||
97 | $output = array(); |
||
98 | $cmd = '/bin/env ' . |
||
99 | $USER_CERT_ENV . ' ' . |
||
100 | $env . ' ' . |
||
101 | 'MYPROXY_SOCKET_TIMEOUT=1 ' . |
||
102 | MYPROXY_LOGON . ' ' . |
||
103 | ' -s ' . escapeshellarg($server) . |
||
104 | " -p $port" . |
||
105 | " -t $lifetime" . |
||
106 | ' -l ' . escapeshellarg($username) . |
||
107 | ' -S -o -' . |
||
108 | ((strlen($certreq) > 0) ? |
||
109 | (' --certreq - <<< ' . escapeshellarg($certreq)) : '') . |
||
110 | ((strlen($passphrase) > 0) ? |
||
111 | (' <<< ' . escapeshellarg($passphrase)) : ' -n') . |
||
112 | ' 2>&1'; |
||
113 | exec($cmd, $output, $return_val); |
||
114 | $retstr = implode("\n", $output); |
||
115 | |||
116 | if ($return_val > 0) { |
||
117 | Util::sendErrorAlert( |
||
118 | 'getMyProxyCredential Error', |
||
119 | "MyProxy Error = $return_val\nMyProxy Output= $retstr" |
||
120 | ); |
||
121 | $retstr = ''; |
||
122 | } |
||
123 | |||
124 | return $retstr; |
||
125 | } |
||
141 |