CommentPlaceholder   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 71
c 0
b 0
f 0
rs 10
ccs 14
cts 14
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 6 1
A setCDataPlaceholder() 0 6 1
B setConditionalCommentsPlaceholder() 0 31 2
1
<?php
2
3
namespace ArjanSchouten\HtmlMinifier\Placeholders;
4
5
use ArjanSchouten\HtmlMinifier\PlaceholderContainer;
6
7
class CommentPlaceholder implements PlaceholderInterface
8
{
9
    /**
10
     * Replace critical content with a temp placeholder for integrity.
11
     *
12
     * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
13
     *
14
     * @return \ArjanSchouten\HtmlMinifier\MinifyContext
15
     */
16 6
    public function process($context)
17
    {
18 6
        $context->setContents($this->setCDataPlaceholder($context->getContents(), $context->getPlaceholderContainer()));
19
20 6
        return $context->setContents($this->setConditionalCommentsPlaceholder($context->getContents(), $context->getPlaceholderContainer()));
21
    }
22
23
    /**
24
     * Replace CData with a temporary placeholder.
25
     *
26
     * @param string                                           $contents
27
     * @param \ArjanSchouten\HtmlMinifier\PlaceholderContainer $placeholderContainer
28
     *
29
     * @return string
30
     */
31 6
    protected function setCDataPlaceholder($contents, PlaceholderContainer $placeholderContainer)
32
    {
33
        return preg_replace_callback('/<!\[CDATA\[((?!\]\]>).)*\]\]>/s', function ($match) use ($placeholderContainer) {
34 1
            return $placeholderContainer->createPlaceholder($match[0]);
35 6
        }, $contents);
36
    }
37
38
    /**
39
     * Replace conditional placeholders used by Internet Explorer.
40
     *
41
     * @param string                                           $contents
42
     * @param \ArjanSchouten\HtmlMinifier\PlaceholderContainer $placeholderContainer
43
     *
44
     * @return string
45
     */
46 6
    protected function setConditionalCommentsPlaceholder($contents, PlaceholderContainer $placeholderContainer)
47
    {
48 6
        return preg_replace_callback(
49 6
            '/
50
                (
51
                    <!                  # Match the start of a comment
52
                    (-{2})?             # IE can understand comments without dashes
53
                    \[                  # Match the start ("[" is a metachar => escape it)
54
                        (?:(?!\]>).)*   # Match everything except "]>"
55
                    \]>                 # Match end
56
                )
57
                (
58
                    (?:
59
                        (?!<!\[endif\]-{2}?>)
60
                    .)*
61
                )                     # Match everything except end of conditional comment
62
                (
63
                    <!\[endif\]
64
                          (?:\2|(?=>))  # Use a trick to ensure that when dashes are captured they are...
65
                                        # matched at the end! Else make sure that the next char is a ">"!
66
                    >                   # Match the endif with the captured dashes
67
                )
68
            /xis',
69 6
            function ($match) use ($placeholderContainer) {
70 2
                if (!empty(preg_replace('/\s*/', '', $match[3]))) {
71 2
                    return $placeholderContainer->createPlaceholder($match[1]).$match[3].$placeholderContainer->createPlaceholder($match[4]);
72
                } else {
73 1
                    return '';
74
                }
75 6
            }, $contents);
76
    }
77
}
78