SimpleAdapter   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
eloc 13
c 1
b 0
f 0
dl 0
loc 19
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A minify() 0 17 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