Completed
Pull Request — master (#38)
by
unknown
09:58
created

xmlrpcval::getval()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
nc 4
nop 0
dl 0
loc 32
rs 8.439
c 1
b 0
f 0
1
<?php
2
// by Edd Dumbill (C) 1999-2002
3
// <[email protected]>
4
5
// Copyright (c) 1999,2000,2002 Edd Dumbill.
6
// All rights reserved.
7
//
8
// Redistribution and use in source and binary forms, with or without
9
// modification, are permitted provided that the following conditions
10
// are met:
11
//
12
//    * Redistributions of source code must retain the above copyright
13
//      notice, this list of conditions and the following disclaimer.
14
//
15
//    * Redistributions in binary form must reproduce the above
16
//      copyright notice, this list of conditions and the following
17
//      disclaimer in the documentation and/or other materials provided
18
//      with the distribution.
19
//
20
//    * Neither the name of the "XML-RPC for PHP" nor the names of its
21
//      contributors may be used to endorse or promote products derived
22
//      from this software without specific prior written permission.
23
//
24
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28
// REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
35
// OF THE POSSIBILITY OF SUCH DAMAGE.
36
37
/******************************************************************************
38
 *
39
 * *** DEPRECATED ***
40
 *
41
 * This file is only used to insure backwards compatibility
42
 * with the API of the library <= rev. 3
43
 *
44
 * If it is included, the library will work without any further autoloading
45
 *****************************************************************************/
46
47
include_once(__DIR__.'/../src/PhpXmlRpc.php');
48
include_once(__DIR__.'/../src/Value.php');
49
include_once(__DIR__.'/../src/Request.php');
50
include_once(__DIR__.'/../src/Response.php');
51
include_once(__DIR__.'/../src/Client.php');
52
include_once(__DIR__.'/../src/Encoder.php');
53
include_once(__DIR__.'/../src/Helper/Charset.php');
54
include_once(__DIR__.'/../src/Helper/Date.php');
55
include_once(__DIR__.'/../src/Helper/Http.php');
56
include_once(__DIR__.'/../src/Helper/Logger.php');
57
include_once(__DIR__.'/../src/Helper/XMLParser.php');
58
59
60
/* Expose the global variables which used to be defined */
61
PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-1'; // old default
62
PhpXmlRpc\PhpXmlRpc::exportGlobals();
63
64
/* some stuff deprecated enough that we do not want to put it in the new lib version */
65
66
/// @deprecated
67
$GLOBALS['xmlEntities'] = array(
68
    'amp'  => '&',
69
    'quot' => '"',
70
    'lt'   => '<',
71
    'gt'   => '>',
72
    'apos' => "'"
73
);
74
75
// formulate backslashes for escaping regexp
76
// Not in use anymore since 2.0. Shall we remove it?
77
/// @deprecated
78
$GLOBALS['xmlrpc_backslash'] = chr(92).chr(92);
79
80
/* Expose with the old names the classes which have been namespaced */
81
82
class xmlrpcval extends PhpXmlRpc\Value
83
{
84
    /**
85
     * @deprecated
86
     * @param xmlrpcval $o
87
     * @return string
88
     */
89
    public function serializeval($o)
90
    {
91
        // add check? slower, but helps to avoid recursion in serializing broken xmlrpcvals...
92
        //if (is_object($o) && (get_class($o) == 'xmlrpcval' || is_subclass_of($o, 'xmlrpcval')))
93
        //{
94
        $ar = $o->me;
95
        reset($ar);
96
        list($typ, $val) = each($ar);
97
98
        return '<value>' . $this->serializedata($typ, $val) . "</value>\n";
99
        //}
100
    }
101
102
    /**
103
     * @deprecated this code looks like it is very fragile and has not been fixed
104
     * for a long long time. Shall we remove it for 2.0?
105
     */
106
    public function getval()
107
    {
108
        // UNSTABLE
109
        reset($this->me);
110
        list($a, $b) = each($this->me);
0 ignored issues
show
Unused Code introduced by
The assignment to $a is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
111
        // contributed by I Sofer, 2001-03-24
112
        // add support for nested arrays to scalarval
113
        // i've created a new method here, so as to
114
        // preserve back compatibility
115
116
        if (is_array($b)) {
117
            @reset($b);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
118
            while (list($id, $cont) = @each($b)) {
119
                $b[$id] = $cont->scalarval();
120
            }
121
        }
122
123
        // add support for structures directly encoding php objects
124
        if (is_object($b)) {
125
            $t = get_object_vars($b);
126
            @reset($t);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
127
            while (list($id, $cont) = @each($t)) {
128
                $t[$id] = $cont->scalarval();
129
            }
130
            @reset($t);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
131
            while (list($id, $cont) = @each($t)) {
132
                @$b->$id = $cont;
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
133
            }
134
        }
135
        // end contrib
136
        return $b;
137
    }
138
139
    /// reset functionality added by parent class: same as it would happen if no interface was declared
140
    public function count()
141
    {
142
        return 1;
143
    }
144
145
    /// reset functionality added by parent class: same as it would happen if no interface was declared
146
    public function getIterator() {
147
        return new ArrayIterator($this);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \ArrayIterator($this); (ArrayIterator) is incompatible with the return type of the parent method PhpXmlRpc\Value::getIterator of type PhpXmlRpc\ArrayIterator.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
148
    }
149
}
150
151
class xmlrpcmsg extends PhpXmlRpc\Request
152
{
153
}
154
155
class xmlrpcresp extends PhpXmlRpc\Response
156
{
157
}
158
159
class xmlrpc_client extends PhpXmlRpc\Client
160
{
161
}
162
163
/* Expose as global functions the ones which are now class methods */
164
165
/// Wrong speling, but we are adamant on backwards compatibility!
166
function xmlrpc_encode_entitites($data, $srcEncoding='', $destEncoding='')
167
{
168
    return PhpXmlRpc\Helper\Charset::instance()->encodeEntitites($data, $srcEncoding, $destEncoding);
0 ignored issues
show
Bug introduced by
The method encodeEntitites() does not exist on PhpXmlRpc\Helper\Charset. Did you maybe mean encodeEntities()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
169
}
170
171
function iso8601_encode($timeT, $utc=0)
172
{
173
    return PhpXmlRpc\Helper\Date::iso8601Encode($timeT, $utc);
174
}
175
176
function iso8601_decode($iDate, $utc=0)
177
{
178
    return PhpXmlRpc\Helper\Date::iso8601Decode($iDate, $utc);
179
}
180
181
function decode_chunked($buffer)
182
{
183
    return PhpXmlRpc\Helper\Http::decodeChunked($buffer);
184
}
185
186
function php_xmlrpc_decode($xmlrpcVal, $options=array())
187
{
188
    $encoder = new PhpXmlRpc\Encoder();
189
    return $encoder->decode($xmlrpcVal, $options);
190
}
191
192
function php_xmlrpc_encode($phpVal, $options=array())
193
{
194
    $encoder = new PhpXmlRpc\Encoder();
195
    return $encoder->encode($phpVal, $options);
196
}
197
198
function php_xmlrpc_decode_xml($xmlVal, $options=array())
199
{
200
    $encoder = new PhpXmlRpc\Encoder();
201
    return $encoder->decodeXml($xmlVal, $options);
202
}
203
204
function guess_encoding($httpHeader='', $xmlChunk='', $encodingPrefs=null)
205
{
206
    return PhpXmlRpc\Helper\XMLParser::guessEncoding($httpHeader, $xmlChunk, $encodingPrefs);
207
}
208
209
function has_encoding($xmlChunk)
210
{
211
    return PhpXmlRpc\Helper\XMLParser::hasEncoding($xmlChunk);
212
}
213
214
function is_valid_charset($encoding, $validList)
215
{
216
    return PhpXmlRpc\Helper\Charset::instance()->isValidCharset($encoding, $validList);
217
}
218