XmlUtils::getXpath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Administrador
5
 * Date: 04/10/2017
6
 * Time: 04:41 PM.
7
 */
8
9
namespace Greenter\Report;
10
11
/**
12
 * Class XmlUtils.
13
 */
14
final class XmlUtils
15
{
16
    const EXT_NAMESPACE = 'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2';
17
    const DS_NAMESPACE = 'http://www.w3.org/2000/09/xmldsig#';
18
    const DIGEST_QUERY = 'ext:ExtensionContent/ds:Signature/ds:SignedInfo/ds:Reference/ds:DigestValue';
19
20
    /**
21
     * @param string $xml
22
     *
23
     * @return string
24
     */
25 4
    public function getHashSign($xml)
26
    {
27 4
        $doc = new \DOMDocument();
28 4
        @$doc->loadXML($xml);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for loadXML(). 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

28
        /** @scrutinizer ignore-unhandled */ @$doc->loadXML($xml);

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...
29
30 4
        return $this->getHashSignFromDoc($doc);
31
    }
32
33
    /**
34
     * @param string $filename
35
     *
36
     * @return string
37
     */
38 4
    public function getHashSignFromFile($filename)
39
    {
40 4
        $doc = new \DOMDocument();
41 4
        @$doc->load($filename);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for load(). 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

41
        /** @scrutinizer ignore-unhandled */ @$doc->load($filename);

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...
42
43 4
        return $this->getHashSignFromDoc($doc);
44
    }
45
46
    /**
47
     * @param \DOMDocument $document
48
     *
49
     * @return string
50
     */
51 11
    public function getHashSignFromDoc(\DOMDocument $document)
52
    {
53 11
        $xpt = $this->getXpath($document);
54
55 11
        $exts = $xpt->query('ext:UBLExtensions/ext:UBLExtension', $document->documentElement);
56 11
        if ($exts->length == 0) {
57 1
            return '';
58
        }
59
60 10
        return $this->getHash($exts, $xpt);
0 ignored issues
show
Bug introduced by
It seems like $exts can also be of type false; however, parameter $exts of Greenter\Report\XmlUtils::getHash() does only seem to accept DOMNodeList, maybe add an additional type check? ( Ignorable by Annotation )

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

60
        return $this->getHash(/** @scrutinizer ignore-type */ $exts, $xpt);
Loading history...
61
    }
62
63
    /**
64
     * @param \DOMDocument $document
65
     *
66
     * @return \DOMXPath
67
     */
68 11
    private function getXpath(\DOMDocument $document)
69
    {
70 11
        $xpt = new \DOMXPath($document);
71 11
        $xpt->registerNamespace('ext', self::EXT_NAMESPACE);
72 11
        $xpt->registerNamespace('ds', self::DS_NAMESPACE);
73
74 11
        return $xpt;
75
    }
76
77
    /**
78
     * @param \DOMNodeList $exts
79
     * @param \DOMXPath $xpt
80
     * @return string
81
     */
82 10
    private function getHash(\DOMNodeList $exts, \DOMXPath $xpt)
83
    {
84 10
        for ($i = $exts->length; $i-- > 0;) {
85 10
            $nodeSign = $exts->item($i);
86 10
            $hash = $xpt->query(self::DIGEST_QUERY, $nodeSign);
87
88 10
            if ($hash->length == 0) {
89 4
                continue;
90
            }
91
92 9
            return $hash->item(0)->nodeValue;
93
        }
94
95 1
        return '';
96
    }
97
}
98