Passed
Push — master ( 1a96d2...6722ee )
by Gaetano
04:55
created

plain_findstate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 * Defines functions and signatures which can be registered as methods exposed by an XMLRPC Server
4
 *
5
 * To use this, use something akin to:
6
 * $signatures = include('wrapper.php');
7
 *
8
 * Wrap methods of xmlrpc-unaware php classes and xmlrpc-unaware php functions so that they can be used transparently.
9
 */
10
11
use PhpXmlRpc\Response;
12
use PhpXmlRpc\Value;
13
14
// *** functions ***
15
16
/**
17
 * Inner code of the state-number server.
18
 * Used to test wrapping of PHP functions into xmlrpc methods.
19
 *
20
 * @param integer $stateNo the state number
21
 *
22
 * @return string the name of the state (or error description)
23
 *
24
 * @throws Exception if state is not found
25
 */
26
function plain_findstate($stateNo)
27
{
28 134
    global $stateNames;
29
30 134
    if (isset($stateNames[$stateNo - 1])) {
31 134
        return $stateNames[$stateNo - 1];
32
    } else {
33
        // not, there so complain
34 39
        throw new Exception("I don't have a state for the index '" . $stateNo . "'", PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser);
35
    }
36
}
37
38 488
$wrapper = new PhpXmlRpc\Wrapper();
39
40 488
$findstate2_sig = $wrapper->wrapPhpFunction('plain_findstate');
41
42
43
// *** objects/classes ***
44
45
/**
46
 * Used to test usage of object methods in dispatch maps and in wrapper code.
47
 */
48
class xmlrpcServerMethodsContainer
49
{
50
    /**
51
     * Method used to test logging of php warnings generated by user functions.
52
     * @param PhpXmlRpc\Request $req
53
     * @return Response
54
     */
55 20
    public function phpWarningGenerator($req)
0 ignored issues
show
Unused Code introduced by
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

55
    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...
56
    {
57 20
        $a = $undefinedVariable; // this triggers a warning in E_ALL mode, since $undefinedVariable is undefined
0 ignored issues
show
Unused Code introduced by
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 20
        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)
0 ignored issues
show
Unused Code introduced by
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

66
    public function exceptionGenerator(/** @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...
67
    {
68 2
        throw new Exception("it's just a test", 1);
69
    }
70
71
    /**
72
     * @param string $msg
73
     */
74 2
    public function debugMessageGenerator($msg)
75
    {
76 2
        PhpXmlRpc\Server::xmlrpc_debugmsg($msg);
77 2
    }
78
79
    /**
80
     * A PHP version of the state-number server. Send me an integer and i'll sell you a state.
81
     * Used to test wrapping of PHP methods into xmlrpc methods.
82
     *
83
     * @param integer $num
84
     * @return string
85
     * @throws Exception
86
     */
87 58
    public static function findState($num)
88
    {
89
        // we are lazy ;-)
90 58
        return plain_findstate($num);
91
    }
92
93
    /**
94
     * Returns an instance of stdClass.
95
     * Used to test wrapping of PHP objects with class preservation
96
     */
97 1
    public function returnObject()
98
    {
99 1
        $obj = new stdClass();
100 1
        $obj->hello = 'world';
101 1
        return $obj;
102
    }
103
}
104
105 488
$findstate3_sig = $wrapper->wrapPhpFunction(array('xmlrpcServerMethodsContainer', 'findState'));
106
107 488
$obj = new xmlrpcServerMethodsContainer();
108 488
$findstate4_sig = $wrapper->wrapPhpFunction(array($obj, 'findstate'));
109
110 488
$findstate5_sig = $wrapper->wrapPhpFunction('xmlrpcServerMethodsContainer::findState', '', array('return_source' => true));
111 488
eval($findstate5_sig['source']);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
112
113 488
$findstate6_sig = $wrapper->wrapPhpFunction('plain_findstate', '', array('return_source' => true));
114 488
eval($findstate6_sig['source']);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
115
116 488
$findstate7_sig = $wrapper->wrapPhpFunction(array('xmlrpcServerMethodsContainer', 'findState'), '', array('return_source' => true));
117 488
eval($findstate7_sig['source']);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
118
119
//$obj = new xmlrpcServerMethodsContainer();
120 488
$findstate8_sig = $wrapper->wrapPhpFunction(array($obj, 'findstate'), '', array('return_source' => true));
121 488
eval($findstate8_sig['source']);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
122
123 488
$findstate9_sig = $wrapper->wrapPhpFunction('xmlrpcServerMethodsContainer::findState', '', array('return_source' => true));
124 488
eval($findstate9_sig['source']);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
125
126
$findstate10_sig = array(
127
    /// @todo add a demo/test with a closure
128
    "function" => function ($req) { return findState($req); },
129 488
    "signature" => array(array(Value::$xmlrpcString, Value::$xmlrpcInt)),
130
    "docstring" => 'When passed an integer between 1 and 51 returns the name of a US state, where the integer is the ' .
131
        'index of that state name in an alphabetic order.',
132
);
133
134
$findstate11_sig = $wrapper->wrapPhpFunction(function ($stateNo) { return plain_findstate($stateNo); });
135
136
/// @todo do we really need a new instance ?
137 488
$c = new xmlrpcServerMethodsContainer();
138
139 488
$moreSignatures = $wrapper->wrapPhpClass($c, array('prefix' => 'tests.', 'method_type' => 'all'));
140
141 488
$returnObj_sig =  $wrapper->wrapPhpFunction(array($c, 'returnObject'), '', array('encode_php_objs' => true));
142
143 488
return array_merge(
144
    array(
145 488
        'tests.getStateName.2' => $findstate2_sig,
146 488
        'tests.getStateName.3' => $findstate3_sig,
147 488
        'tests.getStateName.4' => $findstate4_sig,
148 488
        'tests.getStateName.5' => $findstate5_sig,
149 488
        'tests.getStateName.6' => $findstate6_sig,
150 488
        'tests.getStateName.7' => $findstate7_sig,
151 488
        'tests.getStateName.8' => $findstate8_sig,
152 488
        'tests.getStateName.9' => $findstate9_sig,
153 488
        'tests.getStateName.10' => $findstate10_sig,
154 488
        'tests.getStateName.11' => $findstate11_sig,
155 488
        'tests.returnPhpObject' => $returnObj_sig,
156
    ),
157 488
    $moreSignatures
158
);
159