ClosingHtmlTagRule::validate()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8333
c 0
b 0
f 0
cc 7
nc 4
nop 1
1
<?php
2
3
namespace whm\Smoke\Rules\Html;
4
5
use phm\HttpWebdriverClient\Http\Response\ContentTypeAwareResponse;
6
use Psr\Http\Message\ResponseInterface;
7
use whm\Smoke\Rules\Rule;
8
use whm\Smoke\Rules\ValidationFailedException;
9
10
/**
11
 * This rule checks if a given html document has a closing html tag </html>.
12
 */
13
class ClosingHtmlTagRule implements Rule
14
{
15
    public function validate(ResponseInterface $response)
16
    {
17
        if ($response instanceof ContentTypeAwareResponse) {
18
19
            // @todo this could be part of the StandardRule class
20
            $body = (string)$response->getBody();
21
            $body = preg_replace('/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F,\xFF,\x8B]/', '', $body);
22
23
            if (($response->getStatusCode() < 300 || $response->getStatusCode() >= 400)
24
                && $response->getContentType() === 'text/html'
25
                && strlen($body) > 0
26
            ) {
27
                if (stripos($body, '</html>') === false) {
28
                    throw new ValidationFailedException('Closing html tag is missing (document length: ' . strlen($body) . ').');
29
                }
30
            }
31
        }
32
    }
33
}
34