Util   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 3 Features 0
Metric Value
wmc 9
c 6
b 3
f 0
lcom 0
cbo 0
dl 0
loc 84
ccs 24
cts 24
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMaxKeyLength() 0 13 3
A getMaxValueLength() 0 13 3
A buildPatternLine() 0 9 2
A getVisibleStringLength() 0 8 1
1
<?php
2
3
namespace Matks\Vivian;
4
5
class Util
6
{
7
    const ESCAPE_CHARACTER_REGEX = '\\033\[\d+m';
8
9
    /**
10
     * Find longest string length among keys
11
     *
12
     * Only visible characters are considered
13
     *
14
     * @param array $array
15
     *
16
     * @return int
17
     */
18
    public static function getMaxKeyLength(array $array)
19
    {
20 1
        $maxKeyLength = '0';
21 1
        foreach ($array as $key => $value) {
22 1
            $currentLength = static::getVisibleStringLength($key);
23
24 1
            if ($currentLength > $maxKeyLength) {
25 1
                $maxKeyLength = $currentLength;
26 1
            }
27 1
        }
28
29 1
        return $maxKeyLength;
30
    }
31
32
    /**
33
     * Find longest string length among values
34
     *
35
     * Only visible characters are considered
36
     *
37
     * @param array $array
38
     *
39
     * @return int
40
     */
41
    public static function getMaxValueLength(array $array)
42
    {
43 1
        $maxValueLength = '0';
44 1
        foreach ($array as $value) {
45 1
            $currentLength = static::getVisibleStringLength($value);
46
47 1
            if ($currentLength > $maxValueLength) {
48 1
                $maxValueLength = $currentLength;
49 1
            }
50 1
        }
51
52 1
        return $maxValueLength;
53
    }
54
55
    /**
56
     * Build a string composed of $pattern repeated $length times
57
     *
58
     * @param string $pattern
59
     * @param int    $length
60
     *
61
     * @return string
62
     */
63
    public static function buildPatternLine($pattern, $length)
64
    {
65 1
        $result = '';
66 1
        for ($i = 0; $i < $length; $i++) {
67 1
            $result .= $pattern;
68 1
        }
69
70 1
        return $result;
71
    }
72
73
    /**
74
     * Compute string length of only visible characters
75
     *
76
     * @param $string
77
     *
78
     * @return int
79
     */
80
    public static function getVisibleStringLength($string)
81
    {
82
        // remove escape characters
83 1
        $pattern     = '#' . static::ESCAPE_CHARACTER_REGEX . '#';
84 1
        $cleanString = preg_replace($pattern, '', $string);
85
86 1
        return strlen($cleanString);
87
    }
88
}
89