Passed
Push — master ( fdfad6...cfa948 )
by Alec
03:03
created

Calculator::computeErasingLengths()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 10
cc 4
nc 5
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 32
    public static function computeErasingLengths(array $strings): int
10
    {
11 32
        if (empty($strings)) {
12 7
            return 0;
13
        }
14 25
        $lengths = [];
15 25
        foreach ($strings as $string) {
16 25
            $lengths[] = self::computeErasingLength($string);
17
        }
18 25
        if (1 !== count(array_unique($lengths))) {
19 1
            throw new \InvalidArgumentException('Strings have different erasing lengths.');
20
        }
21 24
        return $lengths[0];
22
    }
23
24
    /**
25
     * @param null|string $in
26
     * @return int
27
     */
28 30
    public static function computeErasingLength(?string $in): int
29
    {
30 30
        if (null === $in || Defaults::EMPTY_STRING === $in) {
31 2
            return 0;
32
        }
33 28
        $in = Strip::escCodes($in);
34 28
        $mbSymbolLen = mb_strlen($in);
35 28
        $oneCharLen = strlen($in) / $mbSymbolLen;
36 28
        if (4 === $oneCharLen) {
37 3
            return 2 * $mbSymbolLen;
38
        }
39 28
        return mb_strwidth($in);
40
    }
41
}
42