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

VariableCG   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 45
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B compareLength() 0 22 4
A variableHasFilter() 0 4 1
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