Completed
Push — master ( f72997...dfc376 )
by Daniel
02:30
created

Salariu::buildArrayOfFieldsStyled()   A

Complexity

Conditions 2
Paths 2

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 2
eloc 5
nc 2
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->appFlags    = [
46
            'FI'   => $interfaceElements['Form Input'],
47
            'TCAS' => $interfaceElements['Table Cell Applied Style'],
48
            'TCSD' => $interfaceElements['Table Cell Style Definitions'],
49
        ];
50
        $this->handleLocalizationSalariu($interfaceElements['Application']);
51
        echo $this->setHeaderHtml();
52
        echo $this->setFormInput();
53
        if (isset($_REQUEST['ym'])) {
54
            $this->refreshExchangeRatesFile($interfaceElements['Application']);
55
            $this->setCurrencyExchangeVariables($interfaceElements['Relevant Currencies']);
56
            $this->getExchangeRates($interfaceElements['Application'], $interfaceElements['Relevant Currencies']);
57
            $aryStngs = $this->readSettingsFromJsonFile('valuesToCompute');
58
            echo $this->setFormOutput($aryStngs);
59
        }
60
        echo $this->setFooterHtml($interfaceElements['Application']);
61
    }
62
63
    private function buildArrayOfFieldsStyled()
64
    {
65
        $sReturn = [];
66
        foreach ($this->appFlags['TCAS'] as $key => $value) {
67
            $sReturn[$this->tApp->gettext($key)] = $value;
68
        }
69
        return $sReturn;
70
    }
71
72
    private function buildStyleForCellFormat($styleId)
73
    {
74
        $sReturn = [];
75
        foreach ($this->appFlags['TCSD'][$styleId] as $key => $value) {
76
            $sReturn[] = $key . ':' . $value;
77
        }
78
        return implode(';', $sReturn) . ';';
79
    }
80
81
    private function getExchangeRates($appSettings, $aryRelevantCurrencies)
82
    {
83
        $xml = new \XMLReader();
84
        if ($xml->open($appSettings['Exchange Rate Local'], 'UTF-8')) {
85
            while ($xml->read()) {
86
                if ($xml->nodeType == \XMLReader::ELEMENT) {
87
                    switch ($xml->localName) {
88
                        case 'Cube':
89
                            $this->appFlags['currency_exchange_rate_date'] = strtotime($xml->getAttribute('date'));
90
                            break;
91
                        case 'Rate':
92
                            if (array_key_exists($xml->getAttribute('currency'), $aryRelevantCurrencies)) {
93
                                $cVal = $xml->readInnerXml();
94
                                if (!is_null($xml->getAttribute('multiplier'))) {
95
                                    $cVal = $cVal / $xml->getAttribute('multiplier');
96
                                }
97
                                $this->appFlags['currency_exchange_rate_value'][$xml->getAttribute('currency')] = $cVal;
98
                            }
99
                            break;
100
                    }
101
                }
102
            }
103
            $xml->close();
104
        }
105
    }
106
107
    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...
108
    {
109
        $pcToBoolean = [0 => true, 1 => false];
110
        $mnth        = $this->setMonthlyAverageWorkingHours($_REQUEST['ym'], $aryStngs, $pcToBoolean[$_REQUEST['pc']]);
111
        return [
112
            'os175' => ceil($_REQUEST['os175'] * 1.75 * $_REQUEST['sn'] / $mnth),
113
            'os200' => ceil($_REQUEST['os200'] * 2 * $_REQUEST['sn'] / $mnth),
114
        ];
115
    }
116
117
    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...
118
    {
119
        $inDate           = $_REQUEST['ym'];
120
        $wkDay            = $this->setWorkingDaysInMonth($inDate, $_REQUEST['pc']);
121
        $nMealDays        = ($wkDay - $_REQUEST['zfb']);
122
        $shLbl            = [
123
            'HFP'  => 'Health Fund Percentage',
124
            'HFUL' => 'Health Fund Upper Limit',
125
            'HTP'  => 'Health Tax Percentage',
126
            'IT'   => 'Income Tax',
127
            'MTV'  => 'Meal Ticket Value',
128
        ];
129
        $unemploymentBase = $lngBase;
130
        if ($_REQUEST['ym'] < mktime(0, 0, 0, 1, 1, 2008)) {
131
            $unemploymentBase = $_REQUEST['sn'];
132
        }
133
        $aReturn           = [
134
            'ba'       => $this->setFoodTicketsValue($inDate, $aStngs[$shLbl['MTV']]) * $nMealDays,
135
            'cas'      => $this->setHealthFundTax($inDate, $lngBase, $aStngs[$shLbl['HFP']], $aStngs[$shLbl['HFUL']]),
136
            'sanatate' => $this->setHealthTax($inDate, $lngBase, $aStngs[$shLbl['HTP']]),
137
            'somaj'    => $this->setUnemploymentTax($inDate, $unemploymentBase),
138
        ];
139
        $pdVal             = [
140
            $inDate,
141
            ($lngBase + $aReturn['ba']),
142
            $_REQUEST['pi'],
143
            $aStngs['Personal Deduction'],
144
        ];
145
        $aReturn['pd']     = $this->setPersonalDeduction($pdVal[0], $pdVal[1], $pdVal[2], $pdVal[3]);
146
        $restArrayToDeduct = [
147
            $aReturn['cas'],
148
            $aReturn['sanatate'],
149
            $aReturn['somaj'],
150
            $aReturn['pd'],
151
        ];
152
        $rest              = $lngBase - array_sum($restArrayToDeduct);
153
        if ($inDate >= mktime(0, 0, 0, 7, 1, 2010)) {
154
            $rest += round($aReturn['ba'], -4);
155
            if ($inDate >= mktime(0, 0, 0, 10, 1, 2010)) {
156
                $aReturn['gbns'] = $_REQUEST['gbns'] * pow(10, 4);
157
                $rest            += round($aReturn['gbns'], -4);
158
            }
159
        }
160
        $rest               += $_REQUEST['afet'] * pow(10, 4);
161
        $aReturn['impozit'] = $this->setIncomeTax($inDate, $rest, $aStngs[$shLbl['IT']]);
162
        $aReturn['zile']    = $this->setWorkingDaysInMonth($inDate, $_REQUEST['pc']);
163
        return $aReturn;
164
    }
165
166
    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...
167
    {
168
        if (isset($_GET['lang'])) {
169
            $_SESSION['lang'] = filter_var($_GET['lang'], FILTER_SANITIZE_STRING);
170
        } elseif (!isset($_SESSION['lang'])) {
171
            $_SESSION['lang'] = $appSettings['Default Language'];
172
        }
173
        /* to avoid potential language injections from other applications that do not applies here */
174
        if (!in_array($_SESSION['lang'], array_keys($appSettings['Available Languages']))) {
175
            $_SESSION['lang'] = $appSettings['Default Language'];
176
        }
177
        $localizationFile = 'Salariu/locale/' . $_SESSION['lang'] . '/LC_MESSAGES/salariu.mo';
178
        $translations     = new \Gettext\Translations;
179
        $translations->addFromMoFile($localizationFile);
180
        $this->tApp       = new \Gettext\Translator();
181
        $this->tApp->loadTranslations($translations);
182
    }
183
184
    private function refreshExchangeRatesFile($appSettings)
185
    {
186
        if ((filemtime($appSettings['Exchange Rate Local']) + 90 * 24 * 60 * 60) < time()) {
187
            $fCntnt = file_get_contents($appSettings['Exchange Rate Source']);
188
            if ($fCntnt !== false) {
189
                file_put_contents($appSettings['Exchange Rate Local'], $fCntnt);
190
                chmod($appSettings['Exchange Rate Local'], 0666);
191
            }
192
        }
193
    }
194
195
    private function setCurrencyExchangeVariables($aryRelevantCurrencies)
196
    {
197
        $this->appFlags['currency_exchanges']          = $aryRelevantCurrencies;
198
        $this->appFlags['currency_exchange_rate_date'] = strtotime('now');
199
        $krncy                                         = array_keys($this->appFlags['currency_exchanges']);
200
        foreach ($krncy as $value) {
201
            $this->appFlags['currency_exchange_rate_value'][$value] = 1;
202
        }
203
    }
204
205
    private function setFooterHtml($appSettings)
206
    {
207
        $sReturn = $this->setUpperRightBoxLanguages($appSettings['Available Languages'])
208
                . '<div class="resetOnly author">&copy; ' . date('Y') . ' '
209
                . $appSettings['Copyright Holder'] . '</div>'
210
                . '<hr/>'
211
                . '<div class="disclaimer">'
212
                . $this->tApp->gettext('i18n_Disclaimer')
213
                . '</div>';
214
        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...
215
    }
216
217
    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...
218
    {
219
        $sReturn      = [];
220
        $sReturn[]    = $this->setFormRow($this->setLabel('ym'), $this->setFormInputSelectYM(), 1);
221
        $sReturn[]    = $this->setFormRow($this->setLabel('sn'), $this->setFormInputText('sn', 10, 'RON'), 1);
222
        $sReturn[]    = $this->setFormRow($this->setLabel('sc'), $this->setFormInputText('sc', 2, '%'), 1);
223
        $sReturn[]    = $this->setFormRow($this->setLabel('pb'), $this->setFormInputText('pb', 10, 'RON'), 1);
224
        $sReturn[]    = $this->setFormRow($this->setLabel('pn'), $this->setFormInputText('pn', 10, 'RON'), 1);
225
        $sReturn[]    = $this->setFormRow($this->setLabel('os175'), $this->setFormInputText('os175', 2, ''), 1);
226
        $sReturn[]    = $this->setFormRow($this->setLabel('os200'), $this->setFormInputText('os200', 2, ''), 1);
227
        $sReturn[]    = $this->setFormRow($this->setLabel('pi'), $this->setFormInputSelectPI(), 1);
228
        $sReturn[]    = $this->setFormRow($this->setLabel('pc'), $this->setFormInputSelectPC(), 1);
229
        $sReturn[]    = $this->setFormRow($this->setLabel('szamnt'), $this->setFormInputText('szamnt', 2, ''), 1);
230
        $sReturn[]    = $this->setFormRow($this->setLabel('zfb'), $this->setFormInputText('zfb', 2, ''), 1);
231
        $sReturn[]    = $this->setFormRow($this->setLabel('gbns'), $this->setFormInputText('gbns', 2, ''), 1);
232
        $sReturn[]    = $this->setFormRow($this->setLabel('afet'), $this->setFormInputText('afet', 2, ''), 1);
233
        $label        = $this->tApp->gettext('i18n_Form_Disclaimer');
234
        $hiddenField  = $this->setStringIntoShortTag('input', [
235
            'type'  => 'hidden',
236
            'name'  => 'action',
237
            'value' => $_SERVER['SERVER_NAME']
238
        ]);
239
        $sReturn[]    = $this->setStringIntoTag($this->setStringIntoTag($label . $hiddenField, 'td', [
240
                    'colspan' => 2,
241
                    'style'   => 'color: red;'
242
                ]), 'tr');
243
        $resetBtn     = $this->setStringIntoShortTag('input', [
244
            'type'  => 'reset',
245
            'id'    => 'reset',
246
            'value' => $this->tApp->gettext('i18n_Form_Button_Reset'),
247
            'style' => 'color:#000;'
248
        ]);
249
        $submitBtnTxt = $this->tApp->gettext('i18n_Form_Button_Calculate');
250
        if (isset($_REQUEST['ym'])) {
251
            $resetBtn     = '';
252
            $submitBtnTxt = $this->tApp->gettext('i18n_Form_Button_Recalculate');
253
        }
254
        $sReturn[] = $this->setFormRow($resetBtn, $this->setStringIntoShortTag('input', [
255
                    'type'  => 'submit',
256
                    'id'    => 'submit',
257
                    'value' => $submitBtnTxt
258
                ]), 1);
259
        $frm       = $this->setStringIntoTag($this->setStringIntoTag(implode('', $sReturn), 'table'), 'form', [
260
            'method' => 'get',
261
            'action' => $_SERVER['SCRIPT_NAME']
262
        ]);
263
        return $this->setStringIntoTag(implode('', [
264
                    $this->setStringIntoTag($this->tApp->gettext('i18n_FieldsetLabel_Inputs'), 'legend'),
265
                    $frm
266
                        ]), 'fieldset', ['style' => 'float: left;']);
267
    }
268
269
    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...
270
    {
271
        $choices = [
272
            $this->tApp->gettext('i18n_Form_Label_CatholicEasterFree_ChoiceNo'),
273
            $this->tApp->gettext('i18n_Form_Label_CatholicEasterFree_ChoiceYes'),
274
        ];
275
        return $this->setArrayToSelect($choices, $_REQUEST['pc'], 'pc', ['size' => 1]);
276
    }
277
278
    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...
279
    {
280
        $temp2 = [];
281
        for ($counter = 0; $counter <= 4; $counter++) {
282
            $temp2[$counter] = $counter . ($counter == 4 ? '+' : '');
283
        }
284
        return $this->setArrayToSelect($temp2, $_REQUEST['pi'], 'pi', ['size' => 1]);
285
    }
286
287
    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...
288
    {
289
        $temp = [];
290
        for ($counter = date('Y'); $counter >= 2001; $counter--) {
291
            for ($counter2 = 12; $counter2 >= 1; $counter2--) {
292
                $crtDate = mktime(0, 0, 0, $counter2, 1, $counter);
293
                if ($crtDate <= mktime(0, 0, 0, date('m'), 1, date('Y'))) {
294
                    $temp[$crtDate] = strftime('%Y, %m (%B)', $crtDate);
295
                }
296
            }
297
        }
298
        return $this->setArrayToSelect($temp, $_REQUEST['ym'], 'ym', ['size' => 1]);
299
    }
300
301
    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...
302
    {
303
        $inputParameters = [
304
            'type'      => 'text',
305
            'name'      => $inName,
306
            'value'     => $_REQUEST[$inName],
307
            'size'      => $inSize,
308
            'maxlength' => $inSize,
309
        ];
310
        return $this->setStringIntoShortTag('input', $inputParameters) . ' ' . $inAfterLabel;
311
    }
312
313
    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...
314
    {
315
        $sReturn   = [];
316
        $overtime  = $this->getOvertimes($aryStngs['Monthly Average Working Hours']);
317
        $additions = $_REQUEST['pb'] + $overtime['os175'] + $overtime['os200'];
318
        $brut      = ($_REQUEST['sn'] * (1 + $_REQUEST['sc'] / 100) + $additions) * pow(10, 4);
319
        $text      = $this->tApp->gettext('i18n_Form_Label_ExchangeRateAtDate');
320
        $xRate     = str_replace('%1', date('d.m.Y', $this->appFlags['currency_exchange_rate_date']), $text);
321
        $sReturn[] = $this->setFormRow($xRate, 1000000);
322
        $text      = $this->tApp->gettext('i18n_Form_Label_NegotiatedSalary');
323
        $sReturn[] = $this->setFormRow($text, $_REQUEST['sn'] * 10000);
324
        $prima     = $_REQUEST['sn'] * $_REQUEST['sc'] * 100;
325
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_CumulatedAddedValue'), $prima);
326
        $text      = $this->tApp->gettext('i18n_Form_Label_AdditionalBruttoAmount');
327
        $sReturn[] = $this->setFormRow($text, $_REQUEST['pb'] * 10000);
328
        $ovTime    = [
329
            'main' => $this->tApp->gettext('i18n_Form_Label_OvertimeAmount'),
330
            1      => $this->tApp->gettext('i18n_Form_Label_OvertimeChoice1'),
331
            2      => $this->tApp->gettext('i18n_Form_Label_OvertimeChoice2'),
332
        ];
333
        $sReturn[] = $this->setFormRow(sprintf($ovTime['main'], $ovTime[1], '175%'), ($overtime['os175'] * pow(10, 4)));
334
        $sReturn[] = $this->setFormRow(sprintf($ovTime['main'], $ovTime[2], '200%'), ($overtime['os200'] * pow(10, 4)));
335
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_BruttoSalary'), $brut);
336
        $brut      += $_REQUEST['afet'] * pow(10, 4);
337
        $amount    = $this->getValues($brut, $aryStngs);
338
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_PensionFund'), $amount['cas']);
339
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_UnemploymentTax'), $amount['somaj']);
340
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_HealthTax'), $amount['sanatate']);
341
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_PersonalDeduction'), $amount['pd']);
342
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_ExciseTax'), $amount['impozit']);
343
        $retineri  = $amount['cas'] + $amount['somaj'] + $amount['sanatate'] + $amount['impozit'];
344
        $net       = $brut - $retineri + $_REQUEST['pn'] * 10000;
345
        $text      = $this->tApp->gettext('i18n_Form_Label_AdditionalNettoAmount');
346
        $sReturn[] = $this->setFormRow($text, $_REQUEST['pn'] * 10000);
347
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_NettoSalary'), $net);
348
        $text      = $this->tApp->gettext('i18n_Form_Label_SeisureAmout');
349
        $sReturn[] = $this->setFormRow($text, $_REQUEST['szamnt'] * 10000);
350
        $text      = $this->tApp->gettext('i18n_Form_Label_NettoSalaryCash');
351
        $sReturn[] = $this->setFormRow($text, ($net - $_REQUEST['szamnt'] * 10000));
352
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_WorkingDays'), $amount['zile'], 'value');
353
        $fBonus    = [
354
            'main'  => $this->tApp->gettext('i18n_Form_Label_FoodBonuses'),
355
            'no'    => $this->tApp->gettext('i18n_Form_Label_FoodBonusesChoiceNo'),
356
            'value' => $this->tApp->gettext('i18n_Form_Label_FoodBonusesChoiceValue')
357
        ];
358
        $fBonusTxt = sprintf($fBonus['main'], $fBonus['value'], $fBonus['no'], ($amount['zile'] - $_REQUEST['zfb']));
359
        $sReturn[] = $this->setFormRow($fBonusTxt, $amount['ba']);
360
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_FoodBonusesValue'), $amount['gbns']);
361
        $total     = ($net + $amount['ba'] + $amount['gbns'] - $_REQUEST['szamnt'] * 10000);
362
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_Total'), $total);
363
        setlocale(LC_TIME, explode('_', $_SESSION['lang'])[0]);
364
        $crtMonth  = strftime('%B', $_REQUEST['ym']);
365
        $legend    = sprintf($this->tApp->gettext('i18n_FieldsetLabel_Results'), $crtMonth, date('Y', $_REQUEST['ym']));
366
        return $this->setStringIntoTag(implode('', [
367
                    $this->setStringIntoTag($legend, 'legend'),
368
                    $this->setStringIntoTag(implode('', $sReturn), 'table')
369
                        ]), 'fieldset', ['style' => 'float: left;']);
370
    }
371
372
    private function setFormRow($text, $value, $type = 'amount')
373
    {
374
        $a                 = '';
375
        $defaultCellStyle  = $this->setFormatRow($text);
376
        $defaultCellStyle2 = [];
377
        switch ($type) {
378
            case 'amount':
379
                $value                      = $value / pow(10, 4);
380
                $defaultCellStyle2['style'] = $defaultCellStyle['style'] . 'text-align:right;';
381
                $cellValue                  = [];
382
                foreach ($this->appFlags['currency_exchanges'] as $key2 => $value2) {
383
                    $fmt         = new \NumberFormatter($value2['locale'], \NumberFormatter::CURRENCY);
384
                    $fmt->setAttribute(\NumberFormatter::FRACTION_DIGITS, $value2['decimals']);
385
                    $x           = $this->appFlags['currency_exchange_rate_value'][$key2];
386
                    $finalValue  = $fmt->formatCurrency($value / $x, $key2);
387
                    $cellValue[] = $this->setStringIntoTag($finalValue, 'td', $defaultCellStyle2);
388
                }
389
                $value2show        = implode('', $cellValue);
390
                break;
391
            case 'value':
392
                $defaultCellStyle2 = array_merge($defaultCellStyle, [
393
                    'colspan' => count($this->appFlags['currency_exchanges'])
394
                ]);
395
                $value2show        = $this->setStringIntoTag($value . $a, 'td', $defaultCellStyle2);
396
                break;
397
            default:
398
                $value2show        = $this->setStringIntoTag($value, 'td');
399
                break;
400
        }
401
        if (!in_array($text, ['', '&nbsp;']) && (strpos($text, '<input') === false)) {
402
            $text .= ':';
403
        }
404
        return $this->setStringIntoTag($this->setStringIntoTag($text, 'td', $defaultCellStyle) . $value2show, 'tr');
405
    }
406
407
    private function setFormatRow($text)
408
    {
409
        $defaultCellStyle = [
410
            'class' => 'labelS',
411
        ];
412
        $fieldsStyled     = $this->buildArrayOfFieldsStyled();
413
        if (array_key_exists($text, $fieldsStyled)) {
414
            $defaultCellStyle['style'] = $this->buildStyleForCellFormat($fieldsStyled[$text]);
415
        }
416
        if ((is_numeric($value)) && ($value == 0)) {
0 ignored issues
show
Bug introduced by
The variable $value does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
417
            $defaultCellStyle['style'] = 'color:#666;';
418
        }
419
        return $defaultCellStyle;
420
    }
421
422
    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...
423
    {
424
        $headerParameters = [
425
            'lang'  => str_replace('_', '-', $_SESSION['lang']),
426
            'title' => $this->tApp->gettext('i18n_ApplicationName'),
427
            'css'   => [
428
                'vendor/components/flag-icon-css/css/flag-icon.min.css',
429
                'Salariu/css/salariu.css',
430
            ],
431
        ];
432
        return $this->setHeaderCommon($headerParameters)
433
                . '<h1>' . $this->tApp->gettext('i18n_ApplicationName') . '</h1>';
434
    }
435
436
    private function setLabel($labelId)
437
    {
438
        $labelInfo = $this->appFlags['FI'][$labelId]['Label'];
439
        $sReturn   = '';
440
        if (is_array($labelInfo)) {
441
            if (count($labelInfo) == 3) {
442
                $pieces  = [
443
                    $this->tApp->gettext($labelInfo[0]),
444
                    $this->tApp->gettext($labelInfo[1]),
445
                ];
446
                $sReturn = sprintf($pieces[0], $pieces[1], $labelInfo[2]);
447
            }
448
        } elseif (is_string($labelInfo)) {
449
            $sReturn = $this->tApp->gettext($labelInfo);
450
        }
451
        return $sReturn;
452
    }
453
}
454