XsdValidator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
dl 0
loc 67
c 0
b 0
f 0
wmc 6
lcom 0
cbo 2
ccs 20
cts 25
cp 0.8
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateProtocol() 0 4 1
A validateMetadata() 0 4 1
A validate() 0 38 4
1
<?php
2
3
/*
4
 * This file is part of the LightSAML-Core package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace LightSaml\Validator\Model\Xsd;
13
14
use LightSaml\Error\LightSamlXmlException;
15
16
class XsdValidator
17
{
18
    /**
19
     * @param string $xml
20
     *
21
     * @return XsdError[]
22
     */
23 7
    public function validateProtocol($xml)
24
    {
25 7
        return $this->validate($xml, 'saml-schema-protocol-2.0.xsd');
26
    }
27
28
    /**
29
     * @param string $xml
30
     *
31
     * @return XsdError[]
32
     */
33 2
    public function validateMetadata($xml)
34
    {
35 2
        return $this->validate($xml, 'saml-schema-metadata-2.0.xsd');
36
    }
37
38
    /**
39
     * @param string $xml
40
     * @param string $schema
41
     *
42
     * @return XsdError[]
43
     */
44 9
    private function validate($xml, $schema)
45
    {
46 9
        $result = [];
47 9
        libxml_clear_errors();
48 9
        $doc = new \DOMDocument();
49
50
        set_error_handler(function ($errno, $errstr, $errfile, $errline, array $errcontext) use (&$result) {
0 ignored issues
show
Unused Code introduced by
The parameter $errfile is not used and could be removed.

This check looks from 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 $errline is not used and could be removed.

This check looks from 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 $errcontext is not used and could be removed.

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

Loading history...
51
            $error = new XsdError(XsdError::FATAL, $errno, $errstr, 0, 0);
52
            $result[] = $error;
53 9
        });
54
55 9
        $schemaFile = __DIR__.'/../../../../../xsd/'.$schema;
56 9
        if (!is_file($schemaFile)) {
57
            throw new LightSamlXmlException('Invalid schema specified');
58
        }
59
60 9
        $ok = @$doc->loadXML($xml);
61 9
        if (!$ok) {
62 2
            restore_error_handler();
63
64
            return [
65 2
                new XsdError(XsdError::FATAL, 0, 'Invalid XML', 0, 0),
66
            ];
67
        }
68
69 7
        @$doc->schemaValidate($schemaFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
70
71
        /** @var \LibXMLError[] $errors */
72 7
        $errors = libxml_get_errors();
73 7
        foreach ($errors as $error) {
74
            $err = XsdError::fromLibXMLError($error);
75
            $result[] = $err;
76
        }
77
78 7
        restore_error_handler();
79
80 7
        return $result;
81
    }
82
}
83