Completed
Push — master ( 57b966...17d4dc )
by Vitaly
02:09
created

VariableCG::compareLength()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 11
cts 11
cp 1
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
crap 4
1
<?php declare(strict_types=1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 06.04.17 at 07:34
5
 */
6
namespace samsonframework\stringconditiontree\string;
7
8
/**
9
 * This class describes character group with variable length.
10
 *
11
 * @author Vitaly Egorov <[email protected]>
12
 */
13
class VariableCG extends AbstractCharacterGroup
14
{
15
    /** string Character group matching regexp pattern matching group name */
16
    const PATTERN_GROUP = 'variable';
17
18
    /** string Regular expression matching character group */
19
    const PATTERN_REGEXP = '{.*?}';
20
21
    /** string Character group matching regexp pattern */
22
    const PATTERN = '(?<'.self::PATTERN_GROUP.'>'.self::PATTERN_REGEXP.')';
23
24
    /**
25
     * @inheritdoc
26
     */
27 8
    protected function compareLength(AbstractCharacterGroup $group): int
28
    {
29 8
        $return = 0;
30
31
        /** @var VariableCG $group */
32 8
        $variableFiltered = $this->variableHasFilter();
33 8
        $comparedFiltered = $group->variableHasFilter();
34
35
        // Filtered variable character group has priority
36 8
        if ($variableFiltered) {
37 4
            $return = 1;
38
            // Both are filtered
39 4
            if ($comparedFiltered) {
40
                // Longer Variable character group has higher priority
41 4
                $return = $this->length <=> $group->length;
42
            }
43 6
        } elseif ($comparedFiltered) {
44 1
            $return = -1;
45
        }
46
47 8
        return $return;
48
    }
49
50
    /**
51
     * @return bool Return true if variable character group has filter
52
     */
53 8
    public function variableHasFilter(): bool
54
    {
55 8
        return strpos($this->string, ':') !== false;
56
    }
57
}
58