Issues (321)

demo/server/methodProviders/wrapper.php (13 issues)

1
<?php
2
/**
3
 * Defines functions and signatures which can be registered as methods exposed by an XML-RPC Server
4
 *
5
 * To use this, use something akin to:
6
 * $signatures = include('wrapper.php');
7
 * NB: requires 'functions.php' to be included first
8
 *
9
 * Wrap methods of xml-rpc-unaware php classes and xml-rpc-unaware php functions so that they can be used transparently.
10
 */
11
12
use PhpXmlRpc\Response;
13
use PhpXmlRpc\Value;
14
15
// *** functions ***
16
17
/**
18
 * Inner code of the state-number server.
19
 * Used to test wrapping of PHP functions into xml-rpc methods.
20
 *
21
 * @param integer $stateNo the state number
22
 *
23
 * @return string the name of the state (or error description)
24
 *
25
 * @throws Exception if state is not found
26
 */
27
function plain_findstate($stateNo)
28 169
{
29
     if (isset(exampleMethods::$stateNames[$stateNo - 1])) {
30 169
        return exampleMethods::$stateNames[$stateNo - 1];
31 169
    } else {
32
        // not, there so complain
33
        throw new Exception("I don't have a state for the index '" . $stateNo . "'", PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser);
34 43
    }
35
}
36
37
$wrapper = new PhpXmlRpc\Wrapper();
38 559
39
$findstate2_sig = $wrapper->wrapPhpFunction('plain_findstate');
0 ignored issues
show
'plain_findstate' of type string 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

39
$findstate2_sig = $wrapper->wrapPhpFunction(/** @scrutinizer ignore-type */ 'plain_findstate');
Loading history...
40 559
41
42
// *** objects/classes ***
43
44
/**
45
 * Used to test usage of object methods in dispatch maps and in wrapper code.
46
 */
47
class handlersContainer
48
{
49
    /**
50
     * Method used to test logging of php warnings generated by user functions.
51
     * @param PhpXmlRpc\Request $req
52
     * @return Response
53
     */
54
    public function phpWarningGenerator($req)
0 ignored issues
show
The parameter $req is not used and could be removed. ( Ignorable by Annotation )

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

54
    public function phpWarningGenerator(/** @scrutinizer ignore-unused */ $req)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55 22
    {
56
        /** @noinspection PhpUndefinedVariableInspection */
57 22
        $a = $undefinedVariable; // this triggers a warning in E_ALL mode, since $undefinedVariable is undefined
0 ignored issues
show
The assignment to $a is dead and can be removed.
Loading history...
Comprehensibility Best Practice introduced by
The variable $undefinedVariable seems to be never defined.
Loading history...
58 22
        return new Response(new Value(1, Value::$xmlrpcBoolean));
59
    }
60
61
    /**
62
     * Method used to test catching of exceptions in the server.
63
     * @param PhpXmlRpc\Request $req
64
     * @throws Exception
65
     */
66 2
    public function exceptionGenerator($req)
67
    {
68 2
        $errNum = 1;
69
        if ($req->getNumParams()) {
70
            $p1 = $req->getParam(0);
71
            if ($p1->kindOf() === 'scalar') {
72
                $errNum = (int)$p1->scalarVal();
73
            }
74 2
        }
75
        throw new Exception("it's just a test", $errNum);
76 2
    }
77 2
78
    /**
79
     * Method used to test catching of errors in the server.
80
     * @param PhpXmlRpc\Request $req
81
     * @throws Exception
82
     */
83
    public function errorGenerator($req)
0 ignored issues
show
The parameter $req is not used and could be removed. ( Ignorable by Annotation )

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

83
    public function errorGenerator(/** @scrutinizer ignore-unused */ $req)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
    {
85
        throw new Error("it's just a test", 1);
86
    }
87 85
88
    /**
89
     * @param string $msg
90 85
     */
91
    public function debugMessageGenerator($msg)
92
    {
93
        PhpXmlRpc\Server::xmlrpc_debugmsg($msg);
94
    }
95
96
    /**
97 1
     * A PHP version of the state-number server. Send me an integer and i'll sell you a state.
98
     * Used to test wrapping of PHP methods into xml-rpc methods.
99 1
     *
100 1
     * @param integer $num
101 1
     * @return string
102
     * @throws Exception
103
     */
104
    public static function findState($num)
105 559
    {
106
        // we are lazy ;-)
107 559
        return plain_findstate($num);
108 559
    }
109
110 559
    /**
111 559
     * Returns an instance of stdClass.
112
     * Used to test wrapping of PHP objects with class preservation
113 559
     */
114 559
    public function returnObject()
115
    {
116 559
        $obj = new stdClass();
117 559
        $obj->hello = 'world';
118
        return $obj;
119
    }
120 559
}
121 559
122
$findstate3_sig = $wrapper->wrapPhpFunction(array('handlersContainer', 'findState'));
0 ignored issues
show
array('handlersContainer', 'findState') of type array<integer,string> 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

122
$findstate3_sig = $wrapper->wrapPhpFunction(/** @scrutinizer ignore-type */ array('handlersContainer', 'findState'));
Loading history...
123 559
124 559
$instance = new handlersContainer();
125
$findstate4_sig = $wrapper->wrapPhpFunction(array($instance, 'findstate'));
0 ignored issues
show
array($instance, 'findstate') of type array<integer,handlersContainer|string> 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

125
$findstate4_sig = $wrapper->wrapPhpFunction(/** @scrutinizer ignore-type */ array($instance, 'findstate'));
Loading history...
126
127
$findstate5_sig = $wrapper->wrapPhpFunction('handlersContainer::findState', '', array('return_source' => true));
128
eval($findstate5_sig['source']);
0 ignored issues
show
The use of eval() is discouraged.
Loading history...
129 559
130
$findstate6_sig = $wrapper->wrapPhpFunction('plain_findstate', '', array('return_source' => true));
131
eval($findstate6_sig['source']);
0 ignored issues
show
The use of eval() is discouraged.
Loading history...
132
133
$findstate7_sig = $wrapper->wrapPhpFunction(array('handlersContainer', 'findState'), '', array('return_source' => true));
134
eval($findstate7_sig['source']);
0 ignored issues
show
The use of eval() is discouraged.
Loading history...
135
136
$findstate8_sig = $wrapper->wrapPhpFunction(array($instance, 'findstate'), '', array('return_source' => true));
137 559
eval($findstate8_sig['source']);
0 ignored issues
show
The use of eval() is discouraged.
Loading history...
138
139 559
$findstate9_sig = $wrapper->wrapPhpFunction('handlersContainer::findState', '', array('return_source' => true));
140
eval($findstate9_sig['source']);
0 ignored issues
show
The use of eval() is discouraged.
Loading history...
141 559
142
$findstate10_sig = array(
143 559
    /// @todo add a demo and test with closure usage
144
    "function" => function ($req) { return exampleMethods::findState($req); },
145 559
    "signature" => array(array(Value::$xmlrpcString, Value::$xmlrpcInt)),
146
    "docstring" => 'When passed an integer between 1 and 51 returns the name of a US state, where the integer is the ' .
147 559
        'index of that state name in an alphabetic order.',
148 559
);
149 559
150 559
$findstate11_sig = $wrapper->wrapPhpFunction(function ($stateNo) { return plain_findstate($stateNo); });
0 ignored issues
show
function(...) { /* ... */ } 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

150
$findstate11_sig = $wrapper->wrapPhpFunction(/** @scrutinizer ignore-type */ function ($stateNo) { return plain_findstate($stateNo); });
Loading history...
151 559
152 559
/// @todo do we really need a new instance ?
153 559
$c = new handlersContainer();
154 559
155 559
$moreSignatures = $wrapper->wrapPhpClass($c, array('prefix' => 'tests.', 'method_type' => 'all'));
156 559
157 559
$namespaceSignatures = $wrapper->wrapPhpClass($c, array('prefix' => 'namespacetest.', 'replace_class_name' => true, 'method_filter' => '/^findState$/', 'method_type' => 'static'));
158
159 559
$returnObj_sig =  $wrapper->wrapPhpFunction(array($c, 'returnObject'), '', array('encode_php_objs' => true));
160 559
161
return array_merge(
162
    array(
163
        'tests.getStateName.2' => $findstate2_sig,
164
165
        'tests.getStateName.3' => $findstate3_sig,
166
        'tests.getStateName.4' => $findstate4_sig,
167
        'tests.getStateName.5' => $findstate5_sig,
168
        'tests.getStateName.6' => $findstate6_sig,
169
        'tests.getStateName.7' => $findstate7_sig,
170
        'tests.getStateName.8' => $findstate8_sig,
171
        'tests.getStateName.9' => $findstate9_sig,
172
        'tests.getStateName.10' => $findstate10_sig,
173
        'tests.getStateName.11' => $findstate11_sig,
174
    ),
175
    $moreSignatures,
176
    $namespaceSignatures,
177
    array(
178
        'tests.returnPhpObject' => $returnObj_sig,
179
        // signature omitted on purpose
180
        "tests.generatePHPWarning" => array(
181
            "function" => array($instance, "phpWarningGenerator"),
182
        ),
183
        // signature omitted on purpose
184
        "tests.raiseException" => array(
185
            "function" => array($instance, "exceptionGenerator"),
186
        ),
187
        "tests.raiseError" => array(
188
            "function" => array($instance, "errorGenerator"),
189
        ),
190
    )
191
);
192