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

XmlValidXsdRule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 10 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
B doValidation() 4 28 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;
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