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

Calculator::computeErasingWidth()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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