Passed
Push — master ( 433f17...2d5bb4 )
by Alec
03:00
created

Calculator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
eloc 14
c 3
b 0
f 0
dl 0
loc 35
ccs 16
cts 16
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A computeErasingWidths() 0 16 4
A computeErasingWidth() 0 7 3
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Spinner\Core;
4
5
use AlecRabbit\Spinner\Settings\Contracts\Defaults;
6
use function AlecRabbit\Helpers\wcswidth;
7
8
class Calculator
9
{
10
    /**
11
     * @param array $strings
12
     * @return int
13
     */
14 27
    public static function computeErasingWidths(array $strings): int
15
    {
16 27
        if (empty($strings)) {
17 1
            return 0;
18
        }
19 26
        $lengths = [];
20 26
        foreach ($strings as $key => $string) {
21 26
            $length = self::computeErasingWidth($string);
22 26
            $lengths[] = $length;
23 26
            $strings[$key] = [$length, $string];
24
        }
25 26
        if (1 !== count(array_unique($lengths))) {
26
            // dump($strings);
27 1
            throw new \InvalidArgumentException('Strings have different erasing lengths.');
28
        }
29 25
        return $lengths[0];
30
    }
31
32
    /**
33
     * @param null|string $in
34
     * @return int
35
     */
36 30
    public static function computeErasingWidth(?string $in): int
37
    {
38 30
        if (null === $in || Defaults::EMPTY_STRING === $in) {
39 2
            return 0;
40
        }
41 28
        $in = Strip::controlCodes($in);
42 28
        return wcswidth($in);
43
    }
44
}
45