Completed
Pull Request — master (#94)
by
unknown
05:56
created

XmlValidXsdRule::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace whm\Smoke\Rules\Xml;
4
5
use whm\Smoke\Http\Response;
6
use whm\Smoke\Rules\StandardRule;
7
use whm\Smoke\Rules\ValidationFailedException;
8
9
/**
10
 * This rule checks if a sitemap.xml file is valid.
11
 */
12
class XmlValidXsdRule extends StandardRule
13
{
14
    private $xsdFiles;
15
16
    protected $contentTypes = array('text/xml', 'application/xml');
17
18
    public function init($xsdFiles)
19
    {
20
        $this->xsdFiles = $xsdFiles;
21
    }
22
23
    protected function doValidation(Response $response)
24
    {
25
        $body = $response->getBody();
26
27
        $dom = new \DOMDocument();
28
        @$dom->loadXML($body);
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...
29
30
        $filename = (string)$response->getUri();
31
32
        $error = false;
33
        $messageParts = array();
34
35
        foreach ($this->xsdFiles AS $xsdFile) {
36
            $valid = @$dom->schemaValidate($xsdFile['xsdfileurl']);
37
38
            if (!$valid) {
39
                $error = true;
40
                $lastError = libxml_get_last_error();
41
42
                $messageParts[] = $xsdFile['xsdfilename'] . ' - ' . $xsdFile['xsdfileurl'] . '(last error: ' . str_replace("\n", '', $lastError->message) . ').';
43
            }
44
        }
45
46 View Code Duplication
        if ($error === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
            $message = 'XML file (' . $filename . ')  does not validate against the following XSD files: ' . implode(", ", $messageParts);
48
            throw new ValidationFailedException($message);
49
        }
50
    }
51
}
52