Completed
Push — master ( 747d15...90301e )
by Nils
01:56
created

ValidRule::doValidation()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.9297
c 0
b 0
f 0
cc 6
nc 12
nop 1
1
<?php
2
3
namespace whm\Smoke\Rules\Xml\Sitemap;
4
5
use phm\HttpWebdriverClient\Http\Response\DomAwareResponse;
6
use Psr\Http\Message\ResponseInterface;
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
    const NON_STRICT_SCHEMA = 'nonStrictSchema.xsd';
17
    const INDEX = 'siteindex.xsd';
18
19
    private $strictMode;
20
    private $debug;
21
22
    // protected $contentTypes = array('text/xml', 'application/xml');
23
24
    public function init($strictMode = true, $debug = false)
25
    {
26
        $this->debug = $debug;
27
        $this->strictMode = $strictMode;
28
    }
29
30
    private function getSchema($isIndex)
31
    {
32
        if ($isIndex) {
33
            return __DIR__ . '/' . self::INDEX;
34
        }
35
36
        if ($this->strictMode) {
37
            return __DIR__ . '/' . self::SCHEMA;
38
        } else {
39
            return __DIR__ . '/' . self::NON_STRICT_SCHEMA;
40
        }
41
    }
42
43
    /**
44
     * @param $body
45
     * @param $filename
46
     * @param bool $isIndex
47
     * @throws ValidationFailedException
48
     */
49
    private function validateBody($body, $filename, $isIndex = true)
50
    {
51
        $dom = new \DOMDocument();
52
        @$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...
53
54
        $valid = @$dom->schemaValidate($this->getSchema($isIndex));
55
56
        if (!$valid) {
57
            $lastError = libxml_get_last_error();
58
            throw new ValidationFailedException(
59
                'The given sitemap file (' . $filename . ') did not validate against the sitemap schema (last error: ' . str_replace("\n", '', $lastError->message) . ').');
60
        }
61
    }
62
63
    /**
64
     * @param ResponseInterface $response
65
     * @throws ValidationFailedException
66
     */
67
    protected function doValidation(ResponseInterface $response)
68
    {
69
        $contentType = $response->getHeader('content-type');
70
71
        if ($response instanceof DomAwareResponse) {
72
            $body = (string)$response->getHtmlBody();
73
        } else {
74
            $body = (string)$response->getBody();
75
        }
76
77
        if (is_array($contentType) && $contentType[0] === "application/gzip") {
78
            $body = gzdecode($response->getBody());
79
        }
80
81
        // sitemapindex or urlset
82
        if (preg_match('/<sitemapindex/', $body)) {
83
            $this->validateBody($body, (string)$response->getUri());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\ResponseInterface as the method getUri() does only exist in the following implementations of said interface: phm\HttpWebdriverClient\...t\Chrome\ChromeResponse, phm\HttpWebdriverClient\...t\Guzzle\GuzzleResponse, phm\HttpWebdriverClient\...\Client\Guzzle\Response, phm\HttpWebdriverClient\...esponse\BrowserResponse, whm\Smoke\Http\ConnectionRefusedResponse, whm\Smoke\Http\ErrorResponse.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
84
        } elseif (preg_match('/<urlset/', $body)) {
85
            $this->validateBody($body, (string)$response->getUri(), false);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\ResponseInterface as the method getUri() does only exist in the following implementations of said interface: phm\HttpWebdriverClient\...t\Chrome\ChromeResponse, phm\HttpWebdriverClient\...t\Guzzle\GuzzleResponse, phm\HttpWebdriverClient\...\Client\Guzzle\Response, phm\HttpWebdriverClient\...esponse\BrowserResponse, whm\Smoke\Http\ConnectionRefusedResponse, whm\Smoke\Http\ErrorResponse.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
86
        } else {
87
            throw new ValidationFailedException('The given document is not a valid sitemap. Nether sitemapindex nor urlset element was found. ');
88
        }
89
    }
90
}
91