Completed
Push — master ( 23dd19...ad8447 )
by Craig
05:45
created

ResponseTransformer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 35
c 1
b 0
f 1
dl 0
loc 62
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A trimWhitespace() 0 49 1
A returnUntrimmedBlocks() 0 9 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ThemeModule\Engine;
15
16
use Symfony\Component\HttpFoundation\Response;
17
18
class ResponseTransformer
19
{
20
    public function trimWhitespace(Response $response): void
21
    {
22
        $content = $response->getContent();
23
24
        // Pull out the script blocks
25
        preg_match_all('!<script[^>]*?>.*?</script>!is', $content, $match);
26
        $scriptBlocks = $match[0];
27
        $content = preg_replace(
28
            '!<script[^>]*?>.*?</script>!is',
29
            '@@@TWIG:TRIM:SCRIPT@@@',
30
            $content
31
        );
32
33
        // Pull out the pre blocks
34
        preg_match_all('!<pre[^>]*?>.*?</pre>!is', $content, $match);
35
        $preBlocks = $match[0];
36
        $content = preg_replace(
37
            '!<pre[^>]*?>.*?</pre>!is',
38
            '@@@TWIG:TRIM:PRE@@@',
39
            $content
40
        );
41
42
        // Pull out the textarea blocks
43
        preg_match_all(
44
            '!<textarea[^>]*?>.*?</textarea>!is',
45
            $content,
46
            $match
47
        );
48
        $textareaBlocks = $match[0];
49
        $content = preg_replace(
50
            '!<textarea[^>]*?>.*?</textarea>!is',
51
            '@@@TWIG:TRIM:TEXTAREA@@@',
52
            $content
53
        );
54
55
        // remove all leading spaces, tabs and carriage returns NOT
56
        // preceeded by a php close tag.
57
        $content = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $content));
58
59
        // replace textarea blocks
60
        $this->returnUntrimmedBlocks('@@@TWIG:TRIM:TEXTAREA@@@', $textareaBlocks, $content);
61
62
        // replace pre blocks
63
        $this->returnUntrimmedBlocks('@@@TWIG:TRIM:PRE@@@', $preBlocks, $content);
64
65
        // replace script blocks
66
        $this->returnUntrimmedBlocks('@@@TWIG:TRIM:SCRIPT@@@', $scriptBlocks, $content);
67
68
        $response->setContent($content);
69
    }
70
71
    private function returnUntrimmedBlocks(string $search, string $replace, string &$subject): void
72
    {
73
        $len = mb_strlen($search);
74
        $pos = 0;
75
        for ($i = 0, $count = count($replace); $i < $count; $i++) {
0 ignored issues
show
Bug introduced by
$replace of type string is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        for ($i = 0, $count = count(/** @scrutinizer ignore-type */ $replace); $i < $count; $i++) {
Loading history...
76
            if (false !== ($pos = mb_strpos($subject, $search, $pos))) {
77
                $subject = substr_replace($subject, $replace[$i], $pos, $len);
78
            } else {
79
                break;
80
            }
81
        }
82
    }
83
}
84