Completed
Push — master ( 5049a1...5cff95 )
by Daniel
02:42
created

DomComponentsByDanielGP::setFooterCommon()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 7
rs 9.4286
cc 3
eloc 4
nc 2
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
    private function buildAttributesForTag($features)
42
    {
43
        $attributes = '';
44
        foreach ($features as $key => $value) {
45
            $val = $value;
46
            if (is_array($value)) {
47
                $valA = [];
48
                foreach ($value as $key2 => $value2) {
49
                    $valA[] = $key2 . ':' . $value2 . ';';
50
                }
51
                $val = implode(';', $valA);
52
            }
53
            $attributes .= ' ' . $key . '="' . $val . '"';
54
        }
55
        return $attributes;
56
    }
57
58
    /**
59
     * Calculate the optimal for all options within a select tag
60
     *
61
     * @param array $aElements
62
     * @param array $aFeatures
63
     * @return string|int
64
     */
65
    private function calculateSelectOptionsSize($aElements, $aFeatures = [])
66
    {
67
        if (is_null($aFeatures)) {
68
            $aFeatures = [];
69
        }
70
        if (is_array($aElements)) {
71
            if (isset($aFeatures['size'])) {
72
                if ($aFeatures['size'] == 0) {
73
                    $selectSize = count($aElements);
74
                } else {
75
                    $selectSize = min(count($aElements), $aFeatures['size']);
76
                }
77
            } else {
78
                $selectSize = 1;
79
            }
80
            if ((in_array('include_null', $aFeatures)) && ($selectSize != '1')) {
81
                $selectSize++;
82
            }
83
            return $selectSize;
84
        } else {
85
            return '';
86
        }
87
    }
88
89
    protected function cleanStringForId($givenString)
90
    {
91
        return preg_replace("/[^a-zA-Z0-9]/", '', ucwords($givenString));
92
    }
93
94
    /**
95
     * Builds a <select> based on a given array
96
     *
97
     * @version 20080618
98
     * @param array $aElements
99
     * @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...
100
     * @param string $select_name
101
     * @param array $features_array
102
     * @return string
103
     */
104
    protected function setArrayToSelect($aElements, $sDefaultValue, $select_name, $features_array = null)
105
    {
106
        if (!is_array($aElements)) {
107
            return '';
108
        }
109
        $select_id = '" id="' . str_replace(['[', ']'], ['', ''], $select_name)
110
                . (isset($features_array['id_no']) ? $features_array['id_no'] : '');
111
        if (isset($features_array['readonly'])) {
112
            return $this->setStringIntoShortTag('input', [
113
                        'name'     => $select_name,
114
                        'id'       => $select_id,
115
                        'readonly' => 'readonly',
116
                        'class'    => 'input_readonly',
117
                        'value'    => $sDefaultValue,
118
                    ]) . $aElements[$sDefaultValue];
119
        }
120
        if (isset($features_array['id_no'])) {
121
            unset($features_array['id_no']);
122
        }
123
        $string2return = '<select name="' . $select_name . $select_id
124
                . '" size="' . $this->calculateSelectOptionsSize($aElements, $features_array) . '"';
125
        if (is_array($features_array)) {
126
            if (isset($features_array['additional_javascript_action'])) {
127
                $temporary_string = $features_array['additional_javascript_action'];
128
            } else {
129
                $temporary_string = '';
130
            }
131
            if (in_array('autosubmit', $features_array)) {
132
                $string2return .= ' onchange="javascript:' . $temporary_string . 'submit();"';
133
            } else {
134
                if ($temporary_string != '') {
135
                    $string2return .= ' onchange="javascript:' . $temporary_string . '"';
136
                }
137
            }
138
            if (in_array('disabled', $features_array)) {
139
                $string2return .= ' disabled="disabled"';
140
            }
141
            if (in_array('hidden', $features_array)) {
142
                $string2return .= ' style="visibility: hidden;"';
143
            }
144
            if (in_array('multiselect', $features_array)) {
145
                $string2return .= ' multiple="multiple"';
146
            }
147
            if (array_key_exists('style', $features_array)) {
148
                $string2return .= ' style="' . $features_array['style'] . '"';
149
            }
150
        }
151
        $string2return .= '>'
152
                . $this->setOptionsForSelect($aElements, $sDefaultValue, $features_array)
153
                . '</select>';
154
        return $string2return;
155
    }
156
157
    /**
158
     * Converts an array to string
159
     *
160
     * @param string $sSeparator
161
     * @param array $aElements
162
     * @return string
163
     */
164
    protected function setArrayToStringForUrl($sSeparator, $aElements, $aExceptedElements = [''])
165
    {
166
        if (is_array($aElements)) {
167
            if (count($aElements) == 0) {
168
                $sReturn = [''];
169
            } else {
170
                $sReturn = [];
171
                foreach ($aElements as $key => $value) {
172
                    if (!in_array($key, $aExceptedElements)) {
173
                        if (is_array($aElements[$key])) {
174
                            $aCounter = count($aElements[$key]);
175
                            for ($counter2 = 0; $counter2 < $aCounter; $counter2++) {
176
                                if ($value[$counter2] !== '') {
177
                                    $sReturn[] = $key . '[]=' . $value[$counter2];
178
                                }
179
                            }
180
                        } else {
181
                            if ($value !== '') {
182
                                $sReturn[] = $key . '=' . $value;
183
                            }
184
                        }
185
                    }
186
                }
187
            }
188
        } else {
189
            $sReturn = [''];
190
        }
191
        return implode($sSeparator, $sReturn);
192
    }
193
194
    /**
195
     * Returns a table from an query
196
     *
197
     * @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...
198
     * @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...
199
     * @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...
200
     * @return string
201
     */
202
    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...
203
    {
204
        $rows = count($aElements);
205
        if ($rows == 0) {
206
            $divTab = [
207
                'start' => '',
208
                'end'   => '',
209
            ];
210
            if (array_key_exists('showGroupingCounter', $ftrs)) {
211
                if (array_key_exists('grouping_cell_type', $ftrs) && ($ftrs['grouping_cell_type'] == 'tab')) {
212
                    $ditTitle = 'No data found';
213
                    if (isset($ftrs['showGroupingCounter'])) {
214
                        $ditTitle .= ' (0)';
215
                    }
216
                    $divTab = [
217
                        'start' => '<div class="tabbertab tabbertabdefault" id="tab_NoData" title="' . $ditTitle . '">',
218
                        'end'   => '</div><!-- from tab_NoData -->',
219
                    ];
220
                    if (!isset($ftrs['noGlobalTab'])) {
221
                        $divTab = [
222
                            'start' => '<div class="tabber" id="tab">' . $divTab['start'],
223
                            'end'   => $divTab['end'] . '</div><!-- from global Tab -->',
224
                        ];
225
                    }
226
                }
227
            }
228
            return $divTab['start']
229
                    . $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...
230
                    . $divTab['end'];
231
        }
232
        if (isset($ftrs['limits'])) {
233
            $ftrs['limits'][1] = min($ftrs['limits'][1], $ftrs['limits'][2]);
234
            if ($ftrs['limits'][2] > $ftrs['limits'][1]) {
235
                $iStartingPageRecord = 1;
236
            }
237
        }
238
        $sReturn = '';
239
        if (isset($ftrs['hidden_columns'])) {
240
            $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...
241
        } else {
242
            $hdClmns = [''];
243
        }
244
        if ((isset($ftrs['actions']['checkbox_inlineEdit'])) || (isset($ftrs['actions']['checkbox']))) {
245
            $checkboxFormId = 'frm' . date('YmdHis');
246
            $sReturn .= '<form id="' . $checkboxFormId . '" ' . 'name="' . $checkboxFormId
247
                    . '" method="post" ' . ' action="' . $_SERVER['PHP_SELF'] . '" >';
248
        }
249
        $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...
250
                . (isset($ftrs['table_style']) ? ' style="' . $ftrs['table_style'] . '"' : '')
251
                . (isset($ftrs['table_class']) ? ' class="' . $ftrs['table_class'] . '"' : '')
252
                . '>';
253
        if (!isset($ftrs['grouping_cell_type'])) {
254
            $ftrs['grouping_cell_type'] = 'row';
255
        }
256
        switch ($ftrs['grouping_cell_type']) {
257
            case 'row':
258
                $sReturn .= $tbl['Def'];
259
                break;
260
            case 'tab':
261
                if (!isset($ftrs['noGlobalTab'])) {
262
                    $sReturn .= '<div class="tabber" id="tab">';
263
                }
264
                break;
265
        }
266
        $iTableColumns    = 0;
267
        $remebered_value  = -1;
268
        $remindGroupValue = null;
269
        $color_no         = null;
270
        if (!isset($ftrs['headers_breaked'])) {
271
            $ftrs['headers_breaked'] = true;
272
        }
273
        for ($rCntr = 0; $rCntr < $rows; $rCntr++) {
274
            if ($rCntr == 0) {
275
                $header        = array_diff_key($aElements[$rCntr], $hdClmns);
276
                $iTableColumns = count($header);
277
                if (isset($ftrs['computed_columns'])) {
278
                    $iTableColumns += count($ftrs['computed_columns']);
279
                }
280
                if (isset($ftrs['actions'])) {
281
                    $iTableColumns += 1;
282
                }
283
                if (isset($ftrs['grouping_cell'])) {
284
                    $iTableColumns -= 1;
285
                }
286
                $tbl['Head'] = '<thead>';
287
                if ($ftrs['grouping_cell_type'] == 'row') {
288
                    $sReturn .= $tbl['Head'];
289
                }
290 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...
291
                    $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...
292
                    $sReturn .= $this->setStringIntoTag($this->setStringIntoTag($pgn, 'th', [
293
                                'colspan' => $iTableColumns
294
                            ]), 'tr');
295
                }
296
                $tbl['Header'] = '<tr>';
297
                if (isset($ftrs['grouping_cell'])) { // Grouping columns
298
                    $header = array_diff_key($header, [$ftrs['grouping_cell'] => '']);
299
                }
300
                if (isset($ftrs['actions'])) { // Action column
301
                    $tbl['Header'] .= '<th>&nbsp;</th>';
302
                }
303
                if (isset($ftrs['RowStyle'])) { //Exclude style columns from displaying
304
                    $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...
305
                    $header   = array_diff_key($header, $tmpClmns);
306
                    $hdClmns  = array_merge($hdClmns, $tmpClmns);
307
                    unset($tmpClmns);
308
                }
309
                $tbl['Header'] .= $this->setTableHeader($header, $ftrs['headers_breaked']); // Regular columns
310
                if (isset($ftrs['computed_columns'])) { // Computed columns
311
                    $tbl['Header'] .= $this->setTableHeader($ftrs['computed_columns'], $ftrs['headers_breaked']);
312
                }
313
                $tbl['Header'] .= '</tr></thead><tbody>';
314
                if ($ftrs['grouping_cell_type'] == 'row') {
315
                    $sReturn .= $tbl['Header'];
316
                }
317
            }
318
            $row_current = array_diff_key($aElements[$rCntr], $hdClmns);
319
            if (isset($ftrs['row_colored_alternated'])) {
320
                if ($ftrs['row_colored_alternated'][0] == '#') {
321
                    $color_column_value = $rCntr;
322
                } else {
323
                    $color_column_value = $row_current[$ftrs['row_colored_alternated'][0]];
324
                }
325
                if ($remebered_value != $color_column_value) {
326
                    if (isset($color_no)) {
327
                        $color_no = 1;
328
                    } else {
329
                        $color_no = 2;
330
                    }
331
                    $remebered_value = $color_column_value;
332
                }
333
                $color = ' style="background-color: ' . $ftrs['row_colored_alternated'][$color_no] . ';"';
334
            } else {
335
                if (isset($ftrs['RowStyle'])) {
336
                    $color = ' style="' . $aElements[$rCntr][$ftrs['RowStyle']] . '"';
337
                } else {
338
                    $color = '';
339
                }
340
            }
341
            $tbl['tr_Color'] = '<tr' . $color . '>';
342
// Grouping column
343
            if (isset($ftrs['grouping_cell'])) {
344
                foreach ($aElements[$rCntr] as $key => $value) {
345
                    if (($ftrs['grouping_cell'] == $key) && ($remindGroupValue != $value)) {
346
                        switch ($ftrs['grouping_cell_type']) {
347
                            case 'row':
348
                                $sReturn .= $tbl['tr_Color'] . '<td ' . 'colspan="' . $iTableColumns . '">'
349
                                        . $this->setStringIntoTag($value, 'div', ['class' => 'rowGroup rounded'])
350
                                        . '</td></tr>';
351
                                break;
352
                            case 'tab':
353
                                if (is_null($remindGroupValue)) {
354
                                    if (isset($ftrs['showGroupingCounter'])) {
355
                                        $groupCounter = 0;
356
                                    }
357
                                } else {
358
                                    $sReturn .= '</tbody></table>';
359
                                    if (isset($ftrs['showGroupingCounter'])) {
360
                                        $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...
361
                                        $groupCounter = 0;
362
                                    }
363
                                    $sReturn .= '</div>';
364
                                }
365
                                $sReturn .= '<div class="tabbertab';
366
                                if (isset($ftrs['grouping_default_tab'])) {
367
                                    $sReturn .= ($ftrs['grouping_default_tab'] == $value ? ' tabbertabdefault' : '');
368
                                }
369
                                $sReturn .= '" id="tab_' . $this->cleanStringForId($value) . '" '
370
                                        . 'title="' . $value . '">'
371
                                        . $tbl['Def'] . $tbl['Head'] . $tbl['Header'];
372
                                break;
373
                        }
374
                        $remindGroupValue = $value;
375
                    }
376
                }
377
            }
378
            if (isset($ftrs['grouping_cell'])) {
379
                if ($ftrs['grouping_cell_type'] == 'tab') {
380
                    if (isset($ftrs['showGroupingCounter'])) {
381
                        $groupCounter++;
382
                    }
383
                }
384
            }
385
            $sReturn .= $tbl['tr_Color'];
386
// Action column
387
            if (isset($ftrs['actions'])) {
388
                $sReturn .= '<td style="white-space:nowrap;">';
389
                $action_argument = 0;
390
                if (isset($ftrs['actions']['key'])) {
391
                    $action_key = $ftrs['actions']['key'];
392
                } else {
393
                    $action_key = 'view';
394
                }
395
                if (isset($ftrs['action_prefix'])) {
396
                    $actPrfx    = $ftrs['action_prefix'] . '&amp;';
397
                    $action_key = 'view2';
398
                } else {
399
                    $actPrfx = '';
400
                }
401
                foreach ($ftrs['actions'] as $key => $value) {
402
                    if ($action_argument != 0) {
403
                        $sReturn .= '&nbsp;';
404
                    }
405
                    switch ($key) {
406
                        case 'checkbox':
407
                            $checkboxName  = $value . '[]';
408
                            $checkboxNameS = $value;
409
                            $sReturn .= '&nbsp;<input type="checkbox" name="' . $checkboxName
410
                                    . '" id="n' . $aElements[$rCntr][$value]
411
                                    . '" value="' . $aElements[$rCntr][$value] . '" ';
412
                            if (isset($_REQUEST[$checkboxNameS])) {
413
                                if (is_array($_REQUEST[$checkboxNameS])) {
414
                                    if (in_array($aElements[$rCntr][$value], $_REQUEST[$checkboxNameS])) {
415
                                        $sReturn .= 'checked="checked" ';
416
                                    }
417
                                } else {
418
                                    if ($aElements[$rCntr][$value] == $_REQUEST[$checkboxNameS]) {
419
                                        $sReturn .= 'checked="checked" ';
420
                                    }
421
                                }
422
                            }
423
                            if (strpos($_REQUEST['view'], 'multiEdit') !== false) {
424
                                $sReturn .= 'disabled="disabled" ';
425
                            }
426
                            $sReturn .= '/>';
427
                            break;
428
                        case 'checkbox_inlineEdit':
429
                            $checkboxName  = $value . '[]';
430
                            $checkboxNameS = $value;
431
                            $sReturn .= '&nbsp;<input type="checkbox" name="' . $checkboxName
432
                                    . '" id="n' . $aElements[$rCntr][$value] . '" value="'
433
                                    . $aElements[$rCntr][$value] . '"/>';
434
                            break;
435
                        case 'edit':
436
                            $edt           = '';
437
                            if (isset($ftrs['NoAjaxEditing'])) {
438
                                $edt .= $_SERVER['PHP_SELF'] . '?' . $actPrfx
439
                                        . $action_key . '=' . $value[0] . '&amp;';
440
                                $iActArgs = count($value[1]);
441
                                for ($cntr2 = 0; $cntr2 < $iActArgs; $cntr2++) {
442
                                    $edt .= $value[1][$cntr2] . '=' . $aElements[$rCntr][$value[1][$cntr2]];
443
                                }
444
                                $sReturn .= '<a href="' . $edt . '"><i class="fa fa-pencil">&nbsp;</i></a>';
445 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...
446
                                $edt .= 'javascript:loadAE(\'' . $_SERVER['PHP_SELF'] . '?'
447
                                        . $actPrfx . $action_key . '=' . $value[0] . '&amp;';
448
                                $iActArgs = count($value[1]);
449
                                for ($cntr2 = 0; $cntr2 < $iActArgs; $cntr2++) {
450
                                    $edt .= $value[1][$cntr2] . '=' . $aElements[$rCntr][$value[1][$cntr2]];
451
                                }
452
                                $edt .= '\');';
453
                                $sReturn .= '<a href="#" onclick="' . $edt . '">'
454
                                        . '<i class="fa fa-pencil">&nbsp;</i></a>';
455
                            }
456
                            break;
457
                        case 'list2':
458
                            $edt = '';
459
                            if (isset($ftrs['NoAjaxEditing'])) {
460
                                $sReturn .= '<a href="?' . $actPrfx . $action_key . '=' . $value[0] . '&amp;';
461
                                $iActArgs = count($value[1]);
462
                                for ($cntr2 = 0; $cntr2 < $iActArgs; $cntr2++) {
463
                                    $sReturn .= $value[1][$cntr2] . '=' . $aElements[$rCntr][$value[1][$cntr2]];
464
                                }
465
                                $sReturn .= '"><i class="fa fa-list">&nbsp;</i></a>';
466 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...
467
                                $edt .= 'javascript:loadAE(\'' . $_SERVER['PHP_SELF'] . '?'
468
                                        . $actPrfx . $action_key . '=' . $value[0] . '&amp;';
469
                                $iActArgs = count($value[1]);
470
                                for ($cntr2 = 0; $cntr2 < $iActArgs; $cntr2++) {
471
                                    $edt .= $value[1][$cntr2] . '=' . $aElements[$rCntr][$value[1][$cntr2]];
472
                                }
473
                                $edt .= '\');';
474
                                $sReturn .= '<a href="#" onclick="' . $edt . '">'
475
                                        . '<i class="fa fa-list">&nbsp;</i></a>';
476
                            }
477
                            break;
478
                        case 'delete':
479
                            $sReturn .= '<a href="javascript:setQuest(\'' . $value[0] . '\',\'';
480
                            $iActArgs = count($value[1]);
481
                            for ($cntr2 = 0; $cntr2 < $iActArgs; $cntr2++) {
482
                                $sReturn .= $value[1][$cntr2] . '=' . $aElements[$rCntr][$value[1][$cntr2]];
483
                            }
484
                            $sReturn .= '\');"><i class="fa fa-times">&nbsp;</i></a>';
485
                            break;
486
                    }
487
                    $action_argument += 1;
488
                }
489
                $sReturn .= '</td>';
490
            }
491
// Regular columns
492
            $sReturn .= $this->setTableCell($row_current, $ftrs);
493
// Computed columns
494
            if (isset($ftrs['computed_columns'])) {
495
                foreach ($ftrs['computed_columns'] as $key => $value) {
496
                    if ($value[0] == '%') {
497
                        $dec = $value[2] + 2;
498
                    } else {
499
                        $dec = $value[2];
500
                    }
501
                    switch ($value[1]) {
502
                        case '/':
503
                            // next variable is only to avoid a long line
504
                            $shorter                 = [
505
                                $aElements[$rCntr][$value[3]],
506
                                $aElements[$rCntr][$value[4]],
507
                            ];
508
                            $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...
509
                            break;
510
                        case '+':
511
                            // next variable is only to avoid a long line
512
                            $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...
513
                                $value[0],
514
                                $value[1],
515
                                $value[2]
516
                            ]);
517
                            $aTemp                   = array_diff($value, $iTemp);
518
                            $aElements[$rCntr][$key] = 0;
519
                            foreach ($aTemp as $sValue) {
520
                                $aElements[$rCntr][$key] += $aElements[$rCntr][$sValue];
521
                            }
522
                            break;
523
                        default:
524
                            $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...
525
                            break;
526
                    }
527
                    if ($value[0] == '%') {
528
                        $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...
529
                        $dec -= 2;
530
                    } else {
531
                        $row_computed[$key] = $aElements[$rCntr][$key];
532
                    }
533
                    $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...
534
                }
535
// displaying them
536
                $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...
537
            }
538
            $sReturn .= '</tr>';
539
        }
540 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...
541
            $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...
542
            $sReturn .= '<tr>' . $this->setStringIntoTag($pgn, 'th', ['colspan' => $iTableColumns]) . '</tr>';
543
        }
544
        $sReturn .= '</tbody></table>';
545
        if ($ftrs['grouping_cell_type'] == 'tab') {
546
            if (isset($ftrs['showGroupingCounter'])) {
547
                $sReturn .= $this->updateDivTitleName($remindGroupValue, $groupCounter);
548
            }
549
            $sReturn .= '</div><!-- from ' . $remindGroupValue . ' -->';
550
            if (!isset($ftrs['noGlobalTab'])) {
551
                $sReturn .= '</div><!-- from global tab -->';
552
            }
553
        }
554
        if (isset($ftrs['actions']['checkbox'])) {
555
            if (strpos($_REQUEST['view'], 'multiEdit') === false) {
556
                $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...
557
                        . '\',\'' . $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...
558
                        . '<a href="#" onclick="javascript:checking(\'' . $checkboxFormId
559
                        . '\',\'' . $checkboxName . '\',false);">Uncheck All</a>&nbsp;&nbsp;'
560
                        . '<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...
561 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...
562
                    if (is_array($ftrs['hiddenInput'])) {
563
                        foreach ($ftrs['hiddenInput'] as $valueF) {
564
                            $sReturn .= '<input type="hidden" name="' . $valueF
565
                                    . '" value="' . $_REQUEST[$valueF] . '" />';
566
                        }
567
                    } else {
568
                        $sReturn .= '<input type="hidden" name="' . $ftrs['hiddenInput']
569
                                . '" value="' . $_REQUEST[$ftrs['hiddenInput']] . '" />';
570
                    }
571
                }
572
                $sReturn .= '<input style="margin: 0 3em 0 3em;" type="submit" ' . 'value="Edit selected" />';
573
            }
574
            $sReturn .= '</form>';
575
        }
576
        if (isset($ftrs['actions']['checkbox_inlineEdit'])) {
577
            $sReturn .= '<a href="#" onclick="javascript:checking(\'' . $checkboxFormId
578
                    . '\',\'' . $checkboxName . '\',true);">Check All</a>&nbsp;&nbsp;'
579
                    . '<a href="#" onclick="javascript:checking(\'' . $checkboxFormId
580
                    . '\',\'' . $checkboxName . '\',false);">Uncheck All</a>&nbsp;&nbsp;';
581
            if (isset($ftrs['visibleInput'])) {
582
                $sReturn .= $ftrs['visibleInput'];
583
            }
584
            $sReturn .= '<input type="hidden" name="view" value="save_' . $checkboxNameS . '" />';
585 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...
586
                if (is_array($ftrs['hiddenInput'])) {
587
                    foreach ($ftrs['hiddenInput'] as $valueF) {
588
                        $sReturn .= '<input type="hidden" name="' . $valueF
589
                                . '" value="' . $_REQUEST[$valueF] . '" />';
590
                    }
591
                } else {
592
                    $sReturn .= '<input type="hidden" name="' . $ftrs['hiddenInput']
593
                            . '" value="' . $_REQUEST[$ftrs['hiddenInput']] . '" />';
594
                }
595
            }
596
            $sReturn .= '<input style="margin: 0 3em 0 3em;" type="submit" value="Store the modification" />';
597
            $sReturn .= '</form>';
598
        }
599
        return $sReturn;
600
    }
601
602
    /**
603
     * Set a control to a user-friendly calendar
604
     *
605
     * @param string $controlName
606
     * @param string $additionalStyle
607
     * @return string
608
     */
609 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...
610
    {
611
        return $this->setStringIntoTag('&nbsp;', 'span', [
612
                    'onclick' => implode('', [
613
                        'javascript:NewCssCal(\'' . $controlName,
614
                        '\',\'yyyyMMdd\',\'dropdown\',false,\'24\',false);',
615
                    ]),
616
                    'class'   => 'fa fa-calendar',
617
                    'id'      => $controlName . '_picker',
618
                    'style'   => 'cursor:pointer;' . $additionalStyle,
619
        ]);
620
    }
621
622
    /**
623
     * Set a control to a user-friendly calendar with time included
624
     *
625
     * @param string $controlName
626
     * @param string $additionalStyle
627
     * @return string
628
     */
629 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...
630
    {
631
        return $this->setStringIntoTag('&nbsp;', 'span', [
632
                    'onclick' => implode('', [
633
                        'javascript:NewCssCal(\'' . $controlName,
634
                        '\',\'yyyyMMdd\',\'dropdown\',true,\'24\',true);',
635
                    ]),
636
                    'class'   => 'fa fa-calendar',
637
                    'id'      => $controlName . '_picker',
638
                    'style'   => 'cursor:pointer;' . $additionalStyle,
639
        ]);
640
    }
641
642
    /**
643
     * Cleans a string for certain internal rules
644
     *
645
     * @param type $urlString
646
     * @return type
647
     */
648
    protected function setCleanUrl($urlString)
649
    {
650
        $arrayToReplace = [
651
            '&#038;'    => '&amp;',
652
            '&'         => '&amp;',
653
            '&amp;amp;' => '&amp;',
654
            ' '         => '%20',
655
        ];
656
        $kys            = array_keys($arrayToReplace);
657
        $vls            = array_values($arrayToReplace);
658
        return str_replace($kys, $vls, filter_var($urlString, FILTER_SANITIZE_URL));
659
    }
660
661
    /**
662
     * Returns a div tag that clear any float
663
     *
664
     * @param integer $height
665
     */
666 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...
667
    {
668
        return $this->setStringIntoTag('&nbsp;', 'div', [
669
                    'style' => implode('', [
670
                        'height:' . $height . 'px;',
671
                        'line-height:' . $height . 'px;',
672
                        'float:none;',
673
                        'clear:both;',
674
                        'margin:0px;'
675
                    ])
676
        ]);
677
    }
678
679
    /**
680
     * Returns css codes
681
     *
682
     * @param string $cssContent
683
     * @param array $optionalFlags
684
     * @return string
685
     */
686
    protected function setCssContent($cssContent, $optionalFlags = null)
687
    {
688
        if (is_null($optionalFlags)) {
689
            $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...
690
        } else {
691
            $knownAttributes = ['media'];
692
            foreach ($knownAttributes as $value) {
693
                if (in_array($value, array_keys($optionalFlags))) {
694
                    $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...
695
                }
696
            }
697
        }
698
        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...
699
                . $cssContent
700
                . '</style>';
701
    }
702
703
    /**
704
     * Returns css link to a given file
705
     *
706
     * @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...
707
     * @return string
708
     */
709
    protected function setCssFile($cssFileName, $hostsWithoutCDNrq = null)
710
    {
711
        $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...
712
        if (is_null($hostsWithoutCDNrq)) {
713
            $hostsWithoutCDNrq = [];
714
        }
715
        if (in_array($this->getClientRealIpAddress(), $hostsWithoutCDNrq)) {
716
            $sReturn = '<link rel="stylesheet" type="text/css" href="'
717
                    . filter_var($cssFileName, FILTER_SANITIZE_STRING)
718
                    . '" />';
719
        } else {
720
            $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...
721
            $sReturn      = '<link rel="stylesheet" type="text/css" href="'
722
                    . filter_var($patternFound[1], FILTER_SANITIZE_STRING)
723
                    . '" />';
724
        }
725
        return $sReturn;
726
    }
727
728
    /**
729
     * Builds a structured modern message
730
     *
731
     * @param string $sType
732
     * @param string $sTitle
733
     * @param string $sMsg
734
     * @param boolean $skipBr
735
     */
736
    protected function setFeedbackModern($sType, $sTitle, $sMsg, $skipBr = false)
737
    {
738
        $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...
739
        $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...
740
        switch ($sType) {
741
            case 'alert':
742
                $formatTitle[]   = 'border:medium solid orange;background-color:orange;color:navy;';
743
                $formatMessage[] = 'background-color:navy;color:orange;border:medium solid orange;';
744
                break;
745
            case 'check':
746
                $formatTitle[]   = 'border:medium solid green;background-color:green;color:white;';
747
                $formatMessage[] = 'background-color:yellow;color:green;border:medium solid green;';
748
                break;
749
            case 'error':
750
                $formatTitle[]   = 'border:medium solid red;background-color:red;color:white;';
751
                $formatMessage[] = 'background-color:yellow;color:red;border:medium solid red;';
752
                break;
753
            case 'info':
754
                $formatTitle[]   = 'border:medium solid black;background-color:black;color:white;font-weight:bold;';
755
                $formatMessage[] = 'background-color: white; color: black;border:medium solid black;';
756
                break;
757
        }
758
        if ($sTitle == 'light') {
759
            echo $sMsg;
760
        } else {
761
            $legend = $this->setStringIntoTag($sTitle, 'legend', ['style' => implode('', $formatTitle)]);
762
            return implode('', [
763
                ($skipBr ? '' : '<br/>'),
764
                $this->setStringIntoTag($legend . $sMsg, 'fieldset', [
765
                    'style' => implode('', $formatMessage)
766
                ]),
767
            ]);
768
        }
769
    }
770
771
    /**
772
     * Outputs an HTML footer
773
     *
774
     * @param array $footerInjected
775
     * @return string
776
     */
777
    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...
778
    {
779
        if (isset($_REQUEST['specialHook']) && (in_array('noFooter', $_REQUEST['specialHook']))) {
780
            return '';
781
        }
782
        return $this->setFooterCommonInjected($footerInjected) . '</body></html>';
783
    }
784
785
    protected function setFooterCommonInjected($footerInjected = null)
786
    {
787
        $sReturn = '';
788
        if (!is_null($footerInjected)) {
789
            $sReturn = $footerInjected;
790
            if (is_array($footerInjected)) {
791
                $sReturn = implode('', $footerInjected);
792
            }
793
        }
794
        return $sReturn;
795
    }
796
797
    /**
798
     * Sets the gzip footer for HTML
799
     */
800
    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...
801
    {
802
        if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
803
            if (strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
804
                if (extension_loaded('zlib')) {
805
                    $gzip_contents = ob_get_contents();
806
                    ob_end_clean();
807
                    $gzip_size     = strlen($gzip_contents);
808
                    $gzip_crc      = crc32($gzip_contents);
809
                    $gzip_contents = gzcompress($gzip_contents, 9);
810
                    $gzip_contents = substr($gzip_contents, 0, strlen($gzip_contents) - 4);
811
                    echo "\x1f\x8b\x08\x00\x00\x00\x00\x00";
812
                    echo $gzip_contents;
813
                    echo pack('V', $gzip_crc);
814
                    echo pack('V', $gzip_size);
815
                }
816
            }
817
        }
818
    }
819
820
    /**
821
     * Outputs an HTML header
822
     *
823
     * @param array $headerFeatures
824
     * @return string
825
     */
826
    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...
827
    {
828
        $sReturn = [];
829
        if (isset($_REQUEST['specialHook']) && (in_array('noHeader', $_REQUEST['specialHook']))) {
830
            $sReturn[] = ''; // no Header
831
        } else {
832
            $fixedHeaderElements = [
833
                'start'    => '<!DOCTYPE html>',
834
                'lang'     => '<html lang="en-US">',
835
                'head'     => '<head>',
836
                'charset'  => '<meta charset="utf-8" />',
837
                'viewport' => '<meta name="viewport" content="' . implode(', ', [
838
                    'width=device-width',
839
                    'height=device-height',
840
                    'initial-scale=1',
841
                ]) . '" />',
842
            ];
843
            if (!is_null($headerFeatures)) {
844
                if (is_array($headerFeatures)) {
845
                    $aFeatures = [];
846
                    foreach ($headerFeatures as $key => $value) {
847
                        switch ($key) {
848 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...
849
                                if (is_array($value)) {
850
                                    foreach ($value as $value2) {
851
                                        $aFeatures[] = $this->setCssFile(filter_var($value2, FILTER_SANITIZE_URL));
852
                                    }
853
                                } else {
854
                                    $aFeatures[] = $this->setCssFile(filter_var($value, FILTER_SANITIZE_URL));
855
                                }
856
                                break;
857 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...
858
                                if (is_array($value)) {
859
                                    foreach ($value as $value2) {
860
                                        $vl          = filter_var($value2, FILTER_SANITIZE_URL);
861
                                        $aFeatures[] = $this->setJavascriptFile($vl);
862
                                    }
863
                                } else {
864
                                    $aFeatures[] = $this->setJavascriptFile(filter_var($value, FILTER_SANITIZE_URL));
865
                                }
866
                                break;
867
                            case 'lang':
868
                                $fixedHeaderElements['lang'] = '<html lang="'
869
                                        . filter_var($value, FILTER_SANITIZE_STRING) . '">';
870
                                break;
871
                            case 'title':
872
                                $aFeatures[]                 = '<title>'
873
                                        . filter_var($value, FILTER_SANITIZE_STRING) . '</title>';
874
                                break;
875
                        }
876
                    }
877
                    $sReturn[] = implode('', $fixedHeaderElements)
878
                            . implode('', $aFeatures)
879
                            . '</head>'
880
                            . '<body>';
881
                } else {
882
                    $sReturn[] = implode('', $fixedHeaderElements)
883
                            . '</head>'
884
                            . '<body>'
885
                            . '<p style="background-color:red;color:#FFF;">The parameter sent to '
886
                            . __FUNCTION__ . ' must be an array</p>'
887
                            . $this->setFooterCommon();
888
                    throw new \Exception($sReturn);
889
                }
890
            }
891
        }
892
        return implode('', $sReturn);
893
    }
894
895
    /**
896
     * Sets the gzip header for HTML
897
     */
898
    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...
899
    {
900
        if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
901
            if (strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
902
                if (extension_loaded('zlib')) {
903
                    ob_start();
904
                    ob_implicit_flush(0);
905
                    header('Content-Encoding: gzip');
906
                }
907
            }
908
        }
909
    }
910
911
    /**
912
     * Sets the no-cache header
913
     */
914
    protected function setHeaderNoCache($contentType = 'application/json')
915
    {
916
        header("Content-Type: " . $contentType . "; charset=utf-8");
917
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
918
        header("Cache-Control: no-store, no-cache, must-revalidate");
919
        header("Cache-Control: post-check=0, pre-check=0", false);
920
        header("Pragma: no-cache");
921
    }
922
923
    /**
924
     * Returns javascript function to support Add or Edit through Ajax
925
     *
926
     * @return string
927
     */
928
    protected function setJavascriptAddEditByAjax($tabName = 'tabStandard')
929
    {
930
        return $this->setJavascriptContent(implode('', [
931
                    'function loadAE(action) {',
932
                    'document.getElementById("' . $tabName . '").tabber.tabShow(1);',
933
                    '$("#DynamicAddEditSpacer").load(action',
934
                    '+"&specialHook[]=noHeader"',
935
                    '+"&specialHook[]=noMenu"',
936
                    '+"&specialHook[]=noContainer"',
937
                    '+"&specialHook[]=noFooter"',
938
                    ');',
939
                    '}',
940
        ]));
941
    }
942
943
    /**
944
     * Returns javascript codes
945
     *
946
     * @param string $javascriptContent
947
     * @return string
948
     */
949
    protected function setJavascriptContent($javascriptContent)
950
    {
951
        return '<script type="text/javascript">' . $javascriptContent . '</script>';
952
    }
953
954
    /**
955
     * Builds up a confirmation dialog and return delection if Yes
956
     *
957
     * @return string
958
     */
959
    protected function setJavascriptDeleteWithConfirmation()
960
    {
961
        return $this->setJavascriptContent(' function setQuest(a, b) { '
962
                        . 'c = a.indexOf("_"); switch(a.slice(0, c)) { '
963
                        . 'case \'delete\': '
964
                        . '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...
965
                        . 'window.location = document.location.protocol + "//" + '
966
                        . 'document.location.host + document.location.pathname + '
967
                        . '"?view=" + a + "&" + b; } break; } }');
968
    }
969
970
    /**
971
     * Returns javascript link to a given file
972
     *
973
     * @param string $jsFileName
974
     * @return string
975
     */
976
    protected function setJavascriptFile($jsFileName, $hostsWithoutCDNrq = null)
977
    {
978
        $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...
979
        if (is_null($hostsWithoutCDNrq)) {
980
            $hostsWithoutCDNrq = [];
981
        }
982
        if (in_array($this->getClientRealIpAddress(), $hostsWithoutCDNrq)) {
983
            $sReturn = '<script type="text/javascript" src="' . $jsFileName . '"></script>';
984
        } else {
985
            $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...
986
            $sReturn      = '<script type="text/javascript" src="' . $patternFound[1] . '"></script>'
987
                    . $patternFound[2];
988
        }
989
        return $sReturn;
990
    }
991
992
    /**
993
     * Returns javascript codes from given file
994
     *
995
     * @param string $jsFileName
996
     * @return string
997
     */
998
    protected function setJavascriptFileContent($jsFileName)
999
    {
1000
        $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...
1001
        $sReturn[] = file_get_contents($jsFileName, true);
1002
        $sReturn[] = ' //--></script>';
1003
        return implode('', $sReturn);
1004
    }
1005
1006
    /**
1007
     * Creates all the child tags required to populate a SELECT tag
1008
     *
1009
     * @param array $aElements
1010
     * @param string|array $sDefaultValue
1011
     * @param array $features_array
1012
     * @return string
1013
     */
1014
    private function setOptionsForSelect($aElements, $sDefaultValue, $features_array = [])
1015
    {
1016
        $string2return = '';
1017
        if (is_array($features_array)) {
1018
            if (in_array('include_null', $features_array)) {
1019
                $string2return .= '<option value="NULL">&nbsp;</option>';
1020
            }
1021
            if (isset($features_array['defaultValue_isSubstring'])) {
1022
                $default_value_array = explode($features_array['defaultValue_isSubstring'], $sDefaultValue);
1023
            }
1024
        }
1025
        $current_group = null;
1026
        foreach ($aElements as $key => $value) {
1027
            if (isset($features_array['grouping'])) {
1028
                $temporary_string = substr($value, 0, strpos($value, $features_array['grouping']) + 1);
1029
                if ($current_group != $temporary_string) {
1030
                    if ($current_group != '') {
1031
                        $string2return .= '</optgroup>';
1032
                    }
1033
                    $current_group = $temporary_string;
1034
                    $string2return .= '<optgroup label="'
1035
                            . str_replace($features_array['grouping'], '', $current_group) . '">';
1036
                }
1037
            } else {
1038
                $current_group = '';
1039
            }
1040
            $string2return .= '<option value="' . $key . '"';
1041
            if (is_array($sDefaultValue)) {
1042
                if (in_array($key, $sDefaultValue)) {
1043
                    $string2return .= ' selected="selected"';
1044
                }
1045
            } else {
1046
                if (strcasecmp($key, $sDefaultValue) === 0) {
1047
                    $string2return .= ' selected="selected"';
1048
                }
1049
                if (isset($default_value_array) && is_array($default_value_array)) {
1050
                    if (in_array($key, $default_value_array)) {
1051
                        $string2return .= ' selected="selected"';
1052
                    }
1053
                }
1054
            }
1055
            if (array_key_exists('styleForOption', $features_array)) {
1056
                $string2return .= ' style="' . $features_array['style'] . '"';
1057
            }
1058
            $string2return .= '>' . str_replace(['&', $current_group], ['&amp;', ''], $value) . '</option>';
1059
        }
1060
        if (isset($features_array['grouping'])) {
1061
            if ($current_group != '') {
1062
                $string2return .= '</optgroup>';
1063
            }
1064
        }
1065
        return $string2return;
1066
    }
1067
1068
    /**
1069
     * Puts a given string into a specific short tag
1070
     *
1071
     * @param string $sTag
1072
     * @param array $features
1073
     * @return string
1074
     */
1075
    protected function setStringIntoShortTag($sTag, $features = null)
1076
    {
1077
        $attributes = $this->buildAttributesForTag($features);
0 ignored issues
show
Unused Code introduced by
$attributes 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...
1078
        return '<' . $sTag . $this->buildAttributesForTag($features)
1079
                . (isset($features['dont_close']) ? '' : '/') . '>';
1080
    }
1081
1082
    /**
1083
     * Puts a given string into a specific tag
1084
     *
1085
     * @param string $sString
1086
     * @param string $sTag
1087
     * @param array $features
1088
     * @return string
1089
     */
1090
    protected function setStringIntoTag($sString, $sTag, $features = null)
1091
    {
1092
        return '<' . $sTag . $this->buildAttributesForTag($features) . '>' . $sString . '</' . $sTag . '>';
1093
    }
1094
1095
    /**
1096
     * Generates a table cell
1097
     *
1098
     * @param array $aElements
1099
     * @param array $features
1100
     * @return string
1101
     */
1102
    private function setTableCell($aElements, $features = null)
1103
    {
1104
        $sReturn = null;
1105
        foreach ($aElements as $key => $value) {
1106
            $value = str_replace(['& ', '\"', "\'"], ['&amp; ', '"', "'"], $value);
1107
            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...
1108
                // just skip
1109
            } else {
1110
                $sReturn .= '<td ';
1111
                if (isset($features['column_formatting'][$key])) {
1112
                    switch ($features['column_formatting'][$key]) {
1113
                        case '@':
1114
                            $sReturn .= 'style="text-align:left;">' . $value;
1115
                            break;
1116
                        case 'right':
1117
                            $sReturn .= 'style="text-align:right;">' . $value;
1118
                            break;
1119
                        default:
1120
                            $sReturn .= '???';
1121
                            break;
1122
                    }
1123
                } else {
1124
                    if (is_numeric($value)) {
1125
                        if (substr($value, 0, 1) === '0') {
1126
                            $sReturn .= 'style="text-align: right;">' . $value;
1127
                        } else {
1128
                            $decimals = 0;
1129
                            if (isset($features['no_of_decimals'])) {
1130
                                $decimals = $features['no_of_decimals'];
1131
                            }
1132
                            if (isset($features['decimals']) && array_key_exists($key, $features['decimals'])) {
1133
                                $decimals = $features['decimals'][$key];
1134
                            }
1135
                            $sReturn .= 'style="text-align: right;">';
1136
                            $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...
1137
                                'MinFractionDigits' => $decimals,
1138
                                'MaxFractionDigits' => $decimals
1139
                            ]);
1140
                        }
1141
                    } else {
1142
                        $outputet = false;
1143
                        if ((strpos($value, '-') !== false) && (strlen($value) == 10)) {
1144
                            if (preg_match("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $value, $regs)) {
1145
                                $outputet = true;
1146
                                $sReturn .= 'style="text-align:right;width: 10px;">'
1147
                                        . $regs[3] . '.' . $regs[2] . '.' . $regs[1];
1148
                            }
1149
                        }
1150
                        if (!$outputet) {
1151
                            $sReturn .= 'style="text-align:left;">' . $value;
1152
                        }
1153
                    }
1154
                }
1155
                $sReturn .= '</td>';
1156
            }
1157
        }
1158
        return $sReturn;
1159
    }
1160
1161
    /**
1162
     * Generates a table header
1163
     *
1164
     * @param array $aElements
1165
     * @param boolean $bHeadersBreaked
1166
     * @return string
1167
     */
1168
    private function setTableHeader($aElements, $bHeadersBreaked)
1169
    {
1170
        if ($bHeadersBreaked) {
1171
            $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...
1172
        } else {
1173
            $aTableHeader = $aElements;
1174
        }
1175
        $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...
1176
        foreach (array_keys($aTableHeader) as $value) {
1177
            $sReturn[] = $this->setStringIntoTag($value, 'th');
1178
        }
1179
        return implode('', $sReturn);
1180
    }
1181
1182
    protected function setViewModernLinkAdd($identifier, $ftrs = null)
1183
    {
1184
        $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...
1185
        return $this->setStringIntoTag($btnText, 'a', [
1186
                    'href'  => $this->setViewModernLinkAddUrl($identifier, $ftrs),
1187
                    'style' => 'margin: 5px 0px 10px 0px; display: inline-block;',
1188
        ]);
1189
    }
1190
1191
    protected function setViewModernLinkAddInjectedArguments($ftrs = null)
1192
    {
1193
        $sArgmnts = '';
1194
        if (isset($ftrs['injectAddArguments'])) {
1195
            foreach ($ftrs['injectAddArguments'] as $key => $value) {
1196
                $sArgmnts .= '&amp;' . $key . '=' . $value;
1197
            }
1198
        }
1199
        return $sArgmnts;
1200
    }
1201
1202
    protected function setViewModernLinkAddUrl($identifier, $ftrs = null)
1203
    {
1204
        $sArgmnts  = $this->setViewModernLinkAddInjectedArguments($ftrs);
1205
        $rqst      = new \Symfony\Component\HttpFoundation\Request;
1206
        $addingUrl = $rqst->server->get('PHP_SELF') . '?view=add_' . $identifier . $sArgmnts;
1207
        if (!isset($ftrs['NoAjax'])) {
1208
            $addingUrl = 'javascript:loadAE(\'' . $addingUrl . '\');';
1209
        }
1210
        return $addingUrl;
1211
    }
1212
1213
    protected function updateDivTitleName($rememberGroupVal, $groupCounter)
1214
    {
1215
        $jsContent = '$(document).ready(function() { $("#tab_'
1216
                . $this->cleanStringForId($rememberGroupVal) . '").attr("title", "'
1217
                . $rememberGroupVal . ' (' . $groupCounter . ')"); });';
1218
        return $this->setJavascriptContent($jsContent);
1219
    }
1220
}
1221