Completed
Push — master ( a4e869...64e2a5 )
by Daniel
03:26
created

setJavascriptAddEditByAjax()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
rs 9.4286
cc 1
eloc 11
nc 1
nop 1
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 \danielgp\browser_agent_info\BrowserAgentInfosByDanielGP;
40
41
    /**
42
     * Calculate the optimal for all options within a select tag
43
     *
44
     * @param array $aElements
45
     * @param array $aFeatures
46
     * @return string|int
47
     */
48
    private function calculateSelectOptionsSize($aElements, $aFeatures = [])
49
    {
50
        if (is_null($aFeatures)) {
51
            $aFeatures = [];
52
        }
53
        if (is_array($aElements)) {
54
            if (isset($aFeatures['size'])) {
55
                if ($aFeatures['size'] == 0) {
56
                    $selectSize = count($aElements);
57
                } else {
58
                    $selectSize = min(count($aElements), $aFeatures['size']);
59
                }
60
            } else {
61
                $selectSize = 1;
62
            }
63
            if ((in_array('include_null', $aFeatures)) && ($selectSize != '1')) {
64
                $selectSize++;
65
            }
66
            return $selectSize;
67
        } else {
68
            return '';
69
        }
70
    }
71
72
    protected function cleanStringForId($givenString)
73
    {
74
        return preg_replace("/[^a-zA-Z0-9]/", '', ucwords($givenString));
75
    }
76
77
    /**
78
     * Builds a <select> based on a given array
79
     *
80
     * @version 20080618
81
     * @param array $aElements
82
     * @param string/array $sDefaultValue
0 ignored issues
show
Documentation introduced by
The doc-type string/array could not be parsed: Unknown type name "string/array" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
83
     * @param string $select_name
84
     * @param array $features_array
85
     * @return string
86
     */
87
    protected function setArrayToSelect($aElements, $sDefaultValue, $select_name, $features_array = null)
88
    {
89
        if (!is_array($aElements)) {
90
            return '';
91
        }
92
        $select_id = '" id="' . str_replace(['[', ']'], ['', ''], $select_name)
93
                . (isset($features_array['id_no']) ? $features_array['id_no'] : '');
94
        if (isset($features_array['readonly'])) {
95
            return $this->setStringIntoShortTag('input', [
96
                        'name'     => $select_name,
97
                        'id'       => $select_id,
98
                        'readonly' => 'readonly',
99
                        'class'    => 'input_readonly',
100
                        'value'    => $sDefaultValue,
101
                    ]) . $aElements[$sDefaultValue];
102
        }
103
        if (isset($features_array['id_no'])) {
104
            unset($features_array['id_no']);
105
        }
106
        $string2return = '<select name="' . $select_name . $select_id
107
                . '" size="' . $this->calculateSelectOptionsSize($aElements, $features_array) . '"';
108
        if (is_array($features_array)) {
109
            if (isset($features_array['additional_javascript_action'])) {
110
                $temporary_string = $features_array['additional_javascript_action'];
111
            } else {
112
                $temporary_string = '';
113
            }
114
            if (in_array('autosubmit', $features_array)) {
115
                $string2return .= ' onchange="javascript:' . $temporary_string . 'submit();"';
116
            } else {
117
                if ($temporary_string != '') {
118
                    $string2return .= ' onchange="javascript:' . $temporary_string . '"';
119
                }
120
            }
121
            if (in_array('disabled', $features_array)) {
122
                $string2return .= ' disabled="disabled"';
123
            }
124
            if (in_array('hidden', $features_array)) {
125
                $string2return .= ' style="visibility: hidden;"';
126
            }
127
            if (in_array('multiselect', $features_array)) {
128
                $string2return .= ' multiple="multiple"';
129
            }
130
            if (array_key_exists('style', $features_array)) {
131
                $string2return .= ' style="' . $features_array['style'] . '"';
132
            }
133
        }
134
        $string2return .= '>'
135
                . $this->setOptionsForSelect($aElements, $sDefaultValue, $features_array)
136
                . '</select>';
137
        return $string2return;
138
    }
139
140
    /**
141
     * Converts an array to string
142
     *
143
     * @param string $sSeparator
144
     * @param array $aElements
145
     * @return string
146
     */
147
    protected function setArrayToStringForUrl($sSeparator, $aElements, $aExceptedElements = [''])
148
    {
149
        if (is_array($aElements)) {
150
            if (count($aElements) == 0) {
151
                $sReturn = [''];
152
            } else {
153
                $sReturn = [];
154
                foreach ($aElements as $key => $value) {
155
                    if (!in_array($key, $aExceptedElements)) {
156
                        if (is_array($aElements[$key])) {
157
                            $aCounter = count($aElements[$key]);
158
                            for ($counter2 = 0; $counter2 < $aCounter; $counter2++) {
159
                                if ($value[$counter2] !== '') {
160
                                    $sReturn[] = $key . '[]=' . $value[$counter2];
161
                                }
162
                            }
163
                        } else {
164
                            if ($value !== '') {
165
                                $sReturn[] = $key . '=' . $value;
166
                            }
167
                        }
168
                    }
169
                }
170
            }
171
        } else {
172
            $sReturn = [''];
173
        }
174
        return implode($sSeparator, $sReturn);
175
    }
176
177
    /**
178
     * Returns a table from an query
179
     *
180
     * @param array $gArray
0 ignored issues
show
Bug introduced by
There is no parameter named $gArray. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
181
     * @param array $features
0 ignored issues
show
Bug introduced by
There is no parameter named $features. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
182
     * @param boolean $bKeepFullPage
0 ignored issues
show
Bug introduced by
There is no parameter named $bKeepFullPage. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
183
     * @return string
184
     */
185
    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...
186
    {
187
        $rows = count($aElements);
188
        if ($rows == 0) {
189
            $divTab = [
190
                'start' => '',
191
                'end'   => '',
192
            ];
193
            if (array_key_exists('showGroupingCounter', $ftrs)) {
194
                if (array_key_exists('grouping_cell_type', $ftrs) && ($ftrs['grouping_cell_type'] == 'tab')) {
195
                    $ditTitle = 'No data found';
196
                    if (isset($ftrs['showGroupingCounter'])) {
197
                        $ditTitle .= ' (0)';
198
                    }
199
                    $divTab = [
200
                        'start' => '<div class="tabbertab tabbertabdefault" id="tab_NoData" title="' . $ditTitle . '">',
201
                        'end'   => '</div><!-- from tab_NoData -->',
202
                    ];
203
                    if (!isset($ftrs['noGlobalTab'])) {
204
                        $divTab = [
205
                            'start' => '<div class="tabber" id="tab">' . $divTab['start'],
206
                            'end'   => $divTab['end'] . '</div><!-- from global Tab -->',
207
                        ];
208
                    }
209
                }
210
            }
211
            return $divTab['start']
212
                    . $this->setFeedbackModern('error', 'Error', $this->lclMsgCmn('i18n_NoData'))
0 ignored issues
show
Bug introduced by
It seems like lclMsgCmn() 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...
213
                    . $divTab['end'];
214
        }
215
        if (isset($ftrs['limits'])) {
216
            $ftrs['limits'][1] = min($ftrs['limits'][1], $ftrs['limits'][2]);
217
            if ($ftrs['limits'][2] > $ftrs['limits'][1]) {
218
                $iStartingPageRecord = 1;
219
            }
220
        }
221
        $sReturn = '';
222
        if (isset($ftrs['hidden_columns'])) {
223
            $hdClmns = $this->setArrayValuesAsKey($ftrs['hidden_columns']);
0 ignored issues
show
Bug introduced by
It seems like setArrayValuesAsKey() 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...
224
        } else {
225
            $hdClmns = [''];
226
        }
227
        if ((isset($ftrs['actions']['checkbox_inlineEdit'])) || (isset($ftrs['actions']['checkbox']))) {
228
            $checkboxFormId = 'frm' . date('YmdHis');
229
            $sReturn .= '<form id="' . $checkboxFormId . '" ' . 'name="' . $checkboxFormId
230
                    . '" method="post" ' . ' action="' . $_SERVER['PHP_SELF'] . '" >';
231
        }
232
        $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...
233
                . (isset($ftrs['table_style']) ? ' style="' . $ftrs['table_style'] . '"' : '')
234
                . (isset($ftrs['table_class']) ? ' class="' . $ftrs['table_class'] . '"' : '')
235
                . '>';
236
        if (!isset($ftrs['grouping_cell_type'])) {
237
            $ftrs['grouping_cell_type'] = 'row';
238
        }
239
        switch ($ftrs['grouping_cell_type']) {
240
            case 'row':
241
                $sReturn .= $tbl['Def'];
242
                break;
243
            case 'tab':
244
                if (!isset($ftrs['noGlobalTab'])) {
245
                    $sReturn .= '<div class="tabber" id="tab">';
246
                }
247
                break;
248
        }
249
        $iTableColumns    = 0;
250
        $remebered_value  = -1;
251
        $remindGroupValue = null;
252
        $color_no         = null;
253
        if (!isset($ftrs['headers_breaked'])) {
254
            $ftrs['headers_breaked'] = true;
255
        }
256
        for ($rCntr = 0; $rCntr < $rows; $rCntr++) {
257
            if ($rCntr == 0) {
258
                $header        = array_diff_key($aElements[$rCntr], $hdClmns);
259
                $iTableColumns = count($header);
260
                if (isset($ftrs['computed_columns'])) {
261
                    $iTableColumns += count($ftrs['computed_columns']);
262
                }
263
                if (isset($ftrs['actions'])) {
264
                    $iTableColumns += 1;
265
                }
266
                if (isset($ftrs['grouping_cell'])) {
267
                    $iTableColumns -= 1;
268
                }
269
                $tbl['Head'] = '<thead>';
270
                if ($ftrs['grouping_cell_type'] == 'row') {
271
                    $sReturn .= $tbl['Head'];
272
                }
273 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...
274
                    $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...
275
                    $sReturn .= $this->setStringIntoTag($this->setStringIntoTag($pgn, 'th', [
276
                                'colspan' => $iTableColumns
277
                            ]), 'tr');
278
                }
279
                $tbl['Header'] = '<tr>';
280
                if (isset($ftrs['grouping_cell'])) { // Grouping columns
281
                    $header = array_diff_key($header, [$ftrs['grouping_cell'] => '']);
282
                }
283
                if (isset($ftrs['actions'])) { // Action column
284
                    $tbl['Header'] .= '<th>&nbsp;</th>';
285
                }
286
                if (isset($ftrs['RowStyle'])) { //Exclude style columns from displaying
287
                    $tmpClmns = $this->setArrayValuesAsKey([$ftrs['RowStyle']]);
0 ignored issues
show
Bug introduced by
It seems like setArrayValuesAsKey() 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...
288
                    $header   = array_diff_key($header, $tmpClmns);
289
                    $hdClmns  = array_merge($hdClmns, $tmpClmns);
290
                    unset($tmpClmns);
291
                }
292
                $tbl['Header'] .= $this->setTableHeader($header, $ftrs['headers_breaked']); // Regular columns
293
                if (isset($ftrs['computed_columns'])) { // Computed columns
294
                    $tbl['Header'] .= $this->setTableHeader($ftrs['computed_columns'], $ftrs['headers_breaked']);
295
                }
296
                $tbl['Header'] .= '</tr></thead><tbody>';
297
                if ($ftrs['grouping_cell_type'] == 'row') {
298
                    $sReturn .= $tbl['Header'];
299
                }
300
            }
301
            $row_current = array_diff_key($aElements[$rCntr], $hdClmns);
302
            if (isset($ftrs['row_colored_alternated'])) {
303
                if ($ftrs['row_colored_alternated'][0] == '#') {
304
                    $color_column_value = $rCntr;
305
                } else {
306
                    $color_column_value = $row_current[$ftrs['row_colored_alternated'][0]];
307
                }
308
                if ($remebered_value != $color_column_value) {
309
                    if (isset($color_no)) {
310
                        $color_no = 1;
311
                    } else {
312
                        $color_no = 2;
313
                    }
314
                    $remebered_value = $color_column_value;
315
                }
316
                $color = ' style="background-color: ' . $ftrs['row_colored_alternated'][$color_no] . ';"';
317
            } else {
318
                if (isset($ftrs['RowStyle'])) {
319
                    $color = ' style="' . $aElements[$rCntr][$ftrs['RowStyle']] . '"';
320
                } else {
321
                    $color = '';
322
                }
323
            }
324
            $tbl['tr_Color'] = '<tr' . $color . '>';
325
// Grouping column
326
            if (isset($ftrs['grouping_cell'])) {
327
                foreach ($aElements[$rCntr] as $key => $value) {
328
                    if (($ftrs['grouping_cell'] == $key) && ($remindGroupValue != $value)) {
329
                        switch ($ftrs['grouping_cell_type']) {
330
                            case 'row':
331
                                $sReturn .= $tbl['tr_Color'] . '<td ' . 'colspan="' . $iTableColumns . '">'
332
                                        . $this->setStringIntoTag($value, 'div', ['class' => 'rowGroup rounded'])
333
                                        . '</td></tr>';
334
                                break;
335
                            case 'tab':
336
                                if (is_null($remindGroupValue)) {
337
                                    if (isset($ftrs['showGroupingCounter'])) {
338
                                        $groupCounter = 0;
339
                                    }
340
                                } else {
341
                                    $sReturn .= '</tbody></table>';
342
                                    if (isset($ftrs['showGroupingCounter'])) {
343
                                        $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...
344
                                        $groupCounter = 0;
345
                                    }
346
                                    $sReturn .= '</div>';
347
                                }
348
                                $sReturn .= '<div class="tabbertab';
349
                                if (isset($ftrs['grouping_default_tab'])) {
350
                                    $sReturn .= ($ftrs['grouping_default_tab'] == $value ? ' tabbertabdefault' : '');
351
                                }
352
                                $sReturn .= '" id="tab_' . $this->cleanStringForId($value) . '" '
353
                                        . 'title="' . $value . '">'
354
                                        . $tbl['Def'] . $tbl['Head'] . $tbl['Header'];
355
                                break;
356
                        }
357
                        $remindGroupValue = $value;
358
                    }
359
                }
360
            }
361
            if (isset($ftrs['grouping_cell'])) {
362
                if ($ftrs['grouping_cell_type'] == 'tab') {
363
                    if (isset($ftrs['showGroupingCounter'])) {
364
                        $groupCounter++;
365
                    }
366
                }
367
            }
368
            $sReturn .= $tbl['tr_Color'];
369
// Action column
370
            if (isset($ftrs['actions'])) {
371
                $sReturn .= '<td style="white-space:nowrap;">';
372
                $action_argument = 0;
373
                if (isset($ftrs['actions']['key'])) {
374
                    $action_key = $ftrs['actions']['key'];
375
                } else {
376
                    $action_key = 'view';
377
                }
378
                if (isset($ftrs['action_prefix'])) {
379
                    $actPrfx    = $ftrs['action_prefix'] . '&amp;';
380
                    $action_key = 'view2';
381
                } else {
382
                    $actPrfx = '';
383
                }
384
                foreach ($ftrs['actions'] as $key => $value) {
385
                    if ($action_argument != 0) {
386
                        $sReturn .= '&nbsp;';
387
                    }
388
                    switch ($key) {
389
                        case 'checkbox':
390
                            $checkboxName  = $value . '[]';
391
                            $checkboxNameS = $value;
392
                            $sReturn .= '&nbsp;<input type="checkbox" name="' . $checkboxName
393
                                    . '" id="n' . $aElements[$rCntr][$value]
394
                                    . '" value="' . $aElements[$rCntr][$value] . '" ';
395
                            if (isset($_REQUEST[$checkboxNameS])) {
396
                                if (is_array($_REQUEST[$checkboxNameS])) {
397
                                    if (in_array($aElements[$rCntr][$value], $_REQUEST[$checkboxNameS])) {
398
                                        $sReturn .= 'checked="checked" ';
399
                                    }
400
                                } else {
401
                                    if ($aElements[$rCntr][$value] == $_REQUEST[$checkboxNameS]) {
402
                                        $sReturn .= 'checked="checked" ';
403
                                    }
404
                                }
405
                            }
406
                            if (strpos($_REQUEST['view'], 'multiEdit') !== false) {
407
                                $sReturn .= 'disabled="disabled" ';
408
                            }
409
                            $sReturn .= '/>';
410
                            break;
411
                        case 'checkbox_inlineEdit':
412
                            $checkboxName  = $value . '[]';
413
                            $checkboxNameS = $value;
414
                            $sReturn .= '&nbsp;<input type="checkbox" name="' . $checkboxName
415
                                    . '" id="n' . $aElements[$rCntr][$value] . '" value="'
416
                                    . $aElements[$rCntr][$value] . '"/>';
417
                            break;
418
                        case 'edit':
419
                            $edt           = '';
420
                            if (isset($ftrs['NoAjaxEditing'])) {
421
                                $edt .= $_SERVER['PHP_SELF'] . '?' . $actPrfx
422
                                        . $action_key . '=' . $value[0] . '&amp;';
423
                                $iActArgs = count($value[1]);
424
                                for ($cntr2 = 0; $cntr2 < $iActArgs; $cntr2++) {
425
                                    $edt .= $value[1][$cntr2] . '=' . $aElements[$rCntr][$value[1][$cntr2]];
426
                                }
427
                                $sReturn .= '<a href="' . $edt . '"><i class="fa fa-pencil">&nbsp;</i></a>';
428 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...
429
                                $edt .= 'javascript:loadAE(\'' . $_SERVER['PHP_SELF'] . '?'
430
                                        . $actPrfx . $action_key . '=' . $value[0] . '&amp;';
431
                                $iActArgs = count($value[1]);
432
                                for ($cntr2 = 0; $cntr2 < $iActArgs; $cntr2++) {
433
                                    $edt .= $value[1][$cntr2] . '=' . $aElements[$rCntr][$value[1][$cntr2]];
434
                                }
435
                                $edt .= '\');';
436
                                $sReturn .= '<a href="#" onclick="' . $edt . '">'
437
                                        . '<i class="fa fa-pencil">&nbsp;</i></a>';
438
                            }
439
                            break;
440
                        case 'list2':
441
                            $edt = '';
442
                            if (isset($ftrs['NoAjaxEditing'])) {
443
                                $sReturn .= '<a href="?' . $actPrfx . $action_key . '=' . $value[0] . '&amp;';
444
                                $iActArgs = count($value[1]);
445
                                for ($cntr2 = 0; $cntr2 < $iActArgs; $cntr2++) {
446
                                    $sReturn .= $value[1][$cntr2] . '=' . $aElements[$rCntr][$value[1][$cntr2]];
447
                                }
448
                                $sReturn .= '"><i class="fa fa-list">&nbsp;</i></a>';
449 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...
450
                                $edt .= 'javascript:loadAE(\'' . $_SERVER['PHP_SELF'] . '?'
451
                                        . $actPrfx . $action_key . '=' . $value[0] . '&amp;';
452
                                $iActArgs = count($value[1]);
453
                                for ($cntr2 = 0; $cntr2 < $iActArgs; $cntr2++) {
454
                                    $edt .= $value[1][$cntr2] . '=' . $aElements[$rCntr][$value[1][$cntr2]];
455
                                }
456
                                $edt .= '\');';
457
                                $sReturn .= '<a href="#" onclick="' . $edt . '">'
458
                                        . '<i class="fa fa-list">&nbsp;</i></a>';
459
                            }
460
                            break;
461
                        case 'delete':
462
                            $sReturn .= '<a href="javascript:setQuest(\'' . $value[0] . '\',\'';
463
                            $iActArgs = count($value[1]);
464
                            for ($cntr2 = 0; $cntr2 < $iActArgs; $cntr2++) {
465
                                $sReturn .= $value[1][$cntr2] . '=' . $aElements[$rCntr][$value[1][$cntr2]];
466
                            }
467
                            $sReturn .= '\');"><i class="fa fa-times">&nbsp;</i></a>';
468
                            break;
469
                    }
470
                    $action_argument += 1;
471
                }
472
                $sReturn .= '</td>';
473
            }
474
// Regular columns
475
            $sReturn .= $this->setTableCell($row_current, $ftrs);
476
// Computed columns
477
            if (isset($ftrs['computed_columns'])) {
478
                foreach ($ftrs['computed_columns'] as $key => $value) {
479
                    if ($value[0] == '%') {
480
                        $dec = $value[2] + 2;
481
                    } else {
482
                        $dec = $value[2];
483
                    }
484
                    switch ($value[1]) {
485
                        case '/':
486
                            // next variable is only to avoid a long line
487
                            $shorter                 = [
488
                                $aElements[$rCntr][$value[3]],
489
                                $aElements[$rCntr][$value[4]],
490
                            ];
491
                            $aElements[$rCntr][$key] = $this->setDividedResult($shorter[0], $shorter[1], $dec);
0 ignored issues
show
Bug introduced by
It seems like setDividedResult() 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...
492
                            break;
493
                        case '+':
494
                            // next variable is only to avoid a long line
495
                            $iTemp                   = $this->setArrayValuesAsKey([
0 ignored issues
show
Bug introduced by
It seems like setArrayValuesAsKey() 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...
496
                                $value[0],
497
                                $value[1],
498
                                $value[2]
499
                            ]);
500
                            $aTemp                   = array_diff($value, $iTemp);
501
                            $aElements[$rCntr][$key] = 0;
502
                            foreach ($aTemp as $sValue) {
503
                                $aElements[$rCntr][$key] += $aElements[$rCntr][$sValue];
504
                            }
505
                            break;
506
                        default:
507
                            $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...
508
                            break;
509
                    }
510
                    if ($value[0] == '%') {
511
                        $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...
512
                        $dec -= 2;
513
                    } else {
514
                        $row_computed[$key] = $aElements[$rCntr][$key];
515
                    }
516
                    $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...
517
                }
518
// displaying them
519
                $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...
520
            }
521
            $sReturn .= '</tr>';
522
        }
523 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...
524
            $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...
525
            $sReturn .= '<tr>' . $this->setStringIntoTag($pgn, 'th', ['colspan' => $iTableColumns]) . '</tr>';
526
        }
527
        $sReturn .= '</tbody></table>';
528
        if ($ftrs['grouping_cell_type'] == 'tab') {
529
            if (isset($ftrs['showGroupingCounter'])) {
530
                $sReturn .= $this->updateDivTitleName($remindGroupValue, $groupCounter);
531
            }
532
            $sReturn .= '</div><!-- from ' . $remindGroupValue . ' -->';
533
            if (!isset($ftrs['noGlobalTab'])) {
534
                $sReturn .= '</div><!-- from global tab -->';
535
            }
536
        }
537
        if (isset($ftrs['actions']['checkbox'])) {
538
            if (strpos($_REQUEST['view'], 'multiEdit') === false) {
539
                $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...
540
                        . '\',\'' . $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...
541
                        . '<a href="#" onclick="javascript:checking(\'' . $checkboxFormId
542
                        . '\',\'' . $checkboxName . '\',false);">Uncheck All</a>&nbsp;&nbsp;'
543
                        . '<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...
544 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...
545
                    if (is_array($ftrs['hiddenInput'])) {
546
                        foreach ($ftrs['hiddenInput'] as $valueF) {
547
                            $sReturn .= '<input type="hidden" name="' . $valueF
548
                                    . '" value="' . $_REQUEST[$valueF] . '" />';
549
                        }
550
                    } else {
551
                        $sReturn .= '<input type="hidden" name="' . $ftrs['hiddenInput']
552
                                . '" value="' . $_REQUEST[$ftrs['hiddenInput']] . '" />';
553
                    }
554
                }
555
                $sReturn .= '<input style="margin: 0 3em 0 3em;" type="submit" ' . 'value="Edit selected" />';
556
            }
557
            $sReturn .= '</form>';
558
        }
559
        if (isset($ftrs['actions']['checkbox_inlineEdit'])) {
560
            $sReturn .= '<a href="#" onclick="javascript:checking(\'' . $checkboxFormId
561
                    . '\',\'' . $checkboxName . '\',true);">Check All</a>&nbsp;&nbsp;'
562
                    . '<a href="#" onclick="javascript:checking(\'' . $checkboxFormId
563
                    . '\',\'' . $checkboxName . '\',false);">Uncheck All</a>&nbsp;&nbsp;';
564
            if (isset($ftrs['visibleInput'])) {
565
                $sReturn .= $ftrs['visibleInput'];
566
            }
567
            $sReturn .= '<input type="hidden" name="view" value="save_' . $checkboxNameS . '" />';
568 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...
569
                if (is_array($ftrs['hiddenInput'])) {
570
                    foreach ($ftrs['hiddenInput'] as $valueF) {
571
                        $sReturn .= '<input type="hidden" name="' . $valueF
572
                                . '" value="' . $_REQUEST[$valueF] . '" />';
573
                    }
574
                } else {
575
                    $sReturn .= '<input type="hidden" name="' . $ftrs['hiddenInput']
576
                            . '" value="' . $_REQUEST[$ftrs['hiddenInput']] . '" />';
577
                }
578
            }
579
            $sReturn .= '<input style="margin: 0 3em 0 3em;" type="submit" value="Store the modification" />';
580
            $sReturn .= '</form>';
581
        }
582
        return $sReturn;
583
    }
584
585
    /**
586
     * Set a control to a user-friendly calendar
587
     *
588
     * @param string $controlName
589
     * @param string $additionalStyle
590
     * @return string
591
     */
592 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...
593
    {
594
        return $this->setStringIntoTag('&nbsp;', 'span', [
595
                    'onclick' => implode('', [
596
                        'javascript:NewCssCal(\'' . $controlName,
597
                        '\',\'yyyyMMdd\',\'dropdown\',false,\'24\',false);',
598
                    ]),
599
                    'class'   => 'fa fa-calendar',
600
                    'id'      => $controlName . '_picker',
601
                    'style'   => 'cursor:pointer;' . $additionalStyle,
602
        ]);
603
    }
604
605
    /**
606
     * Set a control to a user-friendly calendar with time included
607
     *
608
     * @param string $controlName
609
     * @param string $additionalStyle
610
     * @return string
611
     */
612 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...
613
    {
614
        return $this->setStringIntoTag('&nbsp;', 'span', [
615
                    'onclick' => implode('', [
616
                        'javascript:NewCssCal(\'' . $controlName,
617
                        '\',\'yyyyMMdd\',\'dropdown\',true,\'24\',true);',
618
                    ]),
619
                    'class'   => 'fa fa-calendar',
620
                    'id'      => $controlName . '_picker',
621
                    'style'   => 'cursor:pointer;' . $additionalStyle,
622
        ]);
623
    }
624
625
    /**
626
     * Cleans a string for certain internal rules
627
     *
628
     * @param type $urlString
629
     * @return type
630
     */
631
    protected function setCleanUrl($urlString)
632
    {
633
        $arrayToReplace = [
634
            '&#038;'    => '&amp;',
635
            '&'         => '&amp;',
636
            '&amp;amp;' => '&amp;',
637
            ' '         => '%20',
638
        ];
639
        $kys            = array_keys($arrayToReplace);
640
        $vls            = array_values($arrayToReplace);
641
        return str_replace($kys, $vls, filter_var($urlString, FILTER_SANITIZE_URL));
642
    }
643
644
    /**
645
     * Returns a div tag that clear any float
646
     *
647
     * @param integer $height
648
     */
649 View Code Duplication
    protected function setClearBoth1px($height = 1)
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...
650
    {
651
        return $this->setStringIntoTag('&nbsp;', 'div', [
652
                    'style' => implode('', [
653
                        'height:' . $height . 'px;',
654
                        'line-height:' . $height . 'px;',
655
                        'float:none;',
656
                        'clear:both;',
657
                        'margin:0px;'
658
                    ])
659
        ]);
660
    }
661
662
    /**
663
     * Returns css codes
664
     *
665
     * @param string $cssContent
666
     * @param array $optionalFlags
667
     * @return string
668
     */
669
    protected function setCssContent($cssContent, $optionalFlags = null)
670
    {
671
        if (is_null($optionalFlags)) {
672
            $attr['media'] = 'all';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attr was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attr = 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...
673
        } else {
674
            $knownAttributes = ['media'];
675
            foreach ($knownAttributes as $value) {
676
                if (in_array($value, array_keys($optionalFlags))) {
677
                    $attr[$value] = $optionalFlags[$value];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attr was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attr = 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...
678
                }
679
            }
680
        }
681
        return '<style type="text/css" media="' . $attr['media'] . '">'
0 ignored issues
show
Bug introduced by
The variable $attr 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...
682
                . $cssContent
683
                . '</style>';
684
    }
685
686
    /**
687
     * Returns css link to a given file
688
     *
689
     * @param string $cssFile
0 ignored issues
show
Documentation introduced by
There is no parameter named $cssFile. Did you maybe mean $cssFileName?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
690
     * @return string
691
     */
692
    protected function setCssFile($cssFileName, $hostsWithoutCDNrq = null)
693
    {
694
        $sReturn = null;
0 ignored issues
show
Unused Code introduced by
$sReturn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
695
        if (is_null($hostsWithoutCDNrq)) {
696
            $hostsWithoutCDNrq = [];
697
        }
698
        if (in_array($this->getClientRealIpAddress(), $hostsWithoutCDNrq)) {
699
            $sReturn = '<link rel="stylesheet" type="text/css" href="'
700
                    . filter_var($cssFileName, FILTER_SANITIZE_STRING)
701
                    . '" />';
702
        } else {
703
            $patternFound = $this->setCssFileCDN($cssFileName);
0 ignored issues
show
Bug introduced by
The method setCssFileCDN() does not exist on danielgp\common_lib\DomComponentsByDanielGP. Did you maybe mean setCssFile()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
704
            $sReturn      = '<link rel="stylesheet" type="text/css" href="'
705
                    . filter_var($patternFound[1], FILTER_SANITIZE_STRING)
706
                    . '" />';
707
        }
708
        return $sReturn;
709
    }
710
711
    /**
712
     * Builds a structured modern message
713
     *
714
     * @param string $sType
715
     * @param string $sTitle
716
     * @param string $sMsg
717
     * @param boolean $skipBr
718
     */
719
    protected function setFeedbackModern($sType, $sTitle, $sMsg, $skipBr = false)
720
    {
721
        $formatTitle[]   = 'margin-top:-5px;margin-right:20px;padding:5px;';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$formatTitle was never initialized. Although not strictly required by PHP, it is generally a good practice to add $formatTitle = 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...
722
        $formatMessage[] = 'display:inline;padding-right:5px;padding-bottom:5px;';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$formatMessage was never initialized. Although not strictly required by PHP, it is generally a good practice to add $formatMessage = 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...
723
        switch ($sType) {
724
            case 'alert':
725
                $formatTitle[]   = 'border:medium solid orange;background-color:orange;color:navy;';
726
                $formatMessage[] = 'background-color:navy;color:orange;border:medium solid orange;';
727
                break;
728
            case 'check':
729
                $formatTitle[]   = 'border:medium solid green;background-color:green;color:white;';
730
                $formatMessage[] = 'background-color:yellow;color:green;border:medium solid green;';
731
                break;
732
            case 'error':
733
                $formatTitle[]   = 'border:medium solid red;background-color:red;color:white;';
734
                $formatMessage[] = 'background-color:yellow;color:red;border:medium solid red;';
735
                break;
736
            case 'info':
737
                $formatTitle[]   = 'border:medium solid black;background-color:black;color:white;font-weight:bold;';
738
                $formatMessage[] = 'background-color: white; color: black;border:medium solid black;';
739
                break;
740
        }
741
        if ($sTitle == 'light') {
742
            echo $sMsg;
743
        } else {
744
            $legend = $this->setStringIntoTag($sTitle, 'legend', ['style' => implode('', $formatTitle)]);
745
            return implode('', [
746
                ($skipBr ? '' : '<br/>'),
747
                $this->setStringIntoTag($legend . $sMsg, 'fieldset', [
748
                    'style' => implode('', $formatMessage)
749
                ]),
750
            ]);
751
        }
752
    }
753
754
    /**
755
     * Outputs an HTML footer
756
     *
757
     * @param array $footerInjected
758
     * @return string
759
     */
760
    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...
761
    {
762
        if (isset($_REQUEST['specialHook']) && (in_array('noFooter', $_REQUEST['specialHook']))) {
763
            return '';
764
        }
765
        $sReturn = '';
766
        if (!is_null($footerInjected)) {
767
            $sReturn = $footerInjected;
768
            if (is_array($footerInjected)) {
769
                $sReturn = implode('', $footerInjected);
770
            }
771
        }
772
        return $sReturn . '</body></html>';
773
    }
774
775
    /**
776
     * Sets the gzip footer for HTML
777
     */
778
    protected function setFooterGZiped()
0 ignored issues
show
Coding Style introduced by
setFooterGZiped 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...
779
    {
780
        if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
781
            if (strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
782
                if (extension_loaded('zlib')) {
783
                    $gzip_contents = ob_get_contents();
784
                    ob_end_clean();
785
                    $gzip_size     = strlen($gzip_contents);
786
                    $gzip_crc      = crc32($gzip_contents);
787
                    $gzip_contents = gzcompress($gzip_contents, 9);
788
                    $gzip_contents = substr($gzip_contents, 0, strlen($gzip_contents) - 4);
789
                    echo "\x1f\x8b\x08\x00\x00\x00\x00\x00";
790
                    echo $gzip_contents;
791
                    echo pack('V', $gzip_crc);
792
                    echo pack('V', $gzip_size);
793
                }
794
            }
795
        }
796
    }
797
798
    /**
799
     * Outputs an HTML header
800
     *
801
     * @param array $headerFeatures
802
     * @return string
803
     */
804
    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...
805
    {
806
        $sReturn = [];
807
        if (isset($_REQUEST['specialHook']) && (in_array('noHeader', $_REQUEST['specialHook']))) {
808
            $sReturn[] = ''; // no Header
809
        } else {
810
            $fixedHeaderElements = [
811
                'start'    => '<!DOCTYPE html>',
812
                'lang'     => '<html lang="en-US">',
813
                'head'     => '<head>',
814
                'charset'  => '<meta charset="utf-8" />',
815
                'viewport' => '<meta name="viewport" content="' . implode(', ', [
816
                    'width=device-width',
817
                    'height=device-height',
818
                    'initial-scale=1',
819
                ]) . '" />',
820
            ];
821
            if (!is_null($headerFeatures)) {
822
                if (is_array($headerFeatures)) {
823
                    $aFeatures = [];
824
                    foreach ($headerFeatures as $key => $value) {
825
                        switch ($key) {
826 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...
827
                                if (is_array($value)) {
828
                                    foreach ($value as $value2) {
829
                                        $aFeatures[] = $this->setCssFile(filter_var($value2, FILTER_SANITIZE_URL));
830
                                    }
831
                                } else {
832
                                    $aFeatures[] = $this->setCssFile(filter_var($value, FILTER_SANITIZE_URL));
833
                                }
834
                                break;
835 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...
836
                                if (is_array($value)) {
837
                                    foreach ($value as $value2) {
838
                                        $vl          = filter_var($value2, FILTER_SANITIZE_URL);
839
                                        $aFeatures[] = $this->setJavascriptFile($vl);
840
                                    }
841
                                } else {
842
                                    $aFeatures[] = $this->setJavascriptFile(filter_var($value, FILTER_SANITIZE_URL));
843
                                }
844
                                break;
845
                            case 'lang':
846
                                $fixedHeaderElements['lang'] = '<html lang="'
847
                                        . filter_var($value, FILTER_SANITIZE_STRING) . '">';
848
                                break;
849
                            case 'title':
850
                                $aFeatures[]                 = '<title>'
851
                                        . filter_var($value, FILTER_SANITIZE_STRING) . '</title>';
852
                                break;
853
                        }
854
                    }
855
                    $sReturn[] = implode('', $fixedHeaderElements)
856
                            . implode('', $aFeatures)
857
                            . '</head>'
858
                            . '<body>';
859
                } else {
860
                    $sReturn[] = implode('', $fixedHeaderElements)
861
                            . '</head>'
862
                            . '<body>'
863
                            . '<p style="background-color:red;color:#FFF;">The parameter sent to '
864
                            . __FUNCTION__ . ' must be an array</p>'
865
                            . $this->setFooterCommon();
866
                    throw new \Exception($sReturn);
867
                }
868
            }
869
        }
870
        return implode('', $sReturn);
871
    }
872
873
    /**
874
     * Sets the gzip header for HTML
875
     */
876
    protected function setHeaderGZiped()
0 ignored issues
show
Coding Style introduced by
setHeaderGZiped 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...
877
    {
878
        if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
879
            if (strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
880
                if (extension_loaded('zlib')) {
881
                    ob_start();
882
                    ob_implicit_flush(0);
883
                    header('Content-Encoding: gzip');
884
                }
885
            }
886
        }
887
    }
888
889
    /**
890
     * Sets the no-cache header
891
     */
892
    protected function setHeaderNoCache($contentType = 'application/json')
893
    {
894
        header("Content-Type: " . $contentType . "; charset=utf-8");
895
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
896
        header("Cache-Control: no-store, no-cache, must-revalidate");
897
        header("Cache-Control: post-check=0, pre-check=0", false);
898
        header("Pragma: no-cache");
899
    }
900
901
    /**
902
     * Returns javascript function to support Add or Edit through Ajax
903
     *
904
     * @return string
905
     */
906
    protected function setJavascriptAddEditByAjax($tabName = 'tabStandard')
907
    {
908
        return $this->setJavascriptContent(implode('', [
909
                    'function loadAE(action) {',
910
                    'document.getElementById("' . $tabName . '").tabber.tabShow(1);',
911
                    '$("#DynamicAddEditSpacer").load(action',
912
                    '+"&specialHook[]=noHeader"',
913
                    '+"&specialHook[]=noMenu"',
914
                    '+"&specialHook[]=noContainer"',
915
                    '+"&specialHook[]=noFooter"',
916
                    ');',
917
                    '}',
918
        ]));
919
    }
920
921
    /**
922
     * Returns javascript codes
923
     *
924
     * @param string $javascriptContent
925
     * @return string
926
     */
927
    protected function setJavascriptContent($javascriptContent)
928
    {
929
        return '<script type="text/javascript">' . $javascriptContent . '</script>';
930
    }
931
932
    /**
933
     * Builds up a confirmation dialog and return delection if Yes
934
     *
935
     * @return string
936
     */
937
    protected function setJavascriptDeleteWithConfirmation()
938
    {
939
        return $this->setJavascriptContent(' function setQuest(a, b) { '
940
                        . 'c = a.indexOf("_"); switch(a.slice(0, c)) { '
941
                        . 'case \'delete\': '
942
                        . 'if (confirm(\'' . $this->lclMsgCmn('i18n_ActionDelete_ConfirmationQuestion') . '\')) { '
0 ignored issues
show
Bug introduced by
It seems like lclMsgCmn() 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...
943
                        . 'window.location = document.location.protocol + "//" + '
944
                        . 'document.location.host + document.location.pathname + '
945
                        . '"?view=" + a + "&" + b; } break; } }');
946
    }
947
948
    /**
949
     * Returns javascript link to a given file
950
     *
951
     * @param string $jsFileName
952
     * @return string
953
     */
954
    protected function setJavascriptFile($jsFileName, $hostsWithoutCDNrq = null)
955
    {
956
        $sReturn = null;
0 ignored issues
show
Unused Code introduced by
$sReturn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
957
        if (is_null($hostsWithoutCDNrq)) {
958
            $hostsWithoutCDNrq = [];
959
        }
960
        if (in_array($this->getClientRealIpAddress(), $hostsWithoutCDNrq)) {
961
            $sReturn = '<script type="text/javascript" src="' . $jsFileName . '"></script>';
962
        } else {
963
            $patternFound = $this->setJavascriptFileCDN($jsFileName);
0 ignored issues
show
Bug introduced by
The method setJavascriptFileCDN() does not exist on danielgp\common_lib\DomComponentsByDanielGP. Did you maybe mean setJavascriptFile()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
964
            $sReturn      = '<script type="text/javascript" src="' . $patternFound[1] . '"></script>'
965
                    . $patternFound[2];
966
        }
967
        return $sReturn;
968
    }
969
970
    /**
971
     * Returns javascript codes from given file
972
     *
973
     * @param string $jsFileName
974
     * @return string
975
     */
976
    protected function setJavascriptFileContent($jsFileName)
977
    {
978
        $sReturn[] = '<script type="text/javascript"><!-- ';
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...
979
        $sReturn[] = file_get_contents($jsFileName, true);
980
        $sReturn[] = ' //--></script>';
981
        return implode('', $sReturn);
982
    }
983
984
    /**
985
     * Creates all the child tags required to populate a SELECT tag
986
     *
987
     * @param array $aElements
988
     * @param string|array $sDefaultValue
989
     * @param array $features_array
990
     * @return string
991
     */
992
    private function setOptionsForSelect($aElements, $sDefaultValue, $features_array = [])
993
    {
994
        $string2return = '';
995
        if (is_array($features_array)) {
996
            if (in_array('include_null', $features_array)) {
997
                $string2return .= '<option value="NULL">&nbsp;</option>';
998
            }
999
            if (isset($features_array['defaultValue_isSubstring'])) {
1000
                $default_value_array = explode($features_array['defaultValue_isSubstring'], $sDefaultValue);
1001
            }
1002
        }
1003
        $current_group = null;
1004
        foreach ($aElements as $key => $value) {
1005
            if (isset($features_array['grouping'])) {
1006
                $temporary_string = substr($value, 0, strpos($value, $features_array['grouping']) + 1);
1007
                if ($current_group != $temporary_string) {
1008
                    if ($current_group != '') {
1009
                        $string2return .= '</optgroup>';
1010
                    }
1011
                    $current_group = $temporary_string;
1012
                    $string2return .= '<optgroup label="'
1013
                            . str_replace($features_array['grouping'], '', $current_group) . '">';
1014
                }
1015
            } else {
1016
                $current_group = '';
1017
            }
1018
            $string2return .= '<option value="' . $key . '"';
1019
            if (is_array($sDefaultValue)) {
1020
                if (in_array($key, $sDefaultValue)) {
1021
                    $string2return .= ' selected="selected"';
1022
                }
1023
            } else {
1024
                if (strcasecmp($key, $sDefaultValue) === 0) {
1025
                    $string2return .= ' selected="selected"';
1026
                }
1027
                if (isset($default_value_array) && is_array($default_value_array)) {
1028
                    if (in_array($key, $default_value_array)) {
1029
                        $string2return .= ' selected="selected"';
1030
                    }
1031
                }
1032
            }
1033
            if (array_key_exists('styleForOption', $features_array)) {
1034
                $string2return .= ' style="' . $features_array['style'] . '"';
1035
            }
1036
            $string2return .= '>' . str_replace(['&', $current_group], ['&amp;', ''], $value) . '</option>';
1037
        }
1038
        if (isset($features_array['grouping'])) {
1039
            if ($current_group != '') {
1040
                $string2return .= '</optgroup>';
1041
            }
1042
        }
1043
        return $string2return;
1044
    }
1045
1046
    /**
1047
     * Puts a given string into a specific short tag
1048
     *
1049
     * @param string $sTag
1050
     * @param array $features
1051
     * @return string
1052
     */
1053
    protected function setStringIntoShortTag($sTag, $features = null)
1054
    {
1055
        $attributes = '';
1056
        if (!is_null($features)) {
1057
            foreach ($features as $key => $value) {
1058
                if ($key != 'dont_close') {
1059
                    $attributes .= ' ' . $key . '="';
1060 View Code Duplication
                    if (is_array($value)) {
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...
1061
                        foreach ($value as $key2 => $value2) {
1062
                            $attributes .= $key2 . ':' . $value2 . ';';
1063
                        }
1064
                    } else {
1065
                        $attributes .= str_replace('"', '\'', $value);
1066
                    }
1067
                    $attributes .= '"';
1068
                }
1069
            }
1070
        }
1071
        if (isset($features['dont_close'])) {
1072
            $sReturn = '<' . $sTag . $attributes . '>';
1073
        } else {
1074
            $sReturn = '<' . $sTag . $attributes . ' />';
1075
        }
1076
        return $sReturn;
1077
    }
1078
1079
    /**
1080
     * Puts a given string into a specific tag
1081
     *
1082
     * @param string $sString
1083
     * @param string $sTag
1084
     * @param array $features
1085
     * @return string
1086
     */
1087
    protected function setStringIntoTag($sString, $sTag, $features = null)
1088
    {
1089
        $attributes = '';
1090
        if (!is_null($features)) {
1091
            foreach ($features as $key => $value) {
1092
                $attributes .= ' ' . $key . '="';
1093 View Code Duplication
                if (is_array($value)) {
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...
1094
                    foreach ($value as $key2 => $value2) {
1095
                        $attributes .= $key2 . ':' . $value2 . ';';
1096
                    }
1097
                } else {
1098
                    $attributes .= $value;
1099
                }
1100
                $attributes .= '"';
1101
            }
1102
        }
1103
        return '<' . $sTag . $attributes . '>' . $sString . '</' . $sTag . '>';
1104
    }
1105
1106
    /**
1107
     * Generates a table cell
1108
     *
1109
     * @param array $aElements
1110
     * @param array $features
1111
     * @return string
1112
     */
1113
    private function setTableCell($aElements, $features = null)
1114
    {
1115
        $sReturn = null;
1116
        foreach ($aElements as $key => $value) {
1117
            $value = str_replace(['& ', '\"', "\'"], ['&amp; ', '"', "'"], $value);
1118
            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...
1119
                // just skip
1120
            } else {
1121
                $sReturn .= '<td ';
1122
                if (isset($features['column_formatting'][$key])) {
1123
                    switch ($features['column_formatting'][$key]) {
1124
                        case '@':
1125
                            $sReturn .= 'style="text-align:left;">' . $value;
1126
                            break;
1127
                        case 'right':
1128
                            $sReturn .= 'style="text-align:right;">' . $value;
1129
                            break;
1130
                        default:
1131
                            $sReturn .= '???';
1132
                            break;
1133
                    }
1134
                } else {
1135
                    if (is_numeric($value)) {
1136
                        if (substr($value, 0, 1) === '0') {
1137
                            $sReturn .= 'style="text-align: right;">' . $value;
1138
                        } else {
1139
                            $decimals = 0;
1140
                            if (isset($features['no_of_decimals'])) {
1141
                                $decimals = $features['no_of_decimals'];
1142
                            }
1143
                            if (isset($features['decimals']) && array_key_exists($key, $features['decimals'])) {
1144
                                $decimals = $features['decimals'][$key];
1145
                            }
1146
                            $sReturn .= 'style="text-align: right;">';
1147
                            $sReturn .= $this->setNumberFormat($value, [
0 ignored issues
show
Bug introduced by
It seems like setNumberFormat() 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...
1148
                                'MinFractionDigits' => $decimals,
1149
                                'MaxFractionDigits' => $decimals
1150
                            ]);
1151
                        }
1152
                    } else {
1153
                        $outputet = false;
1154
                        if ((strpos($value, '-') !== false) && (strlen($value) == 10)) {
1155
                            if (preg_match("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $value, $regs)) {
1156
                                $outputet = true;
1157
                                $sReturn .= 'style="text-align:right;width: 10px;">'
1158
                                        . $regs[3] . '.' . $regs[2] . '.' . $regs[1];
1159
                            }
1160
                        }
1161
                        if (!$outputet) {
1162
                            $sReturn .= 'style="text-align:left;">' . $value;
1163
                        }
1164
                    }
1165
                }
1166
                $sReturn .= '</td>';
1167
            }
1168
        }
1169
        return $sReturn;
1170
    }
1171
1172
    /**
1173
     * Generates a table header
1174
     *
1175
     * @param array $aElements
1176
     * @param boolean $bHeadersBreaked
1177
     * @return string
1178
     */
1179
    private function setTableHeader($aElements, $bHeadersBreaked)
1180
    {
1181
        if ($bHeadersBreaked) {
1182
            $aTableHeader = $this->setArrayToArrayKbr($aElements);
0 ignored issues
show
Bug introduced by
It seems like setArrayToArrayKbr() 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...
1183
        } else {
1184
            $aTableHeader = $aElements;
1185
        }
1186
        $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...
1187
        foreach (array_keys($aTableHeader) as $value) {
1188
            $sReturn[] = $this->setStringIntoTag($value, 'th');
1189
        }
1190
        return implode('', $sReturn);
1191
    }
1192
1193
    protected function setViewModernLinkAdd($identifier, $ftrs = null)
1194
    {
1195
        $btnText = '<i class="fa fa-plus-square">&nbsp;</i>' . '&nbsp;' . $this->lclMsgCmn('i18n_AddNewRecord');
0 ignored issues
show
Bug introduced by
It seems like lclMsgCmn() 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...
1196
        return $this->setStringIntoTag($btnText, 'a', [
1197
                    'href'  => $this->setViewModernLinkAddUrl($identifier, $ftrs),
1198
                    'style' => 'margin: 5px 0px 10px 0px; display: inline-block;',
1199
        ]);
1200
    }
1201
1202
    protected function setViewModernLinkAddInjectedArguments($identifier, $ftrs = null)
0 ignored issues
show
Unused Code introduced by
The parameter $identifier is not used and could be removed.

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

Loading history...
1203
    {
1204
        $sArgmnts = '';
1205
        if (isset($ftrs['injectAddArguments'])) {
1206
            foreach ($ftrs['injectAddArguments'] as $key => $value) {
1207
                $sArgmnts .= '&amp;' . $key . '=' . $value;
1208
            }
1209
        }
1210
        return $sArgmnts;
1211
    }
1212
1213
    protected function setViewModernLinkAddUrl($identifier, $ftrs = null)
1214
    {
1215
        $sArgmnts  = $this->setViewModernLinkAddInjectedArguments($identifier);
1216
        $rqst      = new \Symfony\Component\HttpFoundation\Request;
1217
        $addingUrl = $rqst->server->get('PHP_SELF') . '?view=add_' . $identifier . $sArgmnts;
1218
        if (!isset($ftrs['NoAjax'])) {
1219
            $addingUrl = 'javascript:loadAE(\'' . $addingUrl . '\');';
1220
        }
1221
        return $addingUrl;
1222
    }
1223
1224
    protected function updateDivTitleName($rememberGroupVal, $groupCounter)
1225
    {
1226
        $jsContent = '$(document).ready(function() { $("#tab_'
1227
                . $this->cleanStringForId($rememberGroupVal) . '").attr("title", "'
1228
                . $rememberGroupVal . ' (' . $groupCounter . ')"); });';
1229
        return $this->setJavascriptContent($jsContent);
1230
    }
1231
}
1232