XmlVisitor::addXml()   D
last analyzed

Complexity

Conditions 10
Paths 29

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 24
nc 29
nop 3

How to fix   Complexity   

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:

1
<?php
2
3
namespace Guzzle\Service\Command\LocationVisitor\Request;
4
5
use Guzzle\Http\Message\RequestInterface;
6
use Guzzle\Service\Command\CommandInterface;
7
use Guzzle\Service\Description\Operation;
8
use Guzzle\Service\Description\Parameter;
9
10
/**
11
 * Location visitor used to serialize XML bodies
12
 */
13
class XmlVisitor extends AbstractRequestVisitor
14
{
15
    /** @var \SplObjectStorage Data object for persisting XML data */
16
    protected $data;
17
18
    /** @var bool Content-Type header added when XML is found */
19
    protected $contentType = 'application/xml';
20
21
    public function __construct()
22
    {
23
        $this->data = new \SplObjectStorage();
24
    }
25
26
    /**
27
     * Change the content-type header that is added when XML is found
28
     *
29
     * @param string $header Header to set when XML is found
30
     *
31
     * @return self
32
     */
33
    public function setContentTypeHeader($header)
34
    {
35
        $this->contentType = $header;
0 ignored issues
show
Documentation Bug introduced by
The property $contentType was declared of type boolean, but $header is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
36
37
        return $this;
38
    }
39
40
    public function visit(CommandInterface $command, RequestInterface $request, Parameter $param, $value)
41
    {
42
        $xml = isset($this->data[$command])
43
            ? $this->data[$command]
44
            : $this->createRootElement($param->getParent());
0 ignored issues
show
Documentation introduced by
$param->getParent() is of type object<Guzzle\Service\De...ription\Parameter>|null, but the function expects a object<Guzzle\Service\Description\Operation>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
        $this->addXml($xml, $param, $value);
46
47
        $this->data[$command] = $xml;
48
    }
49
50
    public function after(CommandInterface $command, RequestInterface $request)
51
    {
52
        $xml = null;
53
54
        // If data was found that needs to be serialized, then do so
55
        if (isset($this->data[$command])) {
56
            $xml = $this->finishDocument($this->data[$command]);
57
            unset($this->data[$command]);
58
        } else {
59
            // Check if XML should always be sent for the command
60
            $operation = $command->getOperation();
61
            if ($operation->getData('xmlAllowEmpty')) {
62
                $xmlWriter = $this->createRootElement($operation);
0 ignored issues
show
Compatibility introduced by
$operation of type object<Guzzle\Service\De...ion\OperationInterface> is not a sub-type of object<Guzzle\Service\Description\Operation>. It seems like you assume a concrete implementation of the interface Guzzle\Service\Description\OperationInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
63
                $xml = $this->finishDocument($xmlWriter);
64
            }
65
        }
66
67
        if ($xml) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $xml of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
68
            // Don't overwrite the Content-Type if one is set
69
            if ($this->contentType && !$request->hasHeader('Content-Type')) {
70
                $request->setHeader('Content-Type', $this->contentType);
71
            }
72
            $request->setBody($xml);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Guzzle\Http\Message\RequestInterface as the method setBody() does only exist in the following implementations of said interface: Guzzle\Http\Message\EntityEnclosingRequest.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
73
        }
74
    }
75
76
    /**
77
     * Create the root XML element to use with a request
78
     *
79
     * @param Operation $operation Operation object
80
     *
81
     * @return \XMLWriter
82
     */
83
    protected function createRootElement(Operation $operation)
84
    {
85
        static $defaultRoot = array('name' => 'Request');
86
        // If no root element was specified, then just wrap the XML in 'Request'
87
        $root = $operation->getData('xmlRoot') ?: $defaultRoot;
88
        // Allow the XML declaration to be customized with xmlEncoding
89
        $encoding = $operation->getData('xmlEncoding');
90
91
        $xmlWriter = $this->startDocument($encoding);
92
93
        $xmlWriter->startElement($root['name']);
94
        // Create the wrapping element with no namespaces if no namespaces were present
95
        if (!empty($root['namespaces'])) {
96
            // Create the wrapping element with an array of one or more namespaces
97
            foreach ((array) $root['namespaces'] as $prefix => $uri) {
98
                $nsLabel = 'xmlns';
99
                if (!is_numeric($prefix)) {
100
                    $nsLabel .= ':'.$prefix;
101
                }
102
                $xmlWriter->writeAttribute($nsLabel, $uri);
103
            }
104
        }
105
        return $xmlWriter;
106
    }
107
108
    /**
109
     * Recursively build the XML body
110
     *
111
     * @param \XMLWriter $xmlWriter XML to modify
112
     * @param Parameter  $param     API Parameter
113
     * @param mixed      $value     Value to add
114
     */
115
    protected function addXml(\XMLWriter $xmlWriter, Parameter $param, $value)
116
    {
117
        if ($value === null) {
118
            return;
119
        }
120
121
        $value = $param->filter($value);
122
        $type = $param->getType();
123
        $name = $param->getWireName();
124
        $prefix = null;
125
        $namespace = $param->getData('xmlNamespace');
126
        if (false !== strpos($name, ':')) {
127
            list($prefix, $name) = explode(':', $name, 2);
128
        }
129
130
        if ($type == 'object' || $type == 'array') {
131
            if (!$param->getData('xmlFlattened')) {
132
                $xmlWriter->startElementNS(null, $name, $namespace);
133
            }
134
            if ($param->getType() == 'array') {
135
                $this->addXmlArray($xmlWriter, $param, $value);
136
            } elseif ($param->getType() == 'object') {
137
                $this->addXmlObject($xmlWriter, $param, $value);
138
            }
139
            if (!$param->getData('xmlFlattened')) {
140
                $xmlWriter->endElement();
141
            }
142
            return;
143
        }
144
        if ($param->getData('xmlAttribute')) {
145
            $this->writeAttribute($xmlWriter, $prefix, $name, $namespace, $value);
146
        } else {
147
            $this->writeElement($xmlWriter, $prefix, $name, $namespace, $value);
148
        }
149
    }
150
151
    /**
152
     * Write an attribute with namespace if used
153
     *
154
     * @param  \XMLWriter $xmlWriter XMLWriter instance
155
     * @param  string     $prefix    Namespace prefix if any
156
     * @param  string     $name      Attribute name
157
     * @param  string     $namespace The uri of the namespace
158
     * @param  string     $value     The attribute content
159
     */
160
    protected function writeAttribute($xmlWriter, $prefix, $name, $namespace, $value)
161
    {
162
        if (empty($namespace)) {
163
            $xmlWriter->writeAttribute($name, $value);
164
        } else {
165
            $xmlWriter->writeAttributeNS($prefix, $name, $namespace, $value);
166
        }
167
    }
168
169
    /**
170
     * Write an element with namespace if used
171
     *
172
     * @param  \XMLWriter $xmlWriter XML writer resource
173
     * @param  string     $prefix    Namespace prefix if any
174
     * @param  string     $name      Element name
175
     * @param  string     $namespace The uri of the namespace
176
     * @param  string     $value     The element content
177
     */
178
    protected function writeElement(\XMLWriter $xmlWriter, $prefix, $name, $namespace, $value)
179
    {
180
        $xmlWriter->startElementNS($prefix, $name, $namespace);
181
        if (strpbrk($value, '<>&')) {
182
            $xmlWriter->writeCData($value);
183
        } else {
184
            $xmlWriter->writeRaw($value);
185
        }
186
        $xmlWriter->endElement();
187
    }
188
189
    /**
190
     * Create a new xml writer and start a document
191
     *
192
     * @param  string $encoding document encoding
193
     *
194
     * @return \XMLWriter the writer resource
195
     */
196
    protected function startDocument($encoding)
197
    {
198
        $xmlWriter = new \XMLWriter();
199
        $xmlWriter->openMemory();
200
        $xmlWriter->startDocument('1.0', $encoding);
201
202
        return $xmlWriter;
203
    }
204
205
    /**
206
     * End the document and return the output
207
     *
208
     * @param \XMLWriter $xmlWriter
209
     *
210
     * @return \string the writer resource
211
     */
212
    protected function finishDocument($xmlWriter)
213
    {
214
        $xmlWriter->endDocument();
215
216
        return $xmlWriter->outputMemory();
217
    }
218
219
    /**
220
     * Add an array to the XML
221
     */
222
    protected function addXmlArray(\XMLWriter $xmlWriter, Parameter $param, &$value)
223
    {
224
        if ($items = $param->getItems()) {
225
            foreach ($value as $v) {
226
                $this->addXml($xmlWriter, $items, $v);
227
            }
228
        }
229
    }
230
231
    /**
232
     * Add an object to the XML
233
     */
234
    protected function addXmlObject(\XMLWriter $xmlWriter, Parameter $param, &$value)
235
    {
236
        $noAttributes = array();
237
        // add values which have attributes
238
        foreach ($value as $name => $v) {
239
            if ($property = $param->getProperty($name)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $property is correct as $param->getProperty($name) (which targets Guzzle\Service\Descripti...arameter::getProperty()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
240
                if ($property->getData('xmlAttribute')) {
241
                    $this->addXml($xmlWriter, $property, $v);
242
                } else {
243
                    $noAttributes[] = array('value' => $v, 'property' => $property);
244
                }
245
            }
246
        }
247
        // now add values with no attributes
248
        foreach ($noAttributes as $element) {
249
            $this->addXml($xmlWriter, $element['property'], $element['value']);
250
        }
251
    }
252
}
253