DoubleSquareBrackets::transform()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 3
rs 10
1
<?php
2
3
namespace Matecat\SubFiltering\Filters;
4
5
use Matecat\SubFiltering\Commons\AbstractHandler;
6
use Matecat\SubFiltering\Enum\ConstantEnum;
7
use Matecat\SubFiltering\Enum\CTypeEnum;
8
9
class DoubleSquareBrackets extends AbstractHandler {
10
    /**
11
     * @inheritDoc
12
     */
13 86
    public function transform( string $segment ): string {
14 86
        preg_match_all( '/\[\[[^<>\s]+?\]\]/', $segment, $html, PREG_SET_ORDER );
15 86
        foreach ( $html as $pos => $snail_variable ) {
16
            //check if inside twig variable there is a tag because in this case shouldn't replace the content with PH tag
17 2
            if ( !strstr( $snail_variable[ 0 ], ConstantEnum::GTPLACEHOLDER ) ) {
18
                //replace subsequent elements excluding already encoded
19 2
                $segment = preg_replace(
20 2
                        '/' . preg_quote( $snail_variable[ 0 ], '/' ) . '/',
21 2
                        '<ph id="' . $this->getPipeline()->getNextId() . '" ctype="' . CTypeEnum::DOUBLE_SQUARE_BRACKETS . '" equiv-text="base64:' . base64_encode( $snail_variable[ 0 ] ) . '"/>',
22 2
                        $segment,
23 2
                        1
24 2
                );
25
            }
26
        }
27
28 86
        return $segment;
29
    }
30
}