SimpleAdapter::minify()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 17
rs 9.8666
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Ctw\Middleware\HtmlMinifierMiddleware\Adapter\SimpleAdapter;
5
6
use Ctw\Middleware\HtmlMinifierMiddleware\Adapter\AdapterInterface;
7
8
class SimpleAdapter extends AbstractSimpleAdapter implements AdapterInterface
9
{
10
    public function minify(string $htmlSource): string
11
    {
12
        $lut = [
13
            '/(\n|^)(\x20+|\t)/'      => "\n",
14
            '/(\n|^)\/\/(.*?)(\n|$)/' => "\n",
15
            '/\n/'                    => ' ',
16
            '/\<\!--.*?-->/'          => '',
17
            '/(\x20+|\t)/'            => ' ',   // Delete multi space (Without \n)
18
            '/\>\s+\</'               => '> <', // Replace white spaces between tags with one space
19
            '/(\"|\')\s+\>/'          => '$1>', // Strip whitespaces between quotation ("') and end tags
20
            '/=\s+(\"|\')/'           => '=$1', // Strip whitespaces between = "'
21
        ];
22
23
        $htmlMinified = preg_replace(array_keys($lut), array_values($lut), $htmlSource);
24
        assert(is_string($htmlMinified));
25
26
        return $this->postProcess($htmlMinified);
27
    }
28
}
29