AbstractXMLResource   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 13
c 1
b 1
f 0
dl 0
loc 39
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getXMLReader() 0 3 1
A closeXMLReader() 0 4 1
A createXMLReader() 0 3 1
A __construct() 0 2 1
A __destruct() 0 3 1
A validateXMLReader() 0 10 2
1
<?php declare(strict_types = 1);
2
3
namespace Spaghetti\XLSXParser;
4
5
use Spaghetti\XLSXParser\Exception\InvalidXLSXFileException;
6
use Throwable;
7
use XMLReader;
8
9
/**
10
 * @internal
11
 */
12
abstract class AbstractXMLResource
13
{
14
    private ?XMLReader $xml = null;
15
16
    public function __construct(private readonly string $path)
17
    {
18
    }
19
20
    public function __destruct()
21
    {
22
        $this->closeXMLReader();
23
    }
24
25
    protected function getXMLReader(): XMLReader
26
    {
27
        return $this->xml ??= $this->createXMLReader();
28
    }
29
30
    protected function createXMLReader(): XMLReader
31
    {
32
        return $this->validateXMLReader(xml: new XMLReader());
33
    }
34
35
    protected function closeXMLReader(): void
36
    {
37
        $this->xml?->close();
38
        $this->xml = null;
39
    }
40
41
    private function validateXMLReader(XMLReader $xml): XMLReader
42
    {
43
        try {
44
            @$xml->open(uri: $this->path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for open(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

44
            /** @scrutinizer ignore-unhandled */ @$xml->open(uri: $this->path);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
45
            $xml->read();
46
        } catch (Throwable $throwable) {
47
            throw new InvalidXLSXFileException(path: $this->path, previous: $throwable);
48
        }
49
50
        return $xml;
51
    }
52
}
53