Passed
Push — main ( c57f14...b113cb )
by Thierry
02:36
created

Util::selectFieldIsValid()   B

Complexity

Conditions 7
Paths 22

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 9
c 1
b 0
f 0
nc 22
nop 2
dl 0
loc 11
rs 8.8333
1
<?php
2
3
namespace Lagdo\DbAdmin\Db;
4
5
use Lagdo\DbAdmin\Driver\Entity\TableEntity;
6
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity;
7
use Lagdo\DbAdmin\Driver\Input;
8
use Lagdo\DbAdmin\Driver\TranslatorInterface;
9
use Lagdo\DbAdmin\Driver\UtilInterface;
10
use Lagdo\DbAdmin\Driver\UtilTrait;
0 ignored issues
show
Bug introduced by
The type Lagdo\DbAdmin\Driver\UtilTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
use function preg_match;
13
use function strlen;
14
15
class Util implements UtilInterface
16
{
17
    use UtilTrait;
18
    use Traits\UtilTrait;
19
    use Traits\SelectUtilTrait;
20
    use Traits\QueryInputTrait;
21
    use Traits\SelectInputTrait;
22
    use Traits\QueryUtilTrait;
23
    use Traits\DumpUtilTrait;
24
25
    /**
26
     * The constructor
27
     *
28
     * @param TranslatorInterface $trans
29
     * @param Input $input
30
     */
31
    public function __construct(TranslatorInterface $trans, Input $input)
32
    {
33
        $this->trans = $trans;
0 ignored issues
show
Bug Best Practice introduced by
The property trans does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
        $this->input = $input;
0 ignored issues
show
Bug Best Practice introduced by
The property input does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40
    public function name(): string
41
    {
42
        return '<a href="https://www.adminer.org/"' . $this->blankTarget() . ' id="h1">Adminer</a>';
43
    }
44
45
    /**
46
     * Get a target="_blank" attribute
47
     *
48
     * @return string
49
     */
50
    public function blankTarget(): string
51
    {
52
        return ' target="_blank" rel="noreferrer noopener"';
53
    }
54
55
    /**
56
     * Find unique identifier of a row
57
     *
58
     * @param array $row
59
     * @param array $indexes Result of indexes()
60
     *
61
     * @return array
62
     */
63
    public function uniqueIds(array $row, array $indexes): array
64
    {
65
        foreach ($indexes as $index) {
66
            if (preg_match('~PRIMARY|UNIQUE~', $index->type)) {
67
                $ids = [];
68
                foreach ($index->columns as $key) {
69
                    if (!isset($row[$key])) { // NULL is ambiguous
70
                        continue 2;
71
                    }
72
                    $ids[$key] = $row[$key];
73
                }
74
                return $ids;
75
            }
76
        }
77
        return [];
78
    }
79
80
    /**
81
     * Table caption used in navigation and headings
82
     *
83
     * @param TableEntity $table
84
     *
85
     * @return string
86
     */
87
    public function tableName(TableEntity $table): string
88
    {
89
        return $this->html($table->name);
90
    }
91
92
    /**
93
     * Field caption used in select and edit
94
     *
95
     * @param TableFieldEntity $field Single field returned from fields()
96
     * @param int $order Order of column in select
97
     *
98
     * @return string
99
     */
100
    public function fieldName(TableFieldEntity $field, /** @scrutinizer ignore-unused */ int $order = 0): string
101
    {
102
        return '<span title="' . $this->html($field->fullType) . '">' . $this->html($field->name) . '</span>';
103
    }
104
105
    /**
106
     * Value printed in select table
107
     *
108
     * @param mixed $value HTML-escaped value to print
109
     * @param string $type Field type
110
     * @param mixed $original Original value before escaping
111
     *
112
     * @return string
113
     */
114
    private function getSelectFieldValue($value, string $type, $original): string
115
    {
116
        if ($value === null) {
117
            return '<i>NULL</i>';
118
        }
119
        if (preg_match('~char|binary|boolean~', $type) && !preg_match('~var~', $type)) {
120
            return "<code>$value</code>";
121
        }
122
        if (preg_match('~blob|bytea|raw|file~', $type) && !$this->isUtf8($value)) {
123
            return '<i>' . $this->trans->lang('%d byte(s)', strlen($original)) . '</i>';
124
        }
125
        if (preg_match('~json~', $type)) {
126
            return "<code>$value</code>";
127
        }
128
        if ($this->isMail($value)) {
129
            return '<a href="' . $this->html("mailto:$value") . '">' . $value . '</a>';
130
        }
131
        elseif ($this->isUrl($value)) {
132
            // IE 11 and all modern browsers hide referrer
133
            return '<a href="' . $this->html($value) . '"' . $this->blankTarget() . '>' . $value . '</a>';
134
        }
135
        return $value;
136
    }
137
138
    /**
139
     * Format value to use in select
140
     *
141
     * @param TableFieldEntity $field
142
     * @param mixed $value
143
     * @param int|string|null $textLength
144
     *
145
     * @return string
146
     */
147
    public function selectValue(TableFieldEntity $field, $value, $textLength): string
148
    {
149
        // if (\is_array($value)) {
150
        //     $expression = '';
151
        //     foreach ($value as $k => $v) {
152
        //         $expression .= '<tr>' . ($value != \array_values($value) ?
153
        //             '<th>' . $this->html($k) :
154
        //             '') . '<td>' . $this->selectValue($field, $v, $textLength);
155
        //     }
156
        //     return "<table cellspacing='0'>$expression</table>";
157
        // }
158
        // if (!$link) {
159
        //     $link = $this->selectLink($value, $field);
160
        // }
161
        $expression = $value;
162
        if (!empty($expression)) {
163
            if (!$this->isUtf8($expression)) {
164
                $expression = "\0"; // htmlspecialchars of binary data returns an empty string
165
            } elseif ($textLength != '' && $this->isShortable($field)) {
166
                // usage of LEFT() would reduce traffic but complicate query -
167
                // expected average speedup: .001 s VS .01 s on local network
168
                $expression = $this->shortenUtf8($expression, \max(0, +$textLength));
169
            } else {
170
                $expression = $this->html($expression);
171
            }
172
        }
173
        return $this->getSelectFieldValue($expression, $field->type, $value);
174
    }
175
}
176