Formatter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
c 2
b 0
f 0
dl 0
loc 76
ccs 28
cts 28
cp 1
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A formatRows() 0 26 4
A formatSingleRow() 0 8 2
A formatKeyValueColumns() 0 13 2
1
<?php
2
3
namespace App\Adventure\Utils;
4
5
/**
6
 * The Formatter class provides methods for formatting data in various ways, such as organizing data into rows,
7
 * aligning columns, and formatting key-value pairs into columns.
8
 */
9
class Formatter
10
{
11
    /**
12
     * Formats a 2D array of data into rows, without displaying keys,
13
     * with equal spacing between columns.
14
     *
15
     * @param  array<int|string, array<int|string, string>> $data    The data to format.
16
     * @param  int                                $gap     The spacing between columns. Default is 1.
17
     * @return string The formatted data as a string.
18
     */
19 2
    public static function formatRows(array $data, int $gap = 1): string
20
    {
21 2
        $columns = array_map(null, ...array_values($data));
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type callable expected by parameter $callback of array_map(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

21
        $columns = array_map(/** @scrutinizer ignore-type */ null, ...array_values($data));
Loading history...
22
23 2
        $columnWidths = [];
24 2
        $counter = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $counter is dead and can be removed.
Loading history...
25
26 2
        foreach ($columns as $column) {
27 2
            $columnWidths[] = max(array_map('strlen', $column));
28
        }
29
30 2
        $formattedRows = [];
31 2
        foreach ($data as $row) {
32 2
            $formattedRow = '';
33 2
            $counter = 0;
34
35 2
            foreach ($row as $value) {
36 2
                $columnWidth = $columnWidths[$counter] ?? 0;
37 2
                $formattedRow .= str_pad($value, $columnWidth + $gap, ' ', STR_PAD_RIGHT);
38 2
                $counter++;
39
            }
40
41 2
            $formattedRows[] = $formattedRow;
42
        }
43
44 2
        return implode("\n", $formattedRows);
45
    }
46
47
    /**
48
     * Formats the values of a 1D array into a single row of data, with equal spacing between columns.
49
     *
50
     * @param  string[] $data The data to format.
51
     * @param  int      $gap The spacing between columns. Default is 1.
52
     * @return string The formatted row as a string.
53
     */
54 8
    public static function formatSingleRow(array $data, int $gap = 1): string
55
    {
56 8
        $formattedRow = '';
57 8
        foreach ($data as $value) {
58 8
            $formattedRow .= str_pad($value, strlen($value) + $gap, ' ', STR_PAD_RIGHT);
59
        }
60
61 8
        return $formattedRow;
62
    }
63
64
    /**
65
     * Formats a 1D associative array of data into columns,
66
     * with keys and values displayed in separate columns.
67
     *
68
     * @param  array<string, string> $data The data to format.
69
     * @param  int                   $gap The spacing between the key and value columns. Default is 1.
70
     * @return string The formatted data as a string.
71
     */
72 2
    public static function formatKeyValueColumns(array $data, int $gap = 1): string
73
    {
74
        // Find the maximum length of the keys
75 2
        $maxLength = max(array_map('strlen', array_keys($data)));
76
77
        // Format each key-value pair
78 2
        $formatted = '';
79 2
        foreach ($data as $key => $value) {
80 2
            $formattedKey = str_pad($key, $maxLength + $gap);
81 2
            $formatted .= $formattedKey . $value . "\n";
82
        }
83
84 2
        return $formatted;
85
    }
86
}
87