BitNormalizer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 76.19%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 0
loc 44
c 0
b 0
f 0
ccs 16
cts 21
cp 0.7619
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D getNormalized() 0 35 10
1
<?php
2
3
/**
4
 * @author KonstantinKuklin <[email protected]>
5
 */
6
7
namespace KonstantinKuklin\DoctrineCompressedFields;
8
9
use KonstantinKuklin\DoctrineCompressedFields\Exception\WrongBitMappingRule;
10
11
class BitNormalizer
12
{
13
    /**
14
     * @param string[] $bitList
15
     *
16
     * @return int[]
17
     * @throws \Exception
18
     */
19 8
    public static function getNormalized(array $bitList)
20
    {
21 8
        $bitListNormalized = [];
22 8
        foreach ($bitList as $bitRow) {
23 8
            $matches = [];
24 8
            preg_match('/^(?<first>[\d]+)(-(?<second>[\d]+))?$/', (string)$bitRow, $matches);
25 8
            if (empty($matches)) {
26
                throw new WrongBitMappingRule("Incorrect Bit syntax: '{$bitRow}'");
27
            }
28
29 8
            if (!isset($matches['second'])) {
30 8
                if (isset($bitListNormalized[$matches['first']])) {
31
                    throw new WrongBitMappingRule('You can`t add "-" after number.');
32
                }
33 8
                $bitListNormalized[$matches['first']] = (int)$matches['first'];
34 8
                continue;
35
            }
36
37 1
            if ($matches['first'] > $matches['second']) {
38
                throw new WrongBitMappingRule("Start bit zone should be greater than end. But got: {$matches['first']} - {$matches['second']}");
39
            }
40 1
            if (!is_numeric($matches['first']) || !is_numeric($matches['second'])) {
41
                throw new WrongBitMappingRule("Start and end of zone should be numbers, but got: {$matches['first']} - {$matches['second']}");
42
            }
43
44 1
            for ($i = (int)$matches['first'], $end = $matches['second']; $i <= $end; $i++) {
45 1
                if (isset($bitListNormalized[$i])) {
46
                    throw new WrongBitMappingRule("Bit {$bitListNormalized[$i]} used more than once");
47
                }
48 1
                $bitListNormalized[$i] = $i; // Bits started from 1, but in array we start working with 0
49
            }
50
        }
51
52 8
        return array_values($bitListNormalized);
53
    }
54
}
55