Passed
Push — master ( 4505d6...4505d6 )
by Alec
02:17
created

Calculator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 16
c 2
b 0
f 0
dl 0
loc 34
ccs 17
cts 17
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A computeErasingLength() 0 14 4
A erasingLen() 0 11 4
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Spinner\Core;
4
5
use AlecRabbit\Spinner\Settings\Contracts\Defaults;
6
7
class Calculator
8
{
9 24
    public static function computeErasingLength(array $strings): int
10
    {
11
        // TODO remove code duplicate from Settings::class
12 24
        if (empty($strings)) {
13 1
            return 0;
14
        }
15 23
        $lengths = [];
16 23
        foreach ($strings as $string) {
17 23
            $lengths[] = self::erasingLen($string);
18
        }
19 23
        if (1 !== count(array_unique($lengths))) {
20 1
            throw new \InvalidArgumentException('Strings have different erasing lengths');
21
        }
22 22
        return $lengths[0];
23
    }
24
25
26
    /**
27
     * @param null|string $symbol
28
     * @return int
29
     */
30 23
    protected static function erasingLen(?string $symbol): int
31
    {
32 23
        if (null === $symbol || Defaults::EMPTY === $symbol) {
33 2
            return 0;
34
        }
35 21
        $mbSymbolLen = mb_strlen($symbol);
36 21
        $oneCharLen = strlen($symbol) / $mbSymbolLen;
37 21
        if (4 === $oneCharLen) {
38 3
            return 2 * $mbSymbolLen;
39
        }
40 18
        return 1 * $mbSymbolLen;
41
    }
42
}
43