Completed
Push — master ( d4f1a2...3c703a )
by Daniel
04:18
created

DomComponentsByDanielGP::setTableCellNumeric()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 7
nc 2
nop 3
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2015 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\common_lib;
30
31
/**
32
 * DOM component functions
33
 *
34
 * @author Daniel Popiniuc
35
 */
36
trait DomComponentsByDanielGP
37
{
38
39
    use CommonBasic,
40
        DomCssAndJavascriptByDanielGP,
41
        DomDynamicSelectByDanielGP;
42
43
    private function normalizeArrayForUrl($featArray)
44
    {
45
        $outArray = [];
46
        foreach ($featArray as $key => $value) {
47
            if (is_numeric($key)) {
48
                $outArray[$value] = 1;
49
            } else {
50
                $outArray[$key] = $value;
51
            }
52
        }
53
        return $outArray;
54
    }
55
56
    /**
57
     * Builds a <select> based on a given array
58
     *
59
     * @version 20080618
60
     * @param array $aElements
61
     * @param mixed $sDefaultValue
62
     * @param string $selectName
63
     * @param array $featArray
64
     * @return string
65
     */
66
    protected function setArrayToSelect($aElements, $sDefaultValue, $selectName, $featArray = null)
67
    {
68
        if (!is_array($aElements)) {
69
            return '';
70
        }
71
        if (isset($featArray['readonly'])) {
72
            $inputFeatures = [
73
                'name'     => $selectName,
74
                'id'       => $this->buildSelectId($selectName, $featArray),
75
                'readonly' => 'readonly',
76
                'class'    => 'input_readonly',
77
                'value'    => $sDefaultValue,
78
            ];
79
            return $this->setStringIntoShortTag('input', $inputFeatures) . $aElements[$sDefaultValue];
80
        }
81
        return $this->setArrayToSelectNotReadOnly($aElements, $sDefaultValue, $selectName, $featArray);
82
    }
83
84
    /**
85
     * Converts an array to string
86
     *
87
     * @param string $sSeparator
88
     * @param array $aElements
89
     * @return string
90
     */
91
    protected function setArrayToStringForUrl($sSeparator, $aElements, $aExceptedElements = [''])
92
    {
93
        $outArray = $this->normalizeArrayForUrl($aElements);
94
        if (count($outArray) < 1) {
95
            return '';
96
        }
97
        $xptArray   = $this->normalizeArrayForUrl($aExceptedElements);
98
        $finalArray = array_diff_key($outArray, $xptArray);
99
        return http_build_query($finalArray, '', $sSeparator);
100
    }
101
102
    /**
103
     * Returns a table from an query
104
     *
105
     * @param array $aElements
106
     * @param array $ftrs
107
     * @param boolean $bKpFlPge
108
     * @return string
109
     */
110
    protected function setArrayToTable($aElements, $ftrs = null, $bKpFlPge = true)
0 ignored issues
show
Coding Style introduced by
setArrayToTable uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
setArrayToTable uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
111
    {
112
        $rows = count($aElements);
113
        if ($rows == 0) {
114
            $divTab = [
115
                'start' => '',
116
                'end'   => '',
117
            ];
118
            if (array_key_exists('showGroupingCounter', $ftrs)) {
119
                if (array_key_exists('grouping_cell_type', $ftrs) && ($ftrs['grouping_cell_type'] == 'tab')) {
120
                    $ditTitle = 'No data found';
121
                    if (isset($ftrs['showGroupingCounter'])) {
122
                        $ditTitle .= ' (0)';
123
                    }
124
                    $divTab = [
125
                        'start' => '<div class="tabbertab tabbertabdefault" id="tab_NoData" title="' . $ditTitle . '">',
126
                        'end'   => '</div><!-- from tab_NoData -->',
127
                    ];
128
                    if (!isset($ftrs['noGlobalTab'])) {
129
                        $divTab = [
130
                            'start' => '<div class="tabber" id="tab">' . $divTab['start'],
131
                            'end'   => $divTab['end'] . '</div><!-- from global Tab -->',
132
                        ];
133
                    }
134
                }
135
            }
136
            return $divTab['start']
137
                    . $this->setFeedbackModern('error', 'Error', $this->lclMsgCmn('i18n_NoData'))
138
                    . $divTab['end'];
139
        }
140
        if (isset($ftrs['limits'])) {
141
            $ftrs['limits'][1] = min($ftrs['limits'][1], $ftrs['limits'][2]);
142
            if ($ftrs['limits'][2] > $ftrs['limits'][1]) {
143
                $iStartingPageRecord = 1;
144
            }
145
        }
146
        $sReturn = '';
147
        if (isset($ftrs['hidden_columns'])) {
148
            $hdClmns = $this->setArrayValuesAsKey($ftrs['hidden_columns']);
149
        } else {
150
            $hdClmns = [''];
151
        }
152
        if ((isset($ftrs['actions']['checkbox_inlineEdit'])) || (isset($ftrs['actions']['checkbox']))) {
153
            $checkboxFormId = 'frm' . date('YmdHis');
154
            $sReturn .= '<form id="' . $checkboxFormId . '" ' . 'name="' . $checkboxFormId
155
                    . '" method="post" ' . ' action="' . $_SERVER['PHP_SELF'] . '" >';
156
        }
157
        $tbl['Def'] = '<table'
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tbl was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tbl = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
158
                . (isset($ftrs['table_style']) ? ' style="' . $ftrs['table_style'] . '"' : '')
159
                . (isset($ftrs['table_class']) ? ' class="' . $ftrs['table_class'] . '"' : '')
160
                . '>';
161
        if (!isset($ftrs['grouping_cell_type'])) {
162
            $ftrs['grouping_cell_type'] = 'row';
163
        }
164
        switch ($ftrs['grouping_cell_type']) {
165
            case 'row':
166
                $sReturn .= $tbl['Def'];
167
                break;
168
            case 'tab':
169
                if (!isset($ftrs['noGlobalTab'])) {
170
                    $sReturn .= '<div class="tabber" id="tab">';
171
                }
172
                break;
173
        }
174
        $iTableColumns    = 0;
175
        $remebered_value  = -1;
176
        $remindGroupValue = null;
177
        $color_no         = null;
178
        if (!isset($ftrs['headers_breaked'])) {
179
            $ftrs['headers_breaked'] = true;
180
        }
181
        for ($rCntr = 0; $rCntr < $rows; $rCntr++) {
182
            if ($rCntr == 0) {
183
                $header        = array_diff_key($aElements[$rCntr], $hdClmns);
184
                $iTableColumns = count($header);
185
                if (isset($ftrs['computed_columns'])) {
186
                    $iTableColumns += count($ftrs['computed_columns']);
187
                }
188
                if (isset($ftrs['actions'])) {
189
                    $iTableColumns += 1;
190
                }
191
                if (isset($ftrs['grouping_cell'])) {
192
                    $iTableColumns -= 1;
193
                }
194
                $tbl['Head'] = '<thead>';
195
                if ($ftrs['grouping_cell_type'] == 'row') {
196
                    $sReturn .= $tbl['Head'];
197
                }
198 View Code Duplication
                if (isset($iStartingPageRecord)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
                    $pgn = $this->setPagination($ftrs['limits'][0], $ftrs['limits'][1], $ftrs['limits'][2], $bKpFlPge);
0 ignored issues
show
Bug introduced by
It seems like setPagination() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
200
                    $sReturn .= $this->setStringIntoTag($this->setStringIntoTag($pgn, 'th', [
201
                                'colspan' => $iTableColumns
202
                            ]), 'tr');
203
                }
204
                $tbl['Header'] = '<tr>';
205
                if (isset($ftrs['grouping_cell'])) { // Grouping columns
206
                    $header = array_diff_key($header, [$ftrs['grouping_cell'] => '']);
207
                }
208
                if (isset($ftrs['actions'])) { // Action column
209
                    $tbl['Header'] .= '<th>&nbsp;</th>';
210
                }
211
                if (isset($ftrs['RowStyle'])) { //Exclude style columns from displaying
212
                    $tmpClmns = $this->setArrayValuesAsKey([$ftrs['RowStyle']]);
213
                    $header   = array_diff_key($header, $tmpClmns);
214
                    $hdClmns  = array_merge($hdClmns, $tmpClmns);
215
                    unset($tmpClmns);
216
                }
217
                $tbl['Header'] .= $this->setTableHeader($header, $ftrs['headers_breaked']); // Regular columns
218
                if (isset($ftrs['computed_columns'])) { // Computed columns
219
                    $tbl['Header'] .= $this->setTableHeader($ftrs['computed_columns'], $ftrs['headers_breaked']);
220
                }
221
                $tbl['Header'] .= '</tr></thead><tbody>';
222
                if ($ftrs['grouping_cell_type'] == 'row') {
223
                    $sReturn .= $tbl['Header'];
224
                }
225
            }
226
            $row_current = array_diff_key($aElements[$rCntr], $hdClmns);
227
            if (isset($ftrs['row_colored_alternated'])) {
228
                if ($ftrs['row_colored_alternated'][0] == '#') {
229
                    $color_column_value = $rCntr;
230
                } else {
231
                    $color_column_value = $row_current[$ftrs['row_colored_alternated'][0]];
232
                }
233
                if ($remebered_value != $color_column_value) {
234
                    if (isset($color_no)) {
235
                        $color_no = 1;
236
                    } else {
237
                        $color_no = 2;
238
                    }
239
                    $remebered_value = $color_column_value;
240
                }
241
                $color = ' style="background-color: ' . $ftrs['row_colored_alternated'][$color_no] . ';"';
242
            } else {
243
                if (isset($ftrs['RowStyle'])) {
244
                    $color = ' style="' . $aElements[$rCntr][$ftrs['RowStyle']] . '"';
245
                } else {
246
                    $color = '';
247
                }
248
            }
249
            $tbl['tr_Color'] = '<tr' . $color . '>';
250
// Grouping column
251
            if (isset($ftrs['grouping_cell'])) {
252
                foreach ($aElements[$rCntr] as $key => $value) {
253
                    if (($ftrs['grouping_cell'] == $key) && ($remindGroupValue != $value)) {
254
                        switch ($ftrs['grouping_cell_type']) {
255
                            case 'row':
256
                                $sReturn .= $tbl['tr_Color'] . '<td ' . 'colspan="' . $iTableColumns . '">'
257
                                        . $this->setStringIntoTag($value, 'div', ['class' => 'rowGroup rounded'])
258
                                        . '</td></tr>';
259
                                break;
260
                            case 'tab':
261
                                if (is_null($remindGroupValue)) {
262
                                    if (isset($ftrs['showGroupingCounter'])) {
263
                                        $groupCounter = 0;
264
                                    }
265
                                } else {
266
                                    $sReturn .= '</tbody></table>';
267
                                    if (isset($ftrs['showGroupingCounter'])) {
268
                                        $sReturn .= $this->updateDivTitleName($remindGroupValue, $groupCounter);
0 ignored issues
show
Bug introduced by
The variable $groupCounter does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
269
                                        $groupCounter = 0;
270
                                    }
271
                                    $sReturn .= '</div>';
272
                                }
273
                                $sReturn .= '<div class="tabbertab';
274
                                if (isset($ftrs['grouping_default_tab'])) {
275
                                    $sReturn .= ($ftrs['grouping_default_tab'] == $value ? ' tabbertabdefault' : '');
276
                                }
277
                                $sReturn .= '" id="tab_' . $this->cleanStringForId($value) . '" '
278
                                        . 'title="' . $value . '">'
279
                                        . $tbl['Def'] . $tbl['Head'] . $tbl['Header'];
280
                                break;
281
                        }
282
                        $remindGroupValue = $value;
283
                    }
284
                }
285
            }
286
            if (isset($ftrs['grouping_cell'])) {
287
                if ($ftrs['grouping_cell_type'] == 'tab') {
288
                    if (isset($ftrs['showGroupingCounter'])) {
289
                        $groupCounter++;
290
                    }
291
                }
292
            }
293
            $sReturn .= $tbl['tr_Color'];
294
// Action column
295
            if (isset($ftrs['actions'])) {
296
                $sReturn .= '<td style="white-space:nowrap;">';
297
                $action_argument = 0;
298
                if (isset($ftrs['actions']['key'])) {
299
                    $action_key = $ftrs['actions']['key'];
300
                } else {
301
                    $action_key = 'view';
302
                }
303
                if (isset($ftrs['action_prefix'])) {
304
                    $actPrfx    = $ftrs['action_prefix'] . '&amp;';
305
                    $action_key = 'view2';
306
                } else {
307
                    $actPrfx = '';
308
                }
309
                foreach ($ftrs['actions'] as $key => $value) {
310
                    if ($action_argument != 0) {
311
                        $sReturn .= '&nbsp;';
312
                    }
313
                    switch ($key) {
314
                        case 'checkbox':
315
                            $checkboxName  = $value . '[]';
316
                            $checkboxNameS = $value;
317
                            $sReturn .= '&nbsp;<input type="checkbox" name="' . $checkboxName
318
                                    . '" id="n' . $aElements[$rCntr][$value]
319
                                    . '" value="' . $aElements[$rCntr][$value] . '" ';
320
                            if (isset($_REQUEST[$checkboxNameS])) {
321
                                if (is_array($_REQUEST[$checkboxNameS])) {
322
                                    if (in_array($aElements[$rCntr][$value], $_REQUEST[$checkboxNameS])) {
323
                                        $sReturn .= 'checked="checked" ';
324
                                    }
325
                                } else {
326
                                    if ($aElements[$rCntr][$value] == $_REQUEST[$checkboxNameS]) {
327
                                        $sReturn .= 'checked="checked" ';
328
                                    }
329
                                }
330
                            }
331
                            if (strpos($_REQUEST['view'], 'multiEdit') !== false) {
332
                                $sReturn .= 'disabled="disabled" ';
333
                            }
334
                            $sReturn .= '/>';
335
                            break;
336
                        case 'checkbox_inlineEdit':
337
                            $checkboxName  = $value . '[]';
338
                            $checkboxNameS = $value;
339
                            $sReturn .= '&nbsp;<input type="checkbox" name="' . $checkboxName
340
                                    . '" id="n' . $aElements[$rCntr][$value] . '" value="'
341
                                    . $aElements[$rCntr][$value] . '"/>';
342
                            break;
343
                        case 'edit':
344
                            $edt           = '';
345
                            if (isset($ftrs['NoAjaxEditing'])) {
346
                                $edt .= $_SERVER['PHP_SELF'] . '?' . $actPrfx
347
                                        . $action_key . '=' . $value[0] . '&amp;';
348
                                $iActArgs = count($value[1]);
349
                                for ($cntr2 = 0; $cntr2 < $iActArgs; $cntr2++) {
350
                                    $edt .= $value[1][$cntr2] . '=' . $aElements[$rCntr][$value[1][$cntr2]];
351
                                }
352
                                $sReturn .= '<a href="' . $edt . '"><i class="fa fa-pencil">&nbsp;</i></a>';
353 View Code Duplication
                            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
354
                                $edt .= 'javascript:loadAE(\'' . $_SERVER['PHP_SELF'] . '?'
355
                                        . $actPrfx . $action_key . '=' . $value[0] . '&amp;';
356
                                $iActArgs = count($value[1]);
357
                                for ($cntr2 = 0; $cntr2 < $iActArgs; $cntr2++) {
358
                                    $edt .= $value[1][$cntr2] . '=' . $aElements[$rCntr][$value[1][$cntr2]];
359
                                }
360
                                $edt .= '\');';
361
                                $sReturn .= '<a href="#" onclick="' . $edt . '">'
362
                                        . '<i class="fa fa-pencil">&nbsp;</i></a>';
363
                            }
364
                            break;
365
                        case 'list2':
366
                            $edt = '';
367
                            if (isset($ftrs['NoAjaxEditing'])) {
368
                                $sReturn .= '<a href="?' . $actPrfx . $action_key . '=' . $value[0] . '&amp;';
369
                                $iActArgs = count($value[1]);
370
                                for ($cntr2 = 0; $cntr2 < $iActArgs; $cntr2++) {
371
                                    $sReturn .= $value[1][$cntr2] . '=' . $aElements[$rCntr][$value[1][$cntr2]];
372
                                }
373
                                $sReturn .= '"><i class="fa fa-list">&nbsp;</i></a>';
374 View Code Duplication
                            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
375
                                $edt .= 'javascript:loadAE(\'' . $_SERVER['PHP_SELF'] . '?'
376
                                        . $actPrfx . $action_key . '=' . $value[0] . '&amp;';
377
                                $iActArgs = count($value[1]);
378
                                for ($cntr2 = 0; $cntr2 < $iActArgs; $cntr2++) {
379
                                    $edt .= $value[1][$cntr2] . '=' . $aElements[$rCntr][$value[1][$cntr2]];
380
                                }
381
                                $edt .= '\');';
382
                                $sReturn .= '<a href="#" onclick="' . $edt . '">'
383
                                        . '<i class="fa fa-list">&nbsp;</i></a>';
384
                            }
385
                            break;
386
                        case 'delete':
387
                            $sReturn .= '<a href="javascript:setQuest(\'' . $value[0] . '\',\'';
388
                            $iActArgs = count($value[1]);
389
                            for ($cntr2 = 0; $cntr2 < $iActArgs; $cntr2++) {
390
                                $sReturn .= $value[1][$cntr2] . '=' . $aElements[$rCntr][$value[1][$cntr2]];
391
                            }
392
                            $sReturn .= '\');"><i class="fa fa-times">&nbsp;</i></a>';
393
                            break;
394
                    }
395
                    $action_argument += 1;
396
                }
397
                $sReturn .= '</td>';
398
            }
399
// Regular columns
400
            $sReturn .= $this->setTableCell($row_current, $ftrs);
401
// Computed columns
402
            if (isset($ftrs['computed_columns'])) {
403
                foreach ($ftrs['computed_columns'] as $key => $value) {
404
                    if ($value[0] == '%') {
405
                        $dec = $value[2] + 2;
406
                    } else {
407
                        $dec = $value[2];
408
                    }
409
                    switch ($value[1]) {
410
                        case '/':
411
                            // next variable is only to avoid a long line
412
                            $shorter                 = [
413
                                $aElements[$rCntr][$value[3]],
414
                                $aElements[$rCntr][$value[4]],
415
                            ];
416
                            $aElements[$rCntr][$key] = $this->setDividedResult($shorter[0], $shorter[1], $dec);
417
                            break;
418
                        case '+':
419
                            // next variable is only to avoid a long line
420
                            $iTemp                   = $this->setArrayValuesAsKey([
421
                                $value[0],
422
                                $value[1],
423
                                $value[2]
424
                            ]);
425
                            $aTemp                   = array_diff($value, $iTemp);
426
                            $aElements[$rCntr][$key] = 0;
427
                            foreach ($aTemp as $sValue) {
428
                                $aElements[$rCntr][$key] += $aElements[$rCntr][$sValue];
429
                            }
430
                            break;
431
                        default:
432
                            $row_computed[$key] = '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$row_computed was never initialized. Although not strictly required by PHP, it is generally a good practice to add $row_computed = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
433
                            break;
434
                    }
435
                    if ($value[0] == '%') {
436
                        $row_computed[$key] = ($aElements[$rCntr][$key] * 100);
0 ignored issues
show
Bug introduced by
The variable $row_computed does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
437
                        $dec -= 2;
438
                    } else {
439
                        $row_computed[$key] = $aElements[$rCntr][$key];
440
                    }
441
                    $decimals[$key] = $dec;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$decimals was never initialized. Although not strictly required by PHP, it is generally a good practice to add $decimals = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
442
                }
443
// displaying them
444
                $sReturn .= $this->setTableCell($row_computed, ['decimals' => $decimals]);
0 ignored issues
show
Bug introduced by
The variable $decimals does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
445
            }
446
            $sReturn .= '</tr>';
447
        }
448 View Code Duplication
        if (isset($iStartingPageRecord)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
449
            $pgn = $this->setPagination($ftrs['limits'][0], $ftrs['limits'][1], $ftrs['limits'][2]);
0 ignored issues
show
Bug introduced by
It seems like setPagination() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
450
            $sReturn .= '<tr>' . $this->setStringIntoTag($pgn, 'th', ['colspan' => $iTableColumns]) . '</tr>';
451
        }
452
        $sReturn .= '</tbody></table>';
453
        if ($ftrs['grouping_cell_type'] == 'tab') {
454
            if (isset($ftrs['showGroupingCounter'])) {
455
                $sReturn .= $this->updateDivTitleName($remindGroupValue, $groupCounter);
456
            }
457
            $sReturn .= '</div><!-- from ' . $remindGroupValue . ' -->';
458
            if (!isset($ftrs['noGlobalTab'])) {
459
                $sReturn .= '</div><!-- from global tab -->';
460
            }
461
        }
462
        if (isset($ftrs['actions']['checkbox'])) {
463
            if (strpos($_REQUEST['view'], 'multiEdit') === false) {
464
                $sReturn .= '<a href="#" onclick="javascript:checking(\'' . $checkboxFormId
0 ignored issues
show
Bug introduced by
The variable $checkboxFormId does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
465
                        . '\',\'' . $checkboxName . '\',true);">Check All</a>&nbsp;&nbsp;'
0 ignored issues
show
Bug introduced by
The variable $checkboxName does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
466
                        . '<a href="#" onclick="javascript:checking(\'' . $checkboxFormId
467
                        . '\',\'' . $checkboxName . '\',false);">Uncheck All</a>&nbsp;&nbsp;'
468
                        . '<input type="hidden" name="action" value="multiEdit_' . $checkboxNameS . '" />';
0 ignored issues
show
Bug introduced by
The variable $checkboxNameS does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
469 View Code Duplication
                if (isset($ftrs['hiddenInput'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
470
                    if (is_array($ftrs['hiddenInput'])) {
471
                        foreach ($ftrs['hiddenInput'] as $valueF) {
472
                            $sReturn .= '<input type="hidden" name="' . $valueF
473
                                    . '" value="' . $_REQUEST[$valueF] . '" />';
474
                        }
475
                    } else {
476
                        $sReturn .= '<input type="hidden" name="' . $ftrs['hiddenInput']
477
                                . '" value="' . $_REQUEST[$ftrs['hiddenInput']] . '" />';
478
                    }
479
                }
480
                $sReturn .= '<input style="margin: 0 3em 0 3em;" type="submit" ' . 'value="Edit selected" />';
481
            }
482
            $sReturn .= '</form>';
483
        }
484
        if (isset($ftrs['actions']['checkbox_inlineEdit'])) {
485
            $sReturn .= '<a href="#" onclick="javascript:checking(\'' . $checkboxFormId
486
                    . '\',\'' . $checkboxName . '\',true);">Check All</a>&nbsp;&nbsp;'
487
                    . '<a href="#" onclick="javascript:checking(\'' . $checkboxFormId
488
                    . '\',\'' . $checkboxName . '\',false);">Uncheck All</a>&nbsp;&nbsp;';
489
            if (isset($ftrs['visibleInput'])) {
490
                $sReturn .= $ftrs['visibleInput'];
491
            }
492
            $sReturn .= '<input type="hidden" name="view" value="save_' . $checkboxNameS . '" />';
493 View Code Duplication
            if (isset($ftrs['hiddenInput'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
494
                if (is_array($ftrs['hiddenInput'])) {
495
                    foreach ($ftrs['hiddenInput'] as $valueF) {
496
                        $sReturn .= '<input type="hidden" name="' . $valueF
497
                                . '" value="' . $_REQUEST[$valueF] . '" />';
498
                    }
499
                } else {
500
                    $sReturn .= '<input type="hidden" name="' . $ftrs['hiddenInput']
501
                            . '" value="' . $_REQUEST[$ftrs['hiddenInput']] . '" />';
502
                }
503
            }
504
            $sReturn .= '<input style="margin: 0 3em 0 3em;" type="submit" value="Store the modification" />';
505
            $sReturn .= '</form>';
506
        }
507
        return $sReturn;
508
    }
509
510
    /**
511
     * Set a control to a user-friendly calendar
512
     *
513
     * @param string $controlName
514
     * @param string $additionalStyle
515
     * @return string
516
     */
517 View Code Duplication
    public function setCalendarControl($controlName, $additionalStyle = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
518
    {
519
        return $this->setStringIntoTag('&nbsp;', 'span', [
520
                    'onclick' => implode('', [
521
                        'javascript:NewCssCal(\'' . $controlName,
522
                        '\',\'yyyyMMdd\',\'dropdown\',false,\'24\',false);',
523
                    ]),
524
                    'class'   => 'fa fa-calendar',
525
                    'id'      => $controlName . '_picker',
526
                    'style'   => 'cursor:pointer;' . $additionalStyle,
527
        ]);
528
    }
529
530
    /**
531
     * Set a control to a user-friendly calendar with time included
532
     *
533
     * @param string $controlName
534
     * @param string $additionalStyle
535
     * @return string
536
     */
537 View Code Duplication
    public function setCalendarControlWithTime($controlName, $additionalStyle = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
538
    {
539
        return $this->setStringIntoTag('&nbsp;', 'span', [
540
                    'onclick' => implode('', [
541
                        'javascript:NewCssCal(\'' . $controlName,
542
                        '\',\'yyyyMMdd\',\'dropdown\',true,\'24\',true);',
543
                    ]),
544
                    'class'   => 'fa fa-calendar',
545
                    'id'      => $controlName . '_picker',
546
                    'style'   => 'cursor:pointer;' . $additionalStyle,
547
        ]);
548
    }
549
550
    /**
551
     * Outputs an HTML footer
552
     *
553
     * @param array $footerInjected
554
     * @return string
555
     */
556
    protected function setFooterCommon($footerInjected = null)
0 ignored issues
show
Coding Style introduced by
setFooterCommon uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
557
    {
558
        if (isset($_REQUEST['specialHook']) && (in_array('noFooter', $_REQUEST['specialHook']))) {
559
            return '';
560
        }
561
        return $this->setFooterCommonInjected($footerInjected) . '</body></html>';
562
    }
563
564
    protected function setFooterCommonInjected($footerInjected = null)
565
    {
566
        $sReturn = '';
567
        if (!is_null($footerInjected)) {
568
            $sReturn = $footerInjected;
569
            if (is_array($footerInjected)) {
570
                $sReturn = implode('', $footerInjected);
571
            }
572
        }
573
        return $sReturn;
574
    }
575
576
    /**
577
     * Outputs an HTML header
578
     *
579
     * @param array $headerFeatures
580
     * @return string
581
     */
582
    protected function setHeaderCommon($headerFeatures = null)
0 ignored issues
show
Coding Style introduced by
setHeaderCommon uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
583
    {
584
        $sReturn = [];
585
        if (isset($_REQUEST['specialHook']) && (in_array('noHeader', $_REQUEST['specialHook']))) {
586
            $sReturn[] = ''; // no Header
587
        } else {
588
            $fixedHeaderElements = [
589
                'start'    => '<!DOCTYPE html>',
590
                'lang'     => '<html lang="en-US">',
591
                'head'     => '<head>',
592
                'charset'  => '<meta charset="utf-8" />',
593
                'viewport' => '<meta name="viewport" content="' . implode(', ', [
594
                    'width=device-width',
595
                    'height=device-height',
596
                    'initial-scale=1',
597
                ]) . '" />',
598
            ];
599
            if (!is_null($headerFeatures)) {
600
                if (is_array($headerFeatures)) {
601
                    $aFeatures = [];
602
                    foreach ($headerFeatures as $key => $value) {
603
                        switch ($key) {
604 View Code Duplication
                            case 'css':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
605
                                if (is_array($value)) {
606
                                    foreach ($value as $value2) {
607
                                        $aFeatures[] = $this->setCssFile(filter_var($value2, FILTER_SANITIZE_URL));
608
                                    }
609
                                } else {
610
                                    $aFeatures[] = $this->setCssFile(filter_var($value, FILTER_SANITIZE_URL));
611
                                }
612
                                break;
613 View Code Duplication
                            case 'javascript':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
614
                                if (is_array($value)) {
615
                                    foreach ($value as $value2) {
616
                                        $vl          = filter_var($value2, FILTER_SANITIZE_URL);
617
                                        $aFeatures[] = $this->setJavascriptFile($vl);
618
                                    }
619
                                } else {
620
                                    $aFeatures[] = $this->setJavascriptFile(filter_var($value, FILTER_SANITIZE_URL));
621
                                }
622
                                break;
623
                            case 'lang':
624
                                $fixedHeaderElements['lang'] = '<html lang="'
625
                                        . filter_var($value, FILTER_SANITIZE_STRING) . '">';
626
                                break;
627
                            case 'title':
628
                                $aFeatures[]                 = '<title>'
629
                                        . filter_var($value, FILTER_SANITIZE_STRING) . '</title>';
630
                                break;
631
                        }
632
                    }
633
                    $sReturn[] = implode('', $fixedHeaderElements)
634
                            . implode('', $aFeatures)
635
                            . '</head>'
636
                            . '<body>';
637
                } else {
638
                    $sReturn[] = implode('', $fixedHeaderElements)
639
                            . '</head>'
640
                            . '<body>'
641
                            . '<p style="background-color:red;color:#FFF;">The parameter sent to '
642
                            . __FUNCTION__ . ' must be an array</p>'
643
                            . $this->setFooterCommon();
644
                    throw new \Exception($sReturn);
645
                }
646
            }
647
        }
648
        return implode('', $sReturn);
649
    }
650
651
    /**
652
     * Generates a table cell
653
     *
654
     * @param array $aElements
655
     * @param array $features
656
     * @return string
657
     */
658
    private function setTableCell($aElements, $features = null)
659
    {
660
        $sReturn = null;
661
        foreach ($aElements as $key => $value) {
662
            $value = str_replace(['& ', '\"', "\'"], ['&amp; ', '"', "'"], $value);
663
            if ((isset($features['grouping_cell'])) && ($features['grouping_cell'] == $key)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
664
                // just skip
665
            } else {
666
                $sReturn .= '<td ';
667
                if (isset($features['column_formatting'][$key])) {
668
                    switch ($features['column_formatting'][$key]) {
669
                        case '@':
670
                            $sReturn .= 'style="text-align:left;">' . $value;
671
                            break;
672
                        case 'right':
673
                            $sReturn .= 'style="text-align:right;">' . $value;
674
                            break;
675
                        default:
676
                            $sReturn .= '???';
677
                            break;
678
                    }
679
                } else {
680
                    if (is_numeric($value)) {
681
                        $sReturn .= $this->setTableCellNumeric($key, $value, $features);
682
                    } else {
683
                        $outputet = false;
684
                        if ((strpos($value, '-') !== false) && (strlen($value) == 10)) {
685
                            if (preg_match("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $value, $regs)) {
686
                                $outputet = true;
687
                                $sReturn .= 'style="text-align:right;width: 10px;">'
688
                                        . $regs[3] . '.' . $regs[2] . '.' . $regs[1];
689
                            }
690
                        }
691
                        if (!$outputet) {
692
                            $sReturn .= 'style="text-align:left;">' . $value;
693
                        }
694
                    }
695
                }
696
                $sReturn .= '</td>';
697
            }
698
        }
699
        return $sReturn;
700
    }
701
702
    private function setTableCellDecimals($key, $features)
703
    {
704
        $decimals = 0;
705
        if (isset($features['no_of_decimals'])) {
706
            $decimals = $features['no_of_decimals'];
707
        }
708
        if (isset($features['decimals']) && array_key_exists($key, $features['decimals'])) {
709
            $decimals = $features['decimals'][$key];
710
        }
711
        return $decimals;
712
    }
713
714
    private function setTableCellNumeric($key, $value, $features)
715
    {
716
        $styleToReturn = 'style="text-align: right;">';
717
        if (substr($value, 0, 1) === '0') {
718
            return $styleToReturn . $value;
719
        }
720
        $decimals = $this->setTableCellDecimals($key, $features);
721
        $nDc      = ['MinFractionDigits' => $decimals, 'MaxFractionDigits' => $decimals];
722
        return $styleToReturn . $this->setNumberFormat($value, $nDc);
723
    }
724
725
    /**
726
     * Generates a table header
727
     *
728
     * @param array $aElements
729
     * @param boolean $bHeadersBreaked
730
     * @return string
731
     */
732
    private function setTableHeader($aElements, $bHeadersBreaked)
733
    {
734
        $aTableHeader = $aElements;
735
        if ($bHeadersBreaked) {
736
            $aTableHeader = $this->setArrayToArrayKbr($aElements);
737
        }
738
        $sReturn[] = null;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$sReturn was never initialized. Although not strictly required by PHP, it is generally a good practice to add $sReturn = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
739
        foreach (array_keys($aTableHeader) as $value) {
740
            $sReturn[] = $this->setStringIntoTag($value, 'th');
741
        }
742
        return implode('', $sReturn);
743
    }
744
745
    /**
746
     * Create an upper right box with choices for languages
747
     * (requires flag-icon.min.css to be loaded)
748
     * (makes usage of custom class "upperRightBox" and id = "visibleOnHover", provided here as scss file)
749
     *
750
     * @param array $aAvailableLanguages
751
     * @return string
752
     */
753
    protected function setUpperRightBoxLanguages($aAvailableLanguages)
754
    {
755
        $this->handleLanguageIntoSession();
756
        return '<div class="upperRightBox">'
757
                . '<div style="text-align:right;">'
758
                . '<span class="flag-icon flag-icon-' . strtolower(substr($this->tCmnSession->get('lang'), -2))
759
                . '" style="margin-right:2px;">&nbsp;</span>'
760
                . $aAvailableLanguages[$this->tCmnSession->get('lang')]
761
                . '</div><!-- default Language -->'
762
                . $this->setUpperRightVisibleOnHoverLanguages($aAvailableLanguages)
763
                . '</div><!-- upperRightBox end -->';
764
    }
765
766
    private function setUpperRightVisibleOnHoverLanguages($aAvailableLanguages)
767
    {
768
        $linkWithoutLanguage = '';
769
        $alR                 = $this->tCmnSuperGlobals->request->all();
770
        if (count($alR) > 0) {
771
            $linkWithoutLanguage = $this->setArrayToStringForUrl('&amp;', $alR, ['lang']) . '&amp;';
772
        }
773
        $sReturn = [];
774
        foreach ($aAvailableLanguages as $key => $value) {
775
            if ($this->tCmnSession->get('lang') !== $key) {
776
                $sReturn[] = '<a href="?' . $linkWithoutLanguage . 'lang=' . $key . '" style="display:block;">'
777
                        . '<span class="flag-icon flag-icon-' . strtolower(substr($key, -2))
778
                        . '" style="margin-right:2px;">&nbsp;</span>' . $value . '</a>';
779
            }
780
        }
781
        return '<div id="visibleOnHover">' . implode('', $sReturn) . '</div><!-- visibleOnHover end -->';
782
    }
783
}
784