XmlParser::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace NoaaCapAlerts\Parser;
4
5
use NoaaCapAlerts\Exceptions\XmlParseException;
6
7
class XmlParser
8
{
9
10
    protected array $output;
11
12 7
    public function __construct()
13
    {
14 7
        $this->output = array();
15 7
    }
16
17 7
    /**
18
     * @throws XmlParseException
19 7
     */
20 7
    public function getArrayFromXml($xml): array
21 7
    {
22 7
        $xmlParser = xml_parser_create();
23
        xml_set_object($xmlParser, $this);
24 7
        xml_set_element_handler($xmlParser, "tagOpen", "tagClosed");
25
        xml_set_character_data_handler($xmlParser, "tagData");
26 7
27 2
        $successfulParse = xml_parse($xmlParser, $xml, true);
28 2
29
        if ($successfulParse === 0) {
30 2
            $errorString = xml_error_string(xml_get_error_code($xmlParser));
31
            $errorLine = xml_get_current_line_number($xmlParser);
32
33 5
            throw new XmlParseException("Error parsing XML: {$errorString} at line {$errorLine}.");
34
        }
35 5
36
        xml_parser_free($xmlParser);
37
38 5
        return $this->output;
39
    }
40
41 5
    protected function tagOpen($parser, $name, $attrs)
0 ignored issues
show
Unused Code introduced by
The parameter $parser is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

41
    protected function tagOpen(/** @scrutinizer ignore-unused */ $parser, $name, $attrs)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42 5
    {
43
        $tag = array(
44
            "name" => $name,
45 5
            "attrs" => $attrs
46 5
        );
47
48 5
        $this->output[] = $tag;
49
    }
50 5
51
    protected function tagClosed($parser, $name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

51
    protected function tagClosed($parser, /** @scrutinizer ignore-unused */ $name)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $parser is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

51
    protected function tagClosed(/** @scrutinizer ignore-unused */ $parser, $name)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52 5
    {
53 5
        $this->output[count($this->output) - 2]['children'][] = $this->output[count($this->output) - 1];
54
55 5
        array_pop($this->output);
56
    }
57 5
58
    protected function tagData($parser, $tagData)
0 ignored issues
show
Unused Code introduced by
The parameter $parser is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

58
    protected function tagData(/** @scrutinizer ignore-unused */ $parser, $tagData)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59 5
    {
60 5
        $notWhitespace = !empty(trim($tagData));
61
62 5
        if ($notWhitespace) {
63
            $this->output[count($this->output) - 1]['tagData'] = $tagData;
64
        }
65
    }
66
}
67
 
68