GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 2ab419...940f7c )
by Robert
32:50
created

XmlResponseFormatter   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 5
dl 0
loc 109
ccs 50
cts 50
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 15 4
C buildXml() 0 34 15
A formatScalarValue() 0 12 3
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\web;
9
10
use DOMDocument;
11
use DOMElement;
12
use DOMText;
13
use yii\base\Arrayable;
14
use yii\base\Component;
15
use yii\helpers\StringHelper;
16
17
/**
18
 * XmlResponseFormatter formats the given data into an XML response content.
19
 *
20
 * It is used by [[Response]] to format response data.
21
 *
22
 * @author Qiang Xue <[email protected]>
23
 * @since 2.0
24
 */
25
class XmlResponseFormatter extends Component implements ResponseFormatterInterface
26
{
27
    /**
28
     * @var string the Content-Type header for the response
29
     */
30
    public $contentType = 'application/xml';
31
    /**
32
     * @var string the XML version
33
     */
34
    public $version = '1.0';
35
    /**
36
     * @var string the XML encoding. If not set, it will use the value of [[Response::charset]].
37
     */
38
    public $encoding;
39
    /**
40
     * @var string the name of the root element.
41
     */
42
    public $rootTag = 'response';
43
    /**
44
     * @var string the name of the elements that represent the array elements with numeric keys.
45
     */
46
    public $itemTag = 'item';
47
    /**
48
     * @var boolean whether to interpret objects implementing the [[\Traversable]] interface as arrays.
49
     * Defaults to `true`.
50
     * @since 2.0.7
51
     */
52
    public $useTraversableAsArray = true;
53
54
55
    /**
56
     * Formats the specified response.
57
     * @param Response $response the response to be formatted.
58
     */
59 16
    public function format($response)
60
    {
61 16
        $charset = $this->encoding === null ? $response->charset : $this->encoding;
62 16
        if (stripos($this->contentType, 'charset') === false) {
63 16
            $this->contentType .= '; charset=' . $charset;
64 16
        }
65 16
        $response->getHeaders()->set('Content-Type', $this->contentType);
66 16
        if ($response->data !== null) {
67 15
            $dom = new DOMDocument($this->version, $charset);
68 15
            $root = new DOMElement($this->rootTag);
69 15
            $dom->appendChild($root);
70 15
            $this->buildXml($root, $response->data);
71 15
            $response->content = $dom->saveXML();
72 15
        }
73 16
    }
74
75
    /**
76
     * @param DOMElement $element
77
     * @param mixed $data
78
     */
79 15
    protected function buildXml($element, $data)
80
    {
81 15
        if (is_array($data) ||
82 10
            ($data instanceof \Traversable && $this->useTraversableAsArray && !$data instanceof Arrayable)
83 15
        ) {
84 10
            foreach ($data as $name => $value) {
85 9
                if (is_int($name) && is_object($value)) {
86 3
                    $this->buildXml($element, $value);
87 9
                } elseif (is_array($value) || is_object($value)) {
88 3
                    $child = new DOMElement(is_int($name) ? $this->itemTag : $name);
89 3
                    $element->appendChild($child);
90 3
                    $this->buildXml($child, $value);
91 3
                } else {
92 9
                    $child = new DOMElement(is_int($name) ? $this->itemTag : $name);
93 9
                    $element->appendChild($child);
94 9
                    $child->appendChild(new DOMText($this->formatScalarValue($value)));
95
                }
96 10
            }
97 15
        } elseif (is_object($data)) {
98 5
            $child = new DOMElement(StringHelper::basename(get_class($data)));
99 5
            $element->appendChild($child);
100 5
            if ($data instanceof Arrayable) {
101 1
                $this->buildXml($child, $data->toArray());
102 1
            } else {
103 4
                $array = [];
104 4
                foreach ($data as $name => $value) {
105 4
                    $array[$name] = $value;
106 4
                }
107 4
                $this->buildXml($child, $array);
108
            }
109 5
        } else {
110 5
            $element->appendChild(new DOMText($this->formatScalarValue($data)));
111
        }
112 15
    }
113
114
    /**
115
     * Formats scalar value to use in XML text node
116
     *
117
     * @param int|string|bool $value
118
     * @return string
119
     * @since 2.0.11
120
     */
121 14
    protected function formatScalarValue($value)
122
    {
123 14
        if ($value === true) {
124 2
            return 'true';
125
        }
126
127 13
        if ($value === false) {
128 2
            return 'false';
129
        }
130
131 12
        return (string) $value;
132
    }
133
}
134