Completed
Pull Request — master (#90)
by
unknown
03:03
created

ValidRule::getSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 4
loc 4
rs 10
c 1
b 0
f 1
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace whm\Smoke\Rules\Xml\Sitemap;
4
5
use whm\Smoke\Http\Response;
6
use whm\Smoke\Rules\Rule;
7
use whm\Smoke\Rules\ValidationFailedException;
8
9
/**
10
 * This rule checks if a sitemap.xml file is valid.
11
 */
12 View Code Duplication
class ValidRule implements Rule
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
13
{
14
    const SCHEMA = 'sitemap0_9.xsd';
15
16
    private function getSchema()
17
    {
18
        return __DIR__ . '/' . self::SCHEMA;
19
    }
20
21
    public function validate(Response $response)
22
    {
23
        if ($response->getContentType() !== 'text/xml') {
24
            return;
25
        }
26
27
        $body = $response->getBody();
28
        if (preg_match('/<sitemap/', $body)) {
29
            libxml_clear_errors();
30
            $dom = new \DOMDocument();
31
            @$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...
32
            $lastError = libxml_get_last_error();
33
            if ($lastError) {
34
                throw new ValidationFailedException(
35
                    'The given sitemap.xml file is not well formed (last error: ' .
36
                    str_replace("\n", '', $lastError->message) . ').');
37
            }
38
            $valid = @$dom->schemaValidate($this->getSchema());
39
            if (!$valid) {
40
                $lastError = libxml_get_last_error();
41
                $lastErrorMessage = str_replace("\n", '', $lastError->message);
42
                throw new ValidationFailedException(
43
                    'The given sitemap.xml file did not validate vs. ' .
44
                    $this->getSchema() . ' (last error: ' .
45
                    $lastErrorMessage . ').');
46
            }
47
        }
48
    }
49
}
50