FixedCG::compare()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
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