Passed
Pull Request — master (#168)
by Alexander
11:02
created

XmlRenderer::renderVerbose()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 12
ccs 0
cts 10
cp 0
crap 2
rs 9.9332
c 1
b 0
f 0
1
<?php
2
namespace Yiisoft\Yii\Web\ErrorHandler;
3
4
/**
5
 * Formats exception into XML string
6
 */
7
final class XmlRenderer extends ThrowableRenderer
8
{
9
    public function render(\Throwable $t): string
10
    {
11
        $out = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
12
        $out .= "<error>\n";
13
        $out .= $this->tag('message', 'An internal server error occurred');
14
        $out .= '</error>';
15
        return $out;
16
    }
17
18
    public function renderVerbose(\Throwable $t): string
19
    {
20
        $out = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
21
        $out .= "<error>\n";
22
        $out .= $this->tag('type', get_class($t));
23
        $out .= $this->tag('message', $this->cdata($t->getMessage()));
24
        $out .= $this->tag('code', $this->cdata($t->getCode()));
25
        $out .= $this->tag('file', $t->getFile());
26
        $out .= $this->tag('line', $t->getLine());
27
        $out .= $this->tag('trace', $t->getTraceAsString());
28
        $out .= '</error>';
29
        return $out;
30
    }
31
32
    private function tag(string $name, string $value): string
33
    {
34
        return "<$name>" . $value . "</$name>\n";
35
    }
36
37
    private function cdata(string $value): string
38
    {
39
        return '<![CDATA[' . str_replace(']]>', ']]]]><![CDATA[>', $value) . ']]>';
40
    }
41
}
42