Completed
Push — master ( d16f9c...38f88f )
by Vitaly
06:03
created

FixedCG::getCommonPrefix()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 3
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 2
    public function getCommonPrefix(AbstractCG $group): string
28
    {
29 2
        $prefix = '';
30
31
        // Get shortest array
32 2
        $minSize = min(strlen($this->string), strlen($group->string));
33
34
        // Iterate longest array
35 2
        for ($i = 0; $i < $minSize; $i++) {
36
            // On first mismatch - break
37 2
            if ($this->string{$i} !== $group->string{$i}) {
38 2
                break;
39
            }
40
41 2
            $prefix .= $this->string{$i};
42
        }
43
44 2
        return $prefix;
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 7
    protected function compareLength(AbstractCG $group): int
51
    {
52
        /**
53
         * Shorter fixed character group has higher priority
54
         */
55 7
        return $group->length <=> $this->length;
56
    }
57
}
58