Extension::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Bavix\Flow\Minify;
4
5
abstract class Extension
6
{
7
8
    /**
9
     * @var string
10
     */
11
    protected $buffer;
12
13
    /**
14
     * Extension constructor.
15
     *
16
     * @param string $buffer
17
     */
18 3
    public function __construct(string $buffer)
19
    {
20 3
        $this->buffer = $buffer;
21 3
    }
22
23
    /**
24
     * @param string $replace
25
     *
26
     * @return string
27
     */
28 3
    protected function pattern(string $replace): string
29
    {
30 3
        return '~' . \str_replace('~', '\\~', $replace) . '~ui';
31
    }
32
33
    /**
34
     * @param string $replace
35
     * @param string $replacement
36
     */
37 3
    protected function replace(string $replace, string $replacement)
38
    {
39 3
        $this->buffer = \preg_replace(
40 3
            $this->pattern($replace),
41 3
            $replacement,
42 3
            $this->buffer
43
        );
44 3
    }
45
46
    /**
47
     * @return void
48
     */
49 3
    public function apply()
50
    {
51 3
        foreach ($this->patterns() as $replace => $replacement)
52
        {
53 3
            $this->replace($replace, $replacement);
54
        }
55 3
    }
56
57
    /**
58
     * @return string
59
     */
60 3
    public function __toString(): string
61
    {
62 3
        return $this->buffer;
63
    }
64
65
    /**
66
     * @return array
67
     */
68
    abstract protected function patterns(): array;
69
70
}
71