GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

FieldInput   F
last analyzed

Complexity

Total Complexity 61

Size/Duplication

Total Lines 391
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 147
c 0
b 0
f 0
dl 0
loc 391
rs 3.52
wmc 61

24 Methods

Rating   Name   Duplication   Size   Complexity  
A allowDatasourceOutputGrouping() 0 3 1
A canFilter() 0 3 1
A __replaceAmpersands() 0 3 1
A isSortable() 0 3 1
A createTable() 0 4 1
A allowDatasourceParamOutput() 0 3 1
A canPrePopulate() 0 3 1
A getImportModes() 0 5 1
A displayPublishPanel() 0 15 5
A setFromPOST() 0 6 2
A __applyValidationRules() 0 5 2
A processRawFieldData() 0 15 2
A __construct() 0 7 1
A commit() 0 17 4
A buildSortingSQL() 0 15 2
A getExportModes() 0 6 1
A buildSortingSelectSQL() 0 3 1
B prepareExportValue() 0 20 7
A appendFormattedElement() 0 19 4
A checkPostFieldData() 0 19 6
A groupRecords() 0 24 5
B buildDSRetrievalSQL() 0 49 7
A displaySettingsPanel() 0 9 1
A prepareImportValue() 0 12 3

How to fix   Complexity   

Complex Class

Complex classes like FieldInput often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FieldInput, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * @package toolkit
5
 */
6
7
/**
8
 * A simple Input field that essentially maps to HTML's `<input type='text'/>`.
9
 */
10
class FieldInput extends Field implements ExportableField, ImportableField
11
{
12
    public function __construct()
13
    {
14
        parent::__construct();
15
        $this->_name = __('Text Input');
0 ignored issues
show
Bug Best Practice introduced by
The property _name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
16
        $this->_required = true;
17
18
        $this->set('required', 'no');
19
    }
20
21
    /*-------------------------------------------------------------------------
22
        Definition:
23
    -------------------------------------------------------------------------*/
24
25
    public function canFilter()
26
    {
27
        return true;
28
    }
29
30
    public function canPrePopulate()
31
    {
32
        return true;
33
    }
34
35
    public function isSortable()
36
    {
37
        return true;
38
    }
39
40
    public function allowDatasourceOutputGrouping()
41
    {
42
        return true;
43
    }
44
45
    public function allowDatasourceParamOutput()
46
    {
47
        return true;
48
    }
49
50
    /*-------------------------------------------------------------------------
51
        Setup:
52
    -------------------------------------------------------------------------*/
53
54
    public function createTable()
55
    {
56
        return Symphony::Database()->query(
57
            "CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` (
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal CREATE TABLE IF NOT EXISTS `tbl_entries_data_ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal ` (\n `id` ...OLLATE=utf8_unicode_ci; does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Bug introduced by
Are you sure $this->get('id') of type array|mixed|null can be used in concatenation? ( Ignorable by Annotation )

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

57
            "CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . /** @scrutinizer ignore-type */ $this->get('id') . "` (
Loading history...
58
              `id` int(11) unsigned NOT null auto_increment,
59
              `entry_id` int(11) unsigned NOT null,
60
              `handle` varchar(255) default null,
61
              `value` varchar(255) default null,
62
              PRIMARY KEY  (`id`),
63
              UNIQUE KEY `entry_id` (`entry_id`),
64
              KEY `handle` (`handle`),
65
              KEY `value` (`value`)
66
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
67
        );
68
    }
69
70
    /*-------------------------------------------------------------------------
71
        Utilities:
72
    -------------------------------------------------------------------------*/
73
74
    private function __applyValidationRules($data)
75
    {
76
        $rule = $this->get('validator');
77
78
        return ($rule ? General::validateString($data, $rule) : true);
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
79
    }
80
81
    private function __replaceAmpersands($value)
82
    {
83
        return preg_replace('/&(?!(#[0-9]+|#x[0-9a-f]+|amp|lt|gt);)/i', '&amp;', trim($value));
84
    }
85
86
    /*-------------------------------------------------------------------------
87
        Settings:
88
    -------------------------------------------------------------------------*/
89
90
    public function setFromPOST(array $settings = array())
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$settings" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$settings"; expected 0 but found 1
Loading history...
91
    {
92
        parent::setFromPOST($settings);
93
94
        if ($this->get('validator') == '') {
95
            $this->remove('validator');
96
        }
97
    }
98
99
    public function displaySettingsPanel(XMLElement &$wrapper, $errors = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$errors" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$errors"; expected 0 but found 1
Loading history...
100
    {
101
        parent::displaySettingsPanel($wrapper, $errors);
102
103
        // Validation rule
104
        $this->buildValidationSelect($wrapper, $this->get('validator'), 'fields['.$this->get('sortorder').'][validator]', 'input', $errors);
0 ignored issues
show
Bug introduced by
It seems like $this->get('validator') can also be of type array; however, parameter $selected of Field::buildValidationSelect() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

104
        $this->buildValidationSelect($wrapper, /** @scrutinizer ignore-type */ $this->get('validator'), 'fields['.$this->get('sortorder').'][validator]', 'input', $errors);
Loading history...
Bug introduced by
Are you sure $this->get('sortorder') of type array|mixed|null can be used in concatenation? ( Ignorable by Annotation )

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

104
        $this->buildValidationSelect($wrapper, $this->get('validator'), 'fields['./** @scrutinizer ignore-type */ $this->get('sortorder').'][validator]', 'input', $errors);
Loading history...
105
106
        // Requirements and table display
107
        $this->appendStatusFooter($wrapper);
108
    }
109
110
    public function commit()
111
    {
112
        if (!parent::commit()) {
113
            return false;
114
        }
115
116
        $id = $this->get('id');
117
118
        if ($id === false) {
119
            return false;
120
        }
121
122
        $fields = array('validator'=>null);
123
124
        $fields['validator'] = ($fields['validator'] == 'custom' ? null : $this->get('validator'));
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
125
126
        return FieldManager::saveSettings($id, $fields);
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type array; however, parameter $field_id of FieldManager::saveSettings() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

126
        return FieldManager::saveSettings(/** @scrutinizer ignore-type */ $id, $fields);
Loading history...
127
    }
128
129
    /*-------------------------------------------------------------------------
130
        Publish:
131
    -------------------------------------------------------------------------*/
132
133
    public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWithError = null, $fieldnamePrefix = null, $fieldnamePostfix = null, $entry_id = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$data" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$data"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$flagWithError" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$flagWithError"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$fieldnamePrefix" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$fieldnamePrefix"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$fieldnamePostfix" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$fieldnamePostfix"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$entry_id" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$entry_id"; expected 0 but found 1
Loading history...
134
    {
135
        $value = General::sanitize(isset($data['value']) ? $data['value'] : null);
136
        $label = Widget::Label($this->get('label'));
0 ignored issues
show
Bug introduced by
It seems like $this->get('label') can also be of type array; however, parameter $name of Widget::Label() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

136
        $label = Widget::Label(/** @scrutinizer ignore-type */ $this->get('label'));
Loading history...
137
138
        if ($this->get('required') !== 'yes') {
139
            $label->appendChild(new XMLElement('i', __('Optional')));
140
        }
141
142
        $label->appendChild(Widget::Input('fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix, (strlen($value) != 0 ? $value : null)));
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
Bug introduced by
Are you sure $this->get('element_name') of type array|mixed|null can be used in concatenation? ( Ignorable by Annotation )

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

142
        $label->appendChild(Widget::Input('fields'.$fieldnamePrefix.'['./** @scrutinizer ignore-type */ $this->get('element_name').']'.$fieldnamePostfix, (strlen($value) != 0 ? $value : null)));
Loading history...
143
144
        if ($flagWithError != null) {
145
            $wrapper->appendChild(Widget::Error($label, $flagWithError));
146
        } else {
147
            $wrapper->appendChild($label);
148
        }
149
    }
150
151
    public function checkPostFieldData($data, &$message, $entry_id = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$entry_id" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$entry_id"; expected 0 but found 1
Loading history...
152
    {
153
        $message = null;
154
155
        if (is_array($data) && isset($data['value'])) {
156
            $data = $data['value'];
157
        }
158
159
        if ($this->get('required') === 'yes' && strlen(trim($data)) == 0) {
160
            $message = __('‘%s’ is a required field.', array($this->get('label')));
161
            return self::__MISSING_FIELDS__;
162
        }
163
164
        if (!$this->__applyValidationRules($data)) {
165
            $message = __('‘%s’ contains invalid data. Please check the contents.', array($this->get('label')));
166
            return self::__INVALID_FIELDS__;
167
        }
168
169
        return self::__OK__;
170
    }
171
172
    public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$message" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$message"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$simulate" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$simulate"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$entry_id" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$entry_id"; expected 0 but found 1
Loading history...
173
    {
174
        $status = self::__OK__;
175
176
        if (strlen(trim($data)) == 0) {
177
            return array();
178
        }
179
180
        $result = array(
181
            'value' => $data
182
        );
183
184
        $result['handle'] = Lang::createHandle($result['value']);
185
186
        return $result;
187
    }
188
189
    /*-------------------------------------------------------------------------
190
        Output:
191
    -------------------------------------------------------------------------*/
192
193
    public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = false, $mode = null, $entry_id = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$encode" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$encode"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$mode" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$mode"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$entry_id" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$entry_id"; expected 0 but found 1
Loading history...
194
    {
195
        $value = $data['value'];
196
197
        if ($encode === true) {
198
            $value = General::sanitize($value);
199
        } else {
200
            if (!General::validateXML($data['value'], $errors, false, new XsltProcess)) {
201
                $value = html_entity_decode($data['value'], ENT_QUOTES, 'UTF-8');
202
                $value = $this->__replaceAmpersands($value);
203
204
                if (!General::validateXML($value, $errors, false, new XsltProcess)) {
205
                    $value = General::sanitize($data['value']);
206
                }
207
            }
208
        }
209
210
        $wrapper->appendChild(
211
            new XMLElement($this->get('element_name'), $value, array('handle' => $data['handle']))
0 ignored issues
show
Bug introduced by
It seems like $this->get('element_name') can also be of type array; however, parameter $name of XMLElement::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

211
            new XMLElement(/** @scrutinizer ignore-type */ $this->get('element_name'), $value, array('handle' => $data['handle']))
Loading history...
212
        );
213
    }
214
215
    /*-------------------------------------------------------------------------
216
        Import:
217
    -------------------------------------------------------------------------*/
218
219
    public function getImportModes()
220
    {
221
        return array(
222
            'getValue' =>       ImportableField::STRING_VALUE,
223
            'getPostdata' =>    ImportableField::ARRAY_VALUE
224
        );
225
    }
226
227
    public function prepareImportValue($data, $mode, $entry_id = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$entry_id" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$entry_id"; expected 0 but found 1
Loading history...
228
    {
229
        $message = $status = null;
230
        $modes = (object)$this->getImportModes();
231
232
        if ($mode === $modes->getValue) {
233
            return $data;
234
        } elseif ($mode === $modes->getPostdata) {
235
            return $this->processRawFieldData($data, $status, $message, true, $entry_id);
236
        }
237
238
        return null;
239
    }
240
241
    /*-------------------------------------------------------------------------
242
        Export:
243
    -------------------------------------------------------------------------*/
244
245
    /**
246
     * Return a list of supported export modes for use with `prepareExportValue`.
247
     *
248
     * @return array
249
     */
250
    public function getExportModes()
251
    {
252
        return array(
253
            'getHandle' =>      ExportableField::HANDLE,
254
            'getUnformatted' => ExportableField::UNFORMATTED,
255
            'getPostdata' =>    ExportableField::POSTDATA
256
        );
257
    }
258
259
    /**
260
     * Give the field some data and ask it to return a value using one of many
261
     * possible modes.
262
     *
263
     * @param mixed $data
264
     * @param integer $mode
265
     * @param integer $entry_id
266
     * @return string|null
267
     */
268
    public function prepareExportValue($data, $mode, $entry_id = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$entry_id" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$entry_id"; expected 0 but found 1
Loading history...
269
    {
270
        $modes = (object)$this->getExportModes();
271
272
        // Export handles:
273
        if ($mode === $modes->getHandle) {
274
            if (isset($data['handle'])) {
275
                return $data['handle'];
276
            } elseif (isset($data['value'])) {
277
                return Lang::createHandle($data['value']);
278
            }
279
280
            // Export unformatted:
281
        } elseif ($mode === $modes->getUnformatted || $mode === $modes->getPostdata) {
282
            return isset($data['value'])
283
                ? $data['value']
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement must be declared on a single line
Loading history...
284
                : null;
285
        }
286
287
        return null;
288
    }
289
290
    /*-------------------------------------------------------------------------
291
        Filtering:
292
    -------------------------------------------------------------------------*/
293
294
    public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = false)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$andOperation" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$andOperation"; expected 0 but found 1
Loading history...
295
    {
296
        $field_id = $this->get('id');
297
298
        if (self::isFilterRegex($data[0])) {
299
            $this->buildRegexSQL($data[0], array('value', 'handle'), $joins, $where);
300
        } elseif (self::isFilterSQL($data[0])) {
301
            $this->buildFilterSQL($data[0], array('value', 'handle'), $joins, $where);
302
        } elseif ($andOperation) {
303
            foreach ($data as $value) {
304
                $this->_key++;
305
                $value = $this->cleanValue($value);
306
                $joins .= "
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $field_id instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
307
                    LEFT JOIN
308
                        `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
309
                        ON (e.id = t{$field_id}_{$this->_key}.entry_id)
310
                ";
311
                $where .= "
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $field_id instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $value instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
312
                    AND (
313
                        t{$field_id}_{$this->_key}.value = '{$value}'
314
                        OR t{$field_id}_{$this->_key}.handle = '{$value}'
315
                    )
316
                ";
317
            }
318
        } else {
319
            if (!is_array($data)) {
320
                $data = array($data);
321
            }
322
323
            foreach ($data as &$value) {
324
                $value = $this->cleanValue($value);
325
            }
326
327
            $this->_key++;
328
            $data = implode("', '", $data);
329
            $joins .= "
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $field_id instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
330
                LEFT JOIN
331
                    `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
332
                    ON (e.id = t{$field_id}_{$this->_key}.entry_id)
333
            ";
334
            $where .= "
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $field_id instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $data instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
335
                AND (
336
                    t{$field_id}_{$this->_key}.value IN ('{$data}')
337
                    OR t{$field_id}_{$this->_key}.handle IN ('{$data}')
338
                )
339
            ";
340
        }
341
342
        return true;
343
    }
344
345
    /*-------------------------------------------------------------------------
346
        Sorting:
347
    -------------------------------------------------------------------------*/
348
349
    public function buildSortingSQL(&$joins, &$where, &$sort, $order = 'ASC')
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$order" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$order"; expected 0 but found 1
Loading history...
350
    {
351
        if ($this->isRandomOrder($order)) {
352
            $sort = 'ORDER BY RAND()';
353
        } else {
354
            $sort = sprintf(
355
                'ORDER BY (
356
                    SELECT %s
357
                    FROM tbl_entries_data_%d AS `ed`
358
                    WHERE entry_id = e.id
359
                ) %s, `e`.`id` %s',
360
                '`ed`.value',
361
                $this->get('id'),
0 ignored issues
show
Bug introduced by
It seems like $this->get('id') can also be of type array; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

361
                /** @scrutinizer ignore-type */ $this->get('id'),
Loading history...
362
                $order,
363
                $order
364
            );
365
        }
366
    }
367
368
    public function buildSortingSelectSQL($sort, $order = 'ASC')
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$order" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$order"; expected 0 but found 1
Loading history...
369
    {
370
        return null;
371
    }
372
373
    /*-------------------------------------------------------------------------
374
        Grouping:
375
    -------------------------------------------------------------------------*/
376
377
    public function groupRecords($records)
378
    {
379
        if (!is_array($records) || empty($records)) {
380
            return;
381
        }
382
383
        $groups = array($this->get('element_name') => array());
384
385
        foreach ($records as $r) {
386
            $data = $r->getData($this->get('id'));
387
            $value = General::sanitize($data['value']);
388
389
            if (!isset($groups[$this->get('element_name')][$data['handle']])) {
390
                $groups[$this->get('element_name')][$data['handle']] = array(
391
                    'attr' => array('handle' => $data['handle'], 'value' => $value),
392
                    'records' => array(),
393
                    'groups' => array()
394
                );
395
            }
396
397
            $groups[$this->get('element_name')][$data['handle']]['records'][] = $r;
398
        }
399
400
        return $groups;
401
    }
402
}
403