ValidRule::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\CheckResult;
8
use whm\Smoke\Rules\StandardRule;
9
use whm\Smoke\Rules\ValidationFailedException;
10
11
/**
12
 * This rule checks if a sitemap.xml file is valid.
13
 */
14
class ValidRule extends StandardRule
15
{
16
    const SCHEMA = 'schema.xsd';
17
    const NON_STRICT_SCHEMA = 'nonStrictSchema.xsd';
18
    const INDEX = 'siteindex.xsd';
19
20
    private $strictMode;
21
    private $debug;
22
23
    private $gzipContentTypes = [
24
        'application/x-gzip',
25
        'application/gzip'
26
    ];
27
28
    public function init($strictMode = true, $debug = false)
29
    {
30
        $this->debug = $debug;
31
        $this->strictMode = $strictMode;
32
    }
33
34
    private function getSchema($isIndex)
35
    {
36
        if ($isIndex) {
37
            return __DIR__ . '/' . self::INDEX;
38
        }
39
40
        if ($this->strictMode) {
41
            return __DIR__ . '/' . self::SCHEMA;
42
        } else {
43
            return __DIR__ . '/' . self::NON_STRICT_SCHEMA;
44
        }
45
    }
46
47
    /**
48
     * @param $body
49
     * @param $filename
50
     * @param bool $isIndex
51
     * @throws ValidationFailedException
52
     */
53
    private function validateBody($body, $filename, $isIndex = true)
54
    {
55
        if (!$this->strictMode) {
56
            $body = str_replace('<sitemapindex>', '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">', $body);
57
        }
58
59
        $dom = new \DOMDocument();
60
61
        @$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...
62
63
        $schema = $this->getSchema($isIndex);
64
        $valid = @$dom->schemaValidate($schema);
65
66
        if (!$valid) {
67
            $lastError = libxml_get_last_error();
68
            $message = 'The given sitemap file (' . $filename . ') did not validate against the sitemap schema (last error: ' . str_replace("\n", '', $lastError->message) . ').';
69
            return new CheckResult(CheckResult::STATUS_FAILURE, $message);
70
        } else {
71
            $message = 'The given sitemap file (' . $filename . ') is valid.';
72
            return new CheckResult(CheckResult::STATUS_SUCCESS, $message);
73
74
        }
75
    }
76
77
    /**
78
     * @param ResponseInterface $response
79
     * @throws ValidationFailedException
80
     */
81
    protected function doValidation(ResponseInterface $response)
82
    {
83
        if ($response instanceof DomAwareResponse) {
84
            $body = (string)$response->getHtmlBody();
85
        } else {
86
            $body = (string)$response->getBody();
87
        }
88
89
        if ($response->hasHeader('content-type')) {
90
            $contentType = $response->getHeader('content-type');
91
            if (is_array($contentType) && in_array(strtolower($contentType[0]), $this->gzipContentTypes)) {
92
                $body = gzdecode($response->getBody());
93
            }
94
        }
95
96
        // sitemapindex or urlset
97
        if (preg_match('/<sitemapindex/', $body)) {
98
            return $this->validateBody($body, (string)$response->getUri(), true);
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...
99
        } elseif (preg_match('/<urlset/', $body)) {
100
            return $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...
101
        } else {
102
            throw new ValidationFailedException('The given document is not a valid sitemap. Nether sitemapindex nor urlset element was found. ');
103
        }
104
    }
105
}
106