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

Calculator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A computeErasingLengths() 0 13 4
A computeErasingLength() 0 12 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