Passed
Push — master ( 6dedb2...9b1dd9 )
by Vitaly
06:42
created

FixedCG   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getCommonPrefix() 0 27 3
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 2
    public function getCommonPrefix(AbstractCG $group): string
28
    {
29 2
        $prefix = '';
30
31
        // Convert strings to arrays
32 2
        $initialArray = str_split($this->string);
33 2
        $comparedArray = str_split($group->string);
34
35
        // Get shortest array
36 2
        $minSize = min(count($initialArray), count($comparedArray));
37
38
        // Iterate longest array
39 2
        for ($i = 0; $i < $minSize; $i++) {
40
            // Get existing character or empty string
41 2
            $initialChar = $initialArray[$i] ?? '';
42 2
            $comparedChar = $comparedArray[$i] ?? '';
43
44
            // On first mismatch - break
45 2
            if ($initialChar !== $comparedChar) {
46 2
                break;
47
            }
48
49 2
            $prefix .= $initialChar;
50
        }
51
52 2
        return $prefix;
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58 7
    protected function compareLength(AbstractCG $group): int
59
    {
60
        /**
61
         * Shorter fixed character group has higher priority
62
         */
63 7
        return $group->length <=> $this->length;
64
    }
65
}
66