BaseDocument::loadDOM()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rs\XmlFilter\Document;
6
7
use Webmozart\Assert\Assert;
8
9
abstract class BaseDocument
10
{
11 2
    public function loadFile(string $file) : Element
12
    {
13 2
        Assert::readable($file);
14
15 2
        return $this->loadString(file_get_contents($file));
16
    }
17
18 2
    public function loadStream($stream) : Element
19
    {
20 2
        Assert::resource($stream);
21
22 2
        $rewinded = @rewind($stream);
23
24 2
        Assert::true($rewinded, 'could not rewind Stream');
25
26 2
        return $this->loadString(stream_get_contents($stream));
27
    }
28
29
    abstract public function loadString(string $doc) : Element;
30
31
    abstract public function loadDOM(\DOMNode $dom) : Element;
32
33 2
    public function loadSimpleXml(\SimpleXMLElement $element) : Element
34
    {
35 2
        $xmlString = $element->asXML();
36
37 2
        Assert::string($xmlString, '\SimpleXMLElement could be converted to string');
38
39 2
        return $this->loadString($xmlString);
0 ignored issues
show
Security Bug introduced by
It seems like $xmlString defined by $element->asXML() on line 35 can also be of type false; however, Rs\XmlFilter\Document\BaseDocument::loadString() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
40
    }
41
42 16
    public function load($content) : Element
43
    {
44 16
        if ($content instanceof \SimpleXMLElement) {
45 2
            return $this->loadSimpleXml($content);
46
        } elseif ($content instanceof \DOMNode) {
47 3
            return $this->loadDOM($content);
48 11
        } elseif (is_resource($content)) {
49 2
            return $this->loadStream($content);
50 9
        } elseif (is_file($content)) {
51 2
            return $this->loadFile($content);
52
        }
53
54 7
        return $this->loadString($content);
55
    }
56
}
57