XmlLoadException   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 10.14 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 7
loc 69
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A __toString() 7 7 1
A decodeXmlError() 0 28 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
2
3
namespace Fazland\SkebbyRestClient\Exception;
4
5
/**
6
 * Represents an exception thrown on XML load.
7
 *
8
 * @author Massimiliano Braglia <[email protected]>
9
 */
10
class XmlLoadException extends Exception
11
{
12
    /**
13
     * @var string
14
     */
15
    private $response;
16
17
    /**
18
     * XmlLoadException constructor.
19
     *
20
     * @param string $response
21
     * @param array  $errors
22
     */
23
    public function __construct(string $response, array $errors)
24
    {
25
        $this->response = $response;
26
        $this->message = '';
27
28
        foreach ($errors as $error) {
29
            $this->message .= $this->decodeXmlError($error, $response);
30
        }
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 View Code Duplication
    public function __toString(): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38
        return '['.get_class($this).'] '.$this->message."\n".
39
            'Response: '."\n".
40
            $this->response
41
        ;
42
    }
43
44
    /**
45
     * @param \LibXMLError $error
46
     * @param string       $xml
47
     *
48
     * @return string
49
     */
50
    private function decodeXmlError(\LibXMLError $error, string $xml): string
51
    {
52
        $return = $xml[$error->line - 1]."\n";
53
54
        switch ($error->level) {
55
            case LIBXML_ERR_WARNING:
56
                $return .= "Warning $error->code: ";
57
                break;
58
59
            case LIBXML_ERR_ERROR:
60
                $return .= "Error $error->code: ";
61
                break;
62
63
            case LIBXML_ERR_FATAL:
64
                $return .= "Fatal Error $error->code: ";
65
                break;
66
        }
67
68
        $return .= trim($error->message).
69
            "\n  Line: $error->line".
70
            "\n  Column: $error->column";
71
72
        if ($error->file) {
73
            $return .= "\n  File: $error->file";
74
        }
75
76
        return $return."\n\n";
77
    }
78
}
79