Completed
Push — master ( 83fedd...20342f )
by Nils
03:00
created

ValidRule   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 7.35 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
c 1
b 0
f 1
lcom 1
cbo 3
dl 5
loc 68
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSchema() 0 4 1
A validateBody() 5 17 3
A getLocations() 0 17 3
A doValidation() 0 15 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\StandardRule;
8
use whm\Smoke\Rules\ValidationFailedException;
9
10
/**
11
 * This rule checks if a sitemap.xml file is valid.
12
 */
13
class ValidRule extends StandardRule
14
{
15
    const SCHEMA = 'schema.xsd';
16
17
    protected $contentTypes = array('text/xml');
18
19
    private function getSchema()
20
    {
21
        return __DIR__ . '/' . self::SCHEMA;
22
    }
23
24
    private function validateBody($body, $filename)
25
    {
26
        libxml_clear_errors();
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
        $lastError = libxml_get_last_error();
30
        if ($lastError) {
31
            throw new ValidationFailedException(
32
                'The given sitemap file (' . $filename . ') is not well formed (last error: ' . str_replace("\n", '', $lastError->message) . ').');
33
        }
34
        $valid = @$dom->schemaValidate($this->getSchema());
35 View Code Duplication
        if (!$valid) {
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...
36
            $lastError = libxml_get_last_error();
37
            throw new ValidationFailedException(
38
                'The given sitemap file (' . $filename . ') did not validate against the sitemap.xsd (last error: ' . str_replace("\n", '', $lastError->message) . ').');
39
        }
40
    }
41
42
    /**
43
     * @param string
44
     *
45
     * @return array
46
     */
47
    private function getLocations($body)
48
    {
49
        $locations = array();
50
        $xml = simplexml_load_string($body);
51
        $json = json_encode($xml);
52
        $xmlValues = json_decode($json, true);
53
54
        if (isset($xmlValues['sitemap']['loc'])) {
55
            $locations[] = $xmlValues['sitemap']['loc'];
56
        } else {
57
            foreach ($xmlValues['sitemap'] as $sitemap) {
58
                $locations[] = $sitemap['loc'];
59
            }
60
        }
61
62
        return $locations;
63
    }
64
65
    protected function doValidation(Response $response)
66
    {
67
        $body = $response->getBody();
68
69
        // sitemapindex or urlset
70
        if (preg_match('/<sitemapindex/', $body)) {
71
            $allSingleSitemapsUrls = $this->getLocations($body);
72
            if (count($allSingleSitemapsUrls) > 0) {
73
                // we only check the first sitemap we find
74
                $this->validateBody(file_get_contents($allSingleSitemapsUrls[0]), $allSingleSitemapsUrls[0]);
75
            }
76
        } elseif (preg_match('/<urlset/', $body)) {
77
            $this->validateBody($body, (string) $response->getUri());
78
        }
79
    }
80
}
81