Completed
Push — master ( 0c60ee...51a211 )
by Daniel
02:16
created

Salariu::setFormInputSelectPC()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2016 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\salariu;
30
31
class Salariu
32
{
33
34
    use \danielgp\bank_holidays\Romanian,
35
        \danielgp\common_lib\CommonCode,
36
        \danielgp\salariu\Bonuses,
37
        \danielgp\salariu\Taxation;
38
39
    private $appFlags;
40
    private $tApp = null;
41
42
    public function __construct()
0 ignored issues
show
Coding Style introduced by
__construct 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...
43
    {
44
        $interfaceElements    = $this->readSettingsFromJsonFile('interfaceElements');
45
        $this->handleLocalizationSalariu($interfaceElements['Application']);
46
        echo $this->setHeaderHtml();
47
        $this->appFlags['FI'] = $interfaceElements['Form Input'];
48
        echo $this->setFormInput();
49
        if (isset($_REQUEST['ym'])) {
50
            $this->refreshExchangeRatesFile($interfaceElements['Application']);
51
            $this->setCurrencyExchangeVariables($interfaceElements['Relevant Currencies']);
52
            $this->getExchangeRates($interfaceElements['Application']);
53
            $aryStngs = $this->readSettingsFromJsonFile('valuesToCompute');
54
            echo $this->setFormOutput($aryStngs);
55
        }
56
        echo $this->setFooterHtml($interfaceElements['Application']);
57
    }
58
59
    private function getExchangeRates($appSettings)
60
    {
61
        $xml = new \XMLReader();
62
        if ($xml->open($appSettings['Exchange Rate Local'], 'UTF-8')) {
63
            $krncy = array_keys($this->appFlags['currency_exchanges']);
64
            while ($xml->read()) {
65
                if ($xml->nodeType == \XMLReader::ELEMENT) {
66
                    switch ($xml->localName) {
67
                        case 'Cube':
68
                            $this->appFlags['currency_exchange_rate_date'] = strtotime($xml->getAttribute('date'));
69
                            break;
70
                        case 'Rate':
71
                            if (in_array($xml->getAttribute('currency'), $krncy)) {
72
                                $cncy = $xml->getAttribute('currency');
73
                                $cVal = $xml->readInnerXml();
74
                                if (!is_null($xml->getAttribute('multiplier'))) {
75
                                    $cVal = $cVal / $xml->getAttribute('multiplier');
76
                                }
77
                                $this->appFlags['currency_exchange_rate_value'][$cncy] = $cVal;
78
                            }
79
                            break;
80
                    }
81
                }
82
            }
83
            $xml->close();
84
        }
85
    }
86
87
    private function getOvertimes($aryStngs)
0 ignored issues
show
Coding Style introduced by
getOvertimes 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...
88
    {
89
        $pcToBoolean = [0 => true, 1 => false];
90
        $mnth        = $this->setMonthlyAverageWorkingHours($_REQUEST['ym'], $aryStngs, $pcToBoolean[$_REQUEST['pc']]);
91
        return [
92
            'os175' => ceil($_REQUEST['os175'] * 1.75 * $_REQUEST['sn'] / $mnth),
93
            'os200' => ceil($_REQUEST['os200'] * 2 * $_REQUEST['sn'] / $mnth),
94
        ];
95
    }
96
97
    private function getValues($lngBase, $aStngs)
0 ignored issues
show
Coding Style introduced by
getValues 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...
98
    {
99
        $inDate           = $_REQUEST['ym'];
100
        $wkDay            = $this->setWorkingDaysInMonth($inDate, $_REQUEST['pc']);
101
        $nMealDays        = ($wkDay - $_REQUEST['zfb']);
102
        $shLbl            = [
103
            'HFP'  => 'Health Fund Percentage',
104
            'HFUL' => 'Health Fund Upper Limit',
105
            'HTP'  => 'Health Tax Percentage',
106
            'IT'   => 'Income Tax',
107
            'MTV'  => 'Meal Ticket Value',
108
        ];
109
        $unemploymentBase = $lngBase;
110
        if ($_REQUEST['ym'] < mktime(0, 0, 0, 1, 1, 2008)) {
111
            $unemploymentBase = $_REQUEST['sn'];
112
        }
113
        $aReturn           = [
114
            'ba'       => $this->setFoodTicketsValue($inDate, $aStngs[$shLbl['MTV']]) * $nMealDays,
115
            'cas'      => $this->setHealthFundTax($inDate, $lngBase, $aStngs[$shLbl['HFP']], $aStngs[$shLbl['HFUL']]),
116
            'sanatate' => $this->setHealthTax($inDate, $lngBase, $aStngs[$shLbl['HTP']]),
117
            'somaj'    => $this->setUnemploymentTax($inDate, $unemploymentBase),
118
        ];
119
        $pdVal             = [
120
            $inDate,
121
            ($lngBase + $aReturn['ba']),
122
            $_REQUEST['pi'],
123
            $aStngs['Personal Deduction'],
124
        ];
125
        $aReturn['pd']     = $this->setPersonalDeduction($pdVal[0], $pdVal[1], $pdVal[2], $pdVal[3]);
126
        $restArrayToDeduct = [
127
            $aReturn['cas'],
128
            $aReturn['sanatate'],
129
            $aReturn['somaj'],
130
            $aReturn['pd'],
131
        ];
132
        $rest              = $lngBase - array_sum($restArrayToDeduct);
133
        if ($inDate >= mktime(0, 0, 0, 7, 1, 2010)) {
134
            $rest += round($aReturn['ba'], -4);
135
            if ($inDate >= mktime(0, 0, 0, 10, 1, 2010)) {
136
                $aReturn['gbns'] = $_REQUEST['gbns'] * pow(10, 4);
137
                $rest            += round($aReturn['gbns'], -4);
138
            }
139
        }
140
        $rest               += $_REQUEST['afet'] * pow(10, 4);
141
        $aReturn['impozit'] = $this->setIncomeTax($inDate, $rest, $aStngs[$shLbl['IT']]);
142
        $aReturn['zile']    = $this->setWorkingDaysInMonth($inDate, $_REQUEST['pc']);
143
        return $aReturn;
144
    }
145
146
    private function handleLocalizationSalariu($appSettings)
0 ignored issues
show
Coding Style introduced by
handleLocalizationSalariu uses the super-global variable $_GET 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
handleLocalizationSalariu uses the super-global variable $_SESSION 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...
147
    {
148
        if (isset($_GET['lang'])) {
149
            $_SESSION['lang'] = filter_var($_GET['lang'], FILTER_SANITIZE_STRING);
150
        } elseif (!isset($_SESSION['lang'])) {
151
            $_SESSION['lang'] = $appSettings['Default Language'];
152
        }
153
        /* to avoid potential language injections from other applications that do not applies here */
154
        if (!in_array($_SESSION['lang'], array_keys($appSettings['Available Languages']))) {
155
            $_SESSION['lang'] = $appSettings['Default Language'];
156
        }
157
        $localizationFile = 'Salariu/locale/' . $_SESSION['lang'] . '/LC_MESSAGES/salariu.mo';
158
        $translations     = new \Gettext\Translations;
159
        $translations->addFromMoFile($localizationFile);
160
        $this->tApp       = new \Gettext\Translator();
161
        $this->tApp->loadTranslations($translations);
162
    }
163
164
    private function refreshExchangeRatesFile($appSettings)
165
    {
166
        if ((filemtime($appSettings['Exchange Rate Local']) + 90 * 24 * 60 * 60) < time()) {
167
            $fCntnt = file_get_contents($appSettings['Exchange Rate Source']);
168
            if ($fCntnt !== false) {
169
                file_put_contents($appSettings['Exchange Rate Local'], $fCntnt);
170
                chmod($appSettings['Exchange Rate Local'], 0666);
171
            }
172
        }
173
    }
174
175
    private function setCurrencyExchangeVariables($aryRelevantCurrencies)
176
    {
177
        $this->appFlags['currency_exchanges']          = $aryRelevantCurrencies;
178
        $this->appFlags['currency_exchange_rate_date'] = strtotime('now');
179
        $krncy                                         = array_keys($this->appFlags['currency_exchanges']);
180
        foreach ($krncy as $value) {
181
            $this->appFlags['currency_exchange_rate_value'][$value] = 1;
182
        }
183
    }
184
185
    private function setFooterHtml($appSettings)
186
    {
187
        $sReturn = $this->setUpperRightBoxLanguages($appSettings['Available Languages'])
188
                . '<div class="resetOnly author">&copy; ' . date('Y') . ' '
189
                . $appSettings['Copyright Holder'] . '</div>'
190
                . '<hr/>'
191
                . '<div class="disclaimer">'
192
                . $this->tApp->gettext('i18n_Disclaimer')
193
                . '</div>';
194
        return $this->setFooterCommon($sReturn);
0 ignored issues
show
Documentation introduced by
$sReturn is of type string, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
195
    }
196
197
    private function setFormInput()
0 ignored issues
show
Coding Style introduced by
setFormInput 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
setFormInput 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...
198
    {
199
        $sReturn[]    = $this->setFormRow($this->setLabel('ym'), $this->setFormInputSelectYM(), 1);
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...
200
        $sReturn[]    = $this->setFormRow($this->setLabel('sn'), $this->setFormInputText('sn', 10, 'RON'), 1);
201
        $sReturn[]    = $this->setFormRow($this->setLabel('sc'), $this->setFormInputText('sc', 2, '%'), 1);
202
        $sReturn[]    = $this->setFormRow($this->setLabel('pb'), $this->setFormInputText('pb', 10, 'RON'), 1);
203
        $sReturn[]    = $this->setFormRow($this->setLabel('pn'), $this->setFormInputText('pn', 10, 'RON'), 1);
204
        $sReturn[]    = $this->setFormRow($this->setLabel('os175'), $this->setFormInputText('os175', 2, ''), 1);
205
        $sReturn[]    = $this->setFormRow($this->setLabel('os200'), $this->setFormInputText('os200', 2, ''), 1);
206
        $sReturn[]    = $this->setFormRow($this->setLabel('pi'), $this->setFormInputSelectPI(), 1);
207
        $sReturn[]    = $this->setFormRow($this->setLabel('pc'), $this->setFormInputSelectPC(), 1);
208
        $sReturn[]    = $this->setFormRow($this->setLabel('szamnt'), $this->setFormInputText('szamnt', 2, ''), 1);
209
        $sReturn[]    = $this->setFormRow($this->setLabel('zfb'), $this->setFormInputText('zfb', 2, ''), 1);
210
        $sReturn[]    = $this->setFormRow($this->setLabel('gbns'), $this->setFormInputText('gbns', 2, ''), 1);
211
        $sReturn[]    = $this->setFormRow($this->setLabel('afet'), $this->setFormInputText('afet', 2, ''), 1);
212
        $label        = $this->tApp->gettext('i18n_Form_Disclaimer');
213
        $hiddenField  = $this->setStringIntoShortTag('input', [
214
            'type'  => 'hidden',
215
            'name'  => 'action',
216
            'value' => $_SERVER['SERVER_NAME']
217
        ]);
218
        $sReturn[]    = $this->setStringIntoTag($this->setStringIntoTag($label . $hiddenField, 'td', [
219
                    'colspan' => 2,
220
                    'style'   => 'color: red;'
221
                ]), 'tr');
222
        $resetBtn     = $this->setStringIntoShortTag('input', [
223
            'type'  => 'reset',
224
            'id'    => 'reset',
225
            'value' => $this->tApp->gettext('i18n_Form_Button_Reset'),
226
            'style' => 'color:#000;'
227
        ]);
228
        $submitBtnTxt = $this->tApp->gettext('i18n_Form_Button_Calculate');
229
        if (isset($_REQUEST['ym'])) {
230
            $resetBtn     = '';
231
            $submitBtnTxt = $this->tApp->gettext('i18n_Form_Button_Recalculate');
232
        }
233
        $sReturn[] = $this->setFormRow($resetBtn, $this->setStringIntoShortTag('input', [
234
                    'type'  => 'submit',
235
                    'id'    => 'submit',
236
                    'value' => $submitBtnTxt
237
                ]), 1);
238
        $frm       = $this->setStringIntoTag($this->setStringIntoTag(implode('', $sReturn), 'table'), 'form', [
239
            'method' => 'get',
240
            'action' => $_SERVER['SCRIPT_NAME']
241
        ]);
242
        return $this->setStringIntoTag(implode('', [
243
                    $this->setStringIntoTag($this->tApp->gettext('i18n_FieldsetLabel_Inputs'), 'legend'),
244
                    $frm
245
                        ]), 'fieldset', ['style' => 'float: left;']);
246
    }
247
248
    private function setFormInputSelectPC()
0 ignored issues
show
Coding Style introduced by
setFormInputSelectPC 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...
249
    {
250
        $choices = [
251
            $this->tApp->gettext('i18n_Form_Label_CatholicEasterFree_ChoiceNo'),
252
            $this->tApp->gettext('i18n_Form_Label_CatholicEasterFree_ChoiceYes'),
253
        ];
254
        return $this->setArrayToSelect($choices, $_REQUEST['pc'], 'pc', ['size' => 1]);
255
    }
256
257
    private function setFormInputSelectPI()
0 ignored issues
show
Coding Style introduced by
setFormInputSelectPI 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...
258
    {
259
        $temp2 = [];
260
        for ($counter = 0; $counter <= 4; $counter++) {
261
            $temp2[$counter] = $counter . ($counter == 4 ? '+' : '');
262
        }
263
        return $this->setArrayToSelect($temp2, $_REQUEST['pi'], 'pi', ['size' => 1]);
264
    }
265
266
    private function setFormInputSelectYM()
0 ignored issues
show
Coding Style introduced by
setFormInputSelectYM 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...
267
    {
268
        $temp = [];
269
        for ($counter = date('Y'); $counter >= 2001; $counter--) {
270
            for ($counter2 = 12; $counter2 >= 1; $counter2--) {
271
                $crtDate = mktime(0, 0, 0, $counter2, 1, $counter);
272
                if ($crtDate <= mktime(0, 0, 0, date('m'), 1, date('Y'))) {
273
                    $temp[$crtDate] = strftime('%Y, %m (%B)', $crtDate);
274
                }
275
            }
276
        }
277
        return $this->setArrayToSelect($temp, $_REQUEST['ym'], 'ym', ['size' => 1]);
278
    }
279
280
    private function setFormInputText($inName, $inSize, $inAfterLabel)
0 ignored issues
show
Coding Style introduced by
setFormInputText 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...
281
    {
282
        $inputParameters = [
283
            'type'      => 'text',
284
            'name'      => $inName,
285
            'value'     => $_REQUEST[$inName],
286
            'size'      => $inSize,
287
            'maxlength' => $inSize,
288
        ];
289
        return $this->setStringIntoShortTag('input', $inputParameters) . ' ' . $inAfterLabel;
290
    }
291
292
    private function setFormOutput($aryStngs)
0 ignored issues
show
Coding Style introduced by
setFormOutput 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...
Coding Style introduced by
setFormOutput uses the super-global variable $_SESSION 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...
293
    {
294
        $overtime  = $this->getOvertimes($aryStngs['Monthly Average Working Hours']);
295
        $additions = $_REQUEST['pb'] + $overtime['os175'] + $overtime['os200'];
296
        $brut      = ($_REQUEST['sn'] * (1 + $_REQUEST['sc'] / 100) + $additions) * pow(10, 4);
297
        $text      = $this->tApp->gettext('i18n_Form_Label_ExchangeRateAtDate');
298
        $xRate     = str_replace('%1', date('d.m.Y', $this->appFlags['currency_exchange_rate_date']), $text);
299
        $sReturn[] = $this->setFormRow($xRate, 1000000);
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...
300
        $text      = $this->tApp->gettext('i18n_Form_Label_NegotiatedSalary');
301
        $sReturn[] = $this->setFormRow($text, $_REQUEST['sn'] * 10000);
302
        $prima     = $_REQUEST['sn'] * $_REQUEST['sc'] * 100;
303
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_CumulatedAddedValue'), $prima);
304
        $text      = $this->tApp->gettext('i18n_Form_Label_AdditionalBruttoAmount');
305
        $sReturn[] = $this->setFormRow($text, $_REQUEST['pb'] * 10000);
306
        $ovTime    = [
307
            'main' => $this->tApp->gettext('i18n_Form_Label_OvertimeAmount'),
308
            1      => $this->tApp->gettext('i18n_Form_Label_OvertimeChoice1'),
309
            2      => $this->tApp->gettext('i18n_Form_Label_OvertimeChoice2'),
310
        ];
311
        $sReturn[] = $this->setFormRow(sprintf($ovTime['main'], $ovTime[1], '175%'), ($overtime['os175'] * pow(10, 4)));
312
        $sReturn[] = $this->setFormRow(sprintf($ovTime['main'], $ovTime[2], '200%'), ($overtime['os200'] * pow(10, 4)));
313
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_BruttoSalary'), $brut);
314
        $brut      += $_REQUEST['afet'] * pow(10, 4);
315
        $amount    = $this->getValues($brut, $aryStngs);
316
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_PensionFund'), $amount['cas']);
317
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_UnemploymentTax'), $amount['somaj']);
318
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_HealthTax'), $amount['sanatate']);
319
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_PersonalDeduction'), $amount['pd']);
320
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_ExciseTax'), $amount['impozit']);
321
        $retineri  = $amount['cas'] + $amount['somaj'] + $amount['sanatate'] + $amount['impozit'];
322
        $net       = $brut - $retineri + $_REQUEST['pn'] * 10000;
323
        $text      = $this->tApp->gettext('i18n_Form_Label_AdditionalNettoAmount');
324
        $sReturn[] = $this->setFormRow($text, $_REQUEST['pn'] * 10000);
325
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_NettoSalary'), $net);
326
        $text      = $this->tApp->gettext('i18n_Form_Label_SeisureAmout');
327
        $sReturn[] = $this->setFormRow($text, $_REQUEST['szamnt'] * 10000);
328
        $text      = $this->tApp->gettext('i18n_Form_Label_NettoSalaryCash');
329
        $sReturn[] = $this->setFormRow($text, ($net - $_REQUEST['szamnt'] * 10000));
330
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_WorkingDays'), $amount['zile'], 'value');
331
        $fBonus    = [
332
            'main'  => $this->tApp->gettext('i18n_Form_Label_FoodBonuses'),
333
            'no'    => $this->tApp->gettext('i18n_Form_Label_FoodBonusesChoiceNo'),
334
            'value' => $this->tApp->gettext('i18n_Form_Label_FoodBonusesChoiceValue')
335
        ];
336
        $fBonusTxt = sprintf($fBonus['main'], $fBonus['value'], $fBonus['no'], ($amount['zile'] - $_REQUEST['zfb']));
337
        $sReturn[] = $this->setFormRow($fBonusTxt, $amount['ba']);
338
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_FoodBonusesValue'), $amount['gbns']);
339
        $total     = ($net + $amount['ba'] + $amount['gbns'] - $_REQUEST['szamnt'] * 10000);
340
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_Total'), $total);
341
        setlocale(LC_TIME, explode('_', $_SESSION['lang'])[0]);
342
        $crtMonth  = strftime('%B', $_REQUEST['ym']);
343
        $legend    = sprintf($this->tApp->gettext('i18n_FieldsetLabel_Results'), $crtMonth, date('Y', $_REQUEST['ym']));
344
        return $this->setStringIntoTag(implode('', [
345
                    $this->setStringIntoTag($legend, 'legend'),
346
                    $this->setStringIntoTag(implode('', $sReturn), 'table')
347
                        ]), 'fieldset', ['style' => 'float: left;']);
348
    }
349
350
    private function setFormRow($text, $value, $type = 'amount')
351
    {
352
        $a                = '';
353
        $defaultCellStyle = ['class' => 'labelS'];
354
        switch ($text) {
355
            case $this->tApp->gettext('i18n_Form_Label_NegotiatedSalary'):
356
            case $this->tApp->gettext('i18n_Form_Label_BruttoSalary'):
357
            case $this->tApp->gettext('i18n_Form_Label_NettoSalaryCash'):
358
                $defaultCellStyle = array_merge($defaultCellStyle, [
359
                    'style' => 'color:#000000;font-weight:bold;'
360
                ]);
361
                break;
362
            case $this->tApp->gettext('i18n_Form_Label_SeisureAmout'):
363
            case $this->tApp->gettext('i18n_Form_Label_PensionFund'):
364
            case $this->tApp->gettext('i18n_Form_Label_HealthTax'):
365
            case $this->tApp->gettext('i18n_Form_Label_UnemploymentTax'):
366
            case $this->tApp->gettext('i18n_Form_Label_ExciseTax'):
367
                $defaultCellStyle = array_merge($defaultCellStyle, [
368
                    'style' => 'color:#ff9900;'
369
                ]);
370
                break;
371
            case $this->tApp->gettext('i18n_Form_Label_Total'):
372
                $defaultCellStyle = array_merge($defaultCellStyle, [
373
                    'style' => 'font-weight:bold;color:#009933;font-size:larger;'
374
                ]);
375
                break;
376
        }
377
        $defaultCellStyle['style'] = '';
378
        if ((is_numeric($value)) && ($value == 0)) {
379
            if (isset($defaultCellStyle['style'])) {
380
                $defaultCellStyle['style'] = 'color:#666;';
381
            } else {
382
                $defaultCellStyle = array_merge($defaultCellStyle, [
383
                    'style' => 'color:#666;'
384
                ]);
385
            }
386
        }
387
        switch ($type) {
388
            case 'amount':
389
                $value                      = $value / pow(10, 4);
390
                $defaultCellStyle2['style'] = $defaultCellStyle['style'] . 'text-align:right;';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$defaultCellStyle2 was never initialized. Although not strictly required by PHP, it is generally a good practice to add $defaultCellStyle2 = 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...
391
                $cellValue                  = [];
392
                foreach ($this->appFlags['currency_exchanges'] as $key2 => $value2) {
393
                    $fmt         = new \NumberFormatter($value2['locale'], \NumberFormatter::CURRENCY);
394
                    $fmt->setAttribute(\NumberFormatter::FRACTION_DIGITS, $value2['decimals']);
395
                    $x           = $this->appFlags['currency_exchange_rate_value'][$key2];
396
                    $finalValue  = $fmt->formatCurrency($value / $x, $key2);
397
                    $cellValue[] = $this->setStringIntoTag($finalValue, 'td', $defaultCellStyle2);
398
                }
399
                $value2show        = implode('', $cellValue);
400
                break;
401
            case 'value':
402
                $defaultCellStyle2 = array_merge($defaultCellStyle, [
403
                    'colspan' => count($this->appFlags['currency_exchanges'])
404
                ]);
405
                $value2show        = $this->setStringIntoTag($value . $a, 'td', $defaultCellStyle2);
406
                break;
407
            default:
408
                $value2show        = $this->setStringIntoTag($value, 'td');
409
                break;
410
        }
411
        if (!in_array($text, ['', '&nbsp;']) && (strpos($text, '<input') === false)) {
412
            $text .= ':';
413
        }
414
        return $this->setStringIntoTag($this->setStringIntoTag($text, 'td', $defaultCellStyle) . $value2show, 'tr');
415
    }
416
417
    private function setHeaderHtml()
0 ignored issues
show
Coding Style introduced by
setHeaderHtml uses the super-global variable $_SESSION 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...
418
    {
419
        $headerParameters = [
420
            'lang'  => str_replace('_', '-', $_SESSION['lang']),
421
            'title' => $this->tApp->gettext('i18n_ApplicationName'),
422
            'css'   => [
423
                'vendor/components/flag-icon-css/css/flag-icon.min.css',
424
                'Salariu/css/salariu.css',
425
            ],
426
        ];
427
        return $this->setHeaderCommon($headerParameters)
428
                . '<h1>' . $this->tApp->gettext('i18n_ApplicationName') . '</h1>';
429
    }
430
431
    private function setLabel($labelId)
432
    {
433
        $labelInfo = $this->appFlags['FI'][$labelId]['Label'];
434
        if (is_array($labelInfo)) {
435
            if (count($labelInfo) == 3) {
436
                $pieces  = [
437
                    $this->tApp->gettext($labelInfo[0]),
438
                    $this->tApp->gettext($labelInfo[1]),
439
                ];
440
                $sReturn = sprintf($pieces[0], $pieces[1], $labelInfo[2]);
441
            }
442
        } elseif (is_string($labelInfo)) {
443
            $sReturn = $this->tApp->gettext($labelInfo);
444
        }
445
        return $sReturn;
0 ignored issues
show
Bug introduced by
The variable $sReturn 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...
446
    }
447
}
448