build_remote_method_wrapper_code()   F
last analyzed

Complexity

Conditions 20
Paths 192

Size

Total Lines 73
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 48
nc 192
nop 14
dl 0
loc 73
rs 3.4
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

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:

Many Parameters

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
2
3
/******************************************************************************
4
 *
5
 * *** DEPRECATED ***
6
 *
7
 * This file is only used to insure backwards compatibility
8
 * with the API of the library <= rev. 3
9
 *****************************************************************************/
10
11
include_once(__DIR__.'/../src/Wrapper.php');
12
13
/* Expose as global functions the ones which are now class methods */
14
15
/**
16
 * @see PhpXmlRpc\Wrapper::php_2_xmlrpc_type
17
 * @param string $phpType
18
 * @return string
19
 */
20
function php_2_xmlrpc_type($phpType)
21
{
22
    $wrapper = new PhpXmlRpc\Wrapper();
23
    return $wrapper->php2XmlrpcType($phpType);
24
}
25
26
/**
27
 * @see PhpXmlRpc\Wrapper::xmlrpc_2_php_type
28
 * @param string $xmlrpcType
29
 * @return string
30
 */
31
function xmlrpc_2_php_type($xmlrpcType)
32
{
33
    $wrapper = new PhpXmlRpc\Wrapper();
34
    return $wrapper->xmlrpc2PhpType($xmlrpcType);
35
}
36
37
/**
38
 * @see PhpXmlRpc\Wrapper::wrap_php_function
39
 * @param callable $funcName
40
 * @param string $newFuncName
41
 * @param array $extraOptions
42
 * @return array|false
43
 */
44
function wrap_php_function($funcName, $newFuncName='', $extraOptions=array())
45
{
46
    $wrapper = new PhpXmlRpc\Wrapper();
47
    if (!isset($extraOptions['return_source']) || $extraOptions['return_source'] == false) {
48
        // backwards compat: return string instead of callable
49
        $extraOptions['return_source'] = true;
50
        $wrapped = $wrapper->wrapPhpFunction($funcName, $newFuncName, $extraOptions);
0 ignored issues
show
Bug introduced by
$funcName of type callable is incompatible with the type Callable expected by parameter $callable of PhpXmlRpc\Wrapper::wrapPhpFunction(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        $wrapped = $wrapper->wrapPhpFunction(/** @scrutinizer ignore-type */ $funcName, $newFuncName, $extraOptions);
Loading history...
51
        eval($wrapped['source']);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
52
    } else {
53
        $wrapped = $wrapper->wrapPhpFunction($funcName, $newFuncName, $extraOptions);
54
    }
55
    return $wrapped;
56
}
57
58
/**
59
 * NB: this function returns an array in a format which is unsuitable for direct use in the server dispatch map, unlike
60
 * PhpXmlRpc\Wrapper::wrapPhpClass. This behaviour might seem like a bug, but has been kept for backwards compatibility.
61
 *
62
 * @see PhpXmlRpc\Wrapper::wrap_php_class
63
 * @param string|object $className
64
 * @param array $extraOptions
65
 * @return array|false
66
 */
67
function wrap_php_class($className, $extraOptions=array())
68
{
69
    $wrapper = new PhpXmlRpc\Wrapper();
70
    $fix = false;
71
    if (!isset($extraOptions['return_source']) || $extraOptions['return_source'] == false) {
72
        // backwards compat: return string instead of callable
73
        $extraOptions['return_source'] = true;
74
        $fix = true;
75
    }
76
    $wrapped = $wrapper->wrapPhpClass($className, $extraOptions);
77
    foreach($wrapped as $name => $value) {
78
        if ($fix) {
79
            eval($value['source']);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
80
        }
81
        $wrapped[$name] = $value['function'];
82
    }
83
    return $wrapped;
84
}
85
86
/**
87
 * @see PhpXmlRpc\Wrapper::wrapXmlrpcMethod
88
 * @param xmlrpc_client $client
89
 * @param string $methodName
90
 * @param int|array $extraOptions the usage of an int as signature number is deprecated, use an option 'signum' in $extraOptions
91
 * @param int $timeout            deprecated, use an option in $extraOptions
92
 * @param string $protocol        deprecated, use an option in $extraOptions
93
 * @param string $newFuncName     deprecated, use an option in $extraOptions
94
 * @return array|callable|false
95
 */
96
function wrap_xmlrpc_method($client, $methodName, $extraOptions=0, $timeout=0, $protocol='', $newFuncName='')
97
{
98
    if (!is_array($extraOptions))
99
    {
100
        $sigNum = $extraOptions;
101
        $extraOptions = array(
102
            'signum' => $sigNum,
103
            'timeout' => $timeout,
104
            'protocol' => $protocol,
105
            'new_function_name' => $newFuncName
106
        );
107
    }
108
109
    $wrapper = new PhpXmlRpc\Wrapper();
110
111
    if (!isset($extraOptions['return_source']) || $extraOptions['return_source'] == false) {
112
        // backwards compat: return string instead of callable
113
        $extraOptions['return_source'] = true;
114
        $wrapped = $wrapper->wrapXmlrpcMethod($client, $methodName, $extraOptions);
115
        if (is_array($wrapped)) {
0 ignored issues
show
introduced by
The condition is_array($wrapped) is always false.
Loading history...
116
            eval($wrapped['source']);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
117
            $wrapped = $wrapped['function'];
118
        }
119
    } else {
120
        $wrapped = $wrapper->wrapXmlrpcMethod($client, $methodName, $extraOptions);
121
    }
122
    return $wrapped;
123
}
124
125
/**
126
 * @see PhpXmlRpc\Wrapper::wrap_xmlrpc_server
127
 * @param xmlrpc_client $client
128
 * @param array $extraOptions
129
 * @return mixed
130
 */
131
function wrap_xmlrpc_server($client, $extraOptions=array())
132
{
133
    $wrapper = new PhpXmlRpc\Wrapper();
134
    return $wrapper->wrapXmlrpcServer($client, $extraOptions);
135
}
136
137
/**
138
 * Given the necessary info, build php code that creates a new function to invoke a remote xmlrpc method.
139
 * Take care that no full checking of input parameters is done to ensure that valid php code is emitted.
140
 * Only kept for backwards compatibility
141
 * Note: real spaghetti code follows...
142
 *
143
 * @deprecated
144
 */
145
function build_remote_method_wrapper_code($client, $methodName, $xmlrpcFuncName,
146
     $mSig, $mDesc = '', $timeout = 0, $protocol = '', $clientCopyMode = 0, $prefix = 'xmlrpc',
147
     $decodePhpObjects = false, $encodePhpObjects = false, $decodeFault = false,
148
     $faultResponse = '', $namespace = '\\PhpXmlRpc\\')
149
{
150
    $code = "function $xmlrpcFuncName (";
151
    if ($clientCopyMode < 2) {
152
        // client copy mode 0 or 1 == partial / full client copy in emitted code
153
        $innerCode = build_client_wrapper_code($client, $clientCopyMode, $prefix);
0 ignored issues
show
Deprecated Code introduced by
The function build_client_wrapper_code() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

153
        $innerCode = /** @scrutinizer ignore-deprecated */ build_client_wrapper_code($client, $clientCopyMode, $prefix);
Loading history...
154
        $innerCode .= "\$client->setDebug(\$debug);\n";
155
        $this_ = '';
156
    } else {
157
        // client copy mode 2 == no client copy in emitted code
158
        $innerCode = '';
159
        $this_ = 'this->';
160
    }
161
    $innerCode .= "\$req = new {$namespace}Request('$methodName');\n";
162
163
    if ($mDesc != '') {
164
        // take care that PHP comment is not terminated unwillingly by method description
165
        $mDesc = "/**\n* " . str_replace('*/', '* /', $mDesc) . "\n";
166
    } else {
167
        $mDesc = "/**\nFunction $xmlrpcFuncName\n";
168
    }
169
170
    // param parsing
171
    $innerCode .= "\$encoder = new {$namespace}Encoder();\n";
172
    $plist = array();
173
    $pCount = count($mSig);
174
    for ($i = 1; $i < $pCount; $i++) {
175
        $plist[] = "\$p$i";
176
        $pType = $mSig[$i];
177
        if ($pType == 'i4' || $pType == 'i8' || $pType == 'int' || $pType == 'boolean' || $pType == 'double' ||
178
            $pType == 'string' || $pType == 'dateTime.iso8601' || $pType == 'base64' || $pType == 'null'
179
        ) {
180
            // only build directly xmlrpc values when type is known and scalar
181
            $innerCode .= "\$p$i = new {$namespace}Value(\$p$i, '$pType');\n";
182
        } else {
183
            if ($encodePhpObjects) {
184
                $innerCode .= "\$p$i = \$encoder->encode(\$p$i, array('encode_php_objs'));\n";
185
            } else {
186
                $innerCode .= "\$p$i = \$encoder->encode(\$p$i);\n";
187
            }
188
        }
189
        $innerCode .= "\$req->addparam(\$p$i);\n";
190
        $mDesc .= '* @param ' . xmlrpc_2_php_type($pType) . " \$p$i\n";
191
    }
192
    if ($clientCopyMode < 2) {
193
        $plist[] = '$debug=0';
194
        $mDesc .= "* @param int \$debug when 1 (or 2) will enable debugging of the underlying {$prefix} call (defaults to 0)\n";
195
    }
196
    $plist = implode(', ', $plist);
197
    $mDesc .= '* @return ' . xmlrpc_2_php_type($mSig[0]) . " (or an {$namespace}Response obj instance if call fails)\n*/\n";
198
199
    $innerCode .= "\$res = \${$this_}client->send(\$req, $timeout, '$protocol');\n";
200
    if ($decodeFault) {
201
        if (is_string($faultResponse) && ((strpos($faultResponse, '%faultCode%') !== false) || (strpos($faultResponse, '%faultString%') !== false))) {
202
            $respCode = "str_replace(array('%faultCode%', '%faultString%'), array(\$res->faultCode(), \$res->faultString()), '" . str_replace("'", "''", $faultResponse) . "')";
203
        } else {
204
            $respCode = var_export($faultResponse, true);
205
        }
206
    } else {
207
        $respCode = '$res';
208
    }
209
    if ($decodePhpObjects) {
210
        $innerCode .= "if (\$res->faultCode()) return $respCode; else return \$encoder->decode(\$res->value(), array('decode_php_objs'));";
211
    } else {
212
        $innerCode .= "if (\$res->faultCode()) return $respCode; else return \$encoder->decode(\$res->value());";
213
    }
214
215
    $code = $code . $plist . ") {\n" . $innerCode . "\n}\n";
216
217
    return array('source' => $code, 'docstring' => $mDesc);
218
}
219
220
/**
221
 * @deprecated
222
 */
223
function build_client_wrapper_code($client, $verbatim_client_copy, $prefix='xmlrpc')
224
{
225
    $code = "\$client = new {$prefix}_client('".str_replace("'", "\'", $client->path).
226
        "', '" . str_replace("'", "\'", $client->server) . "', $client->port);\n";
227
228
    // copy all client fields to the client that will be generated runtime
229
    // (this provides for future expansion or subclassing of client obj)
230
    if ($verbatim_client_copy)
231
    {
232
        foreach ($client as $fld => $val)
233
        {
234
            if ($fld != 'debug' && $fld != 'return_type')
235
            {
236
                $val = var_export($val, true);
237
                $code .= "\$client->$fld = $val;\n";
238
            }
239
        }
240
    }
241
    // only make sure that client always returns the correct data type
242
    $code .= "\$client->return_type = '{$prefix}vals';\n";
243
    //$code .= "\$client->setDebug(\$debug);\n";
244
    return $code;
245
}
246