Passed
Push — master ( 59aad4...7e370e )
by Jonathan
24:38
created

AbstractTidyMiddleware::doctype()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.432

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 19
ccs 7
cts 10
cp 0.7
crap 4.432
rs 9.9666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Ctw\Middleware\TidyMiddleware;
5
6
use Ctw\Middleware\AbstractMiddleware;
7
8
abstract class AbstractTidyMiddleware extends AbstractMiddleware
9
{
10
    /**
11
     * Default Tidy config (overwrite in site config)
12
     *
13
     * @var array
14
     */
15
    private array $config
16
        = [
17
            'char-encoding'    => 'utf8',
18
            'doctype'          => 'html5',
19
            //'new-blocklevel-tags' => 'article,header,footer,section,nav,aside',
20
            //'new-inline-tags'     => 'video,audio,canvas,ruby,rt,rp',
21
            'bare'             => true,
22
            'break-before-br'  => true,
23
            'indent'           => false,
24
            'indent-spaces'    => 0,
25
            'logical-emphasis' => true,
26
            'numeric-entities' => true,
27
            'quiet'            => true,
28
            'quote-ampersand'  => false,
29
            'tidy-mark'        => false,
30
            'uppercase-tags'   => false,
31
            'vertical-space'   => false,
32
            'wrap'             => 10000,
33
            'wrap-attributes'  => false,
34
            'write-back'       => true,
35
        ];
36
37 4
    public function getConfig(): array
38
    {
39 4
        return $this->config;
40
    }
41
42 8
    public function setConfig(array $config): self
43
    {
44 8
        $this->config = $config;
45
46 8
        return $this;
47
    }
48
49 4
    protected function postProcess(string $htmlModified): string
50
    {
51 4
        $htmlModified = $this->trim($htmlModified);
52 4
        $htmlModified = $this->doctype($htmlModified);
53
54 4
        return $htmlModified;
55
    }
56
57 4
    private function trim(string $htmlModified): string
58
    {
59 4
        return trim($htmlModified);
60
    }
61
62
    /**
63
     * Tidy removes the doctype when parsing HTML5 (bug?).
64
     * This causes the browser to switch to quirks mode, which is undesirable.
65
     * This method re-adds the doctype in the case of HTML5.
66
     *
67
     * @param string $htmlModified
68
     *
69
     * @return string
70
     */
71 4
    private function doctype(string $htmlModified): string
72
    {
73 4
        $config = $this->getConfig();
74
75 4
        if (!isset($config['doctype'])) {
76
            return $htmlModified;
77
        }
78
79 4
        if ('html5' !== $config['doctype']) {
80
            return $htmlModified;
81
        }
82
83 4
        $prefix = '<!DOCTYPE html>';
84
85 4
        if ($prefix === substr($htmlModified, 0, strlen($prefix))) {
86
            return $htmlModified;
87
        }
88
89 4
        return $prefix . PHP_EOL . $htmlModified;
90
    }
91
}
92