FixedCG   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCommonPrefix() 0 19 3
A compare() 0 12 2
A compareLength() 0 7 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 fixed length.
10
 *
11
 * @author Vitaly Egorov <[email protected]>
12
 */
13
class FixedCG extends AbstractCG
14
{
15
    /** string Character group matching regexp pattern matching group name */
16
    const PATTERN_GROUP = 'fixed';
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 3
    public function getCommonPrefix(AbstractCG $group): string
28
    {
29 3
        $prefix = '';
30
31
        // Get shortest array
32 3
        $minSize = min(strlen($this->string), strlen($group->string));
33
34
        // Iterate longest array
35 3
        for ($i = 0; $i < $minSize; $i++) {
36
            // On first mismatch - break
37 3
            if ($this->string{$i} !== $group->string{$i}) {
38 2
                break;
39
            }
40
41 3
            $prefix .= $this->string{$i};
42
        }
43
44 3
        return $prefix;
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 11
    public function compare(AbstractCG $group): int
51
    {
52
        /**
53
         * Shorter fixed character group has higher priority
54
         */
55 11
        if ($this->isSameType($group)) {
56 7
            return $this->compareLength($group);
57
        }
58
59
        // Fixed character group always has higher priority
60 5
        return 1;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 7
    protected function compareLength(AbstractCG $group): int
67
    {
68
        /**
69
         * Shorter fixed character group has higher priority
70
         */
71 7
        return $group->length <=> $this->length;
72
    }
73
}
74