Completed
Push — master ( 45724e...d399d9 )
by Daniel
06:49
created

DomComponentsByDanielGP::setCssContent()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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