Issues (5)

src/ArrayToTable.php (1 issue)

1
<?php
2
3
namespace Sunnysideup\Vardump;
4
5
class ArrayToTable
6
{
7
    public static function convert(array $array, $maxCols = 20, $maxRows = 20): string
0 ignored issues
show
The parameter $maxRows is not used and could be removed. ( Ignorable by Annotation )

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

7
    public static function convert(array $array, $maxCols = 20, /** @scrutinizer ignore-unused */ $maxRows = 20): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
8
    {
9
        $maxRows = 9999999999;
10
        $html = '';
11
        $rowCount = 0;
12
        if ([] !== $array) {
13
            $html = '
14
            <style>
15
                .vardump-data-table {
16
                    width: 100%;
17
                }
18
                .vardump-data-table td,
19
                .vardump-data-table th {
20
                    padding: 2px;
21
                    font-size: 10px;
22
                    text-align: left;
23
                }
24
            </style>
25
            <table class="vardump-data-table" border="1">
26
            ';
27
            if (self::isMultiDimensionalArray($array)) {
28
                $header = $array[0] ?? $array;
29
                $html .= '<tr>';
30
                $colCount = 0;
31
                foreach ($header as $key => $value) {
32
                    ++$colCount;
33
                    if ($colCount < $maxCols) {
34
                        $html .= '<th>' . htmlspecialchars($key) . '</th>';
35
                    } else {
36
                        $html .= '<th>...</th>';
37
                    }
38
                }
39
40
                $html .= '</tr>';
41
                // data rows
42
43
                foreach ($array as $key => $value) {
44
                    if ($rowCount < $maxRows) {
45
                        ++$rowCount;
46
                        $colCount = 0;
47
                        $html .= '<tr>';
48
                        foreach ($value as $key2 => $value2) {
49
                            ++$colCount;
50
                            if ($colCount < $maxCols) {
51
                                $html .= '<td>' . strip_tags( (string) $value2) . '</td>';
52
                            } else {
53
                                $html .= '<td>...</td>';
54
                            }
55
                        }
56
57
                        $html .= '</tr>';
58
                    }
59
                }
60
            } else {
61
                foreach ($array as $key => $value) {
62
                    if ($rowCount < $maxRows) {
63
                        ++$rowCount;
64
                        $html .= '
65
                            <tr>
66
                                <th>' . $key . '</th>
67
                                <td>' . strip_tags( (string) $value) . '</td>
68
                            </tr>';
69
                    }
70
                }
71
            }
72
73
            $html .= '</table>';
74
            if ($rowCount === $maxRows) {
75
                $html .= '<p>not all rows shown</p>';
76
            }
77
        } else {
78
            $html .= '<p>no data available</p>';
79
        }
80
81
        return $html;
82
    }
83
84
    protected static function isMultiDimensionalArray(array $array): bool
85
    {
86
        return count($array) !== count($array, COUNT_RECURSIVE);
87
    }
88
}
89