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

Calculator::erasingLen()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 10
cc 4
nc 3
nop 1
crap 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