Completed
Push — master ( dcb10a...8251f2 )
by Daniel
02:25
created

Salariu::refreshExchangeRatesFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 7
nc 3
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
        $this->appFlags = [
45
            'available_languages' => [
46
                'en_US' => 'US English',
47
                'ro_RO' => 'Română',
48
            ],
49
            'default_language'    => 'ro_RO'
50
        ];
51
        $this->handleLocalizationSalariu();
52
        echo $this->setHeaderHtml();
53
        echo $this->setFormInput();
54
        if (isset($_REQUEST['ym'])) {
55
            $this->refreshExchangeRatesFile();
56
            $aryStngs = $this->readSettingsFromJsonFile('static');
57
            $this->getExchangeRates($aryStngs['Relevant Currencies']);
58
            echo $this->setFormOutput($aryStngs);
59
        }
60
        echo $this->setFooterHtml();
61
    }
62
63
    private function getExchangeRates($aryRelevantCurrencies)
64
    {
65
        $this->appFlags['currency_exchanges']          = $aryRelevantCurrencies;
66
        $this->appFlags['currency_exchange_rate_date'] = strtotime('now');
67
        $krncy                                         = array_keys($this->appFlags['currency_exchanges']);
68
        foreach ($krncy as $value) {
69
            $this->appFlags['currency_exchange_rate_value'][$value] = 1;
70
        }
71
        $xml   = new \XMLReader();
72
        $xFile = EXCHANGE_RATES_LOCAL;
73
        if ($xml->open($xFile, 'UTF-8')) {
74
            while ($xml->read()) {
75
                if ($xml->nodeType == \XMLReader::ELEMENT) {
76
                    switch ($xml->localName) {
77
                        case 'Cube':
78
                            $this->appFlags['currency_exchange_rate_date'] = strtotime($xml->getAttribute('date'));
79
                            break;
80
                        case 'Rate':
81
                            if (in_array($xml->getAttribute('currency'), $krncy)) {
82
                                $cncy = $xml->getAttribute('currency');
83
                                $cVal = $xml->readInnerXml();
84
                                if (!is_null($xml->getAttribute('multiplier'))) {
85
                                    $cVal = $cVal / $xml->getAttribute('multiplier');
86
                                }
87
                                $this->appFlags['currency_exchange_rate_value'][$cncy] = $cVal;
88
                            }
89
                            break;
90
                    }
91
                }
92
            }
93
            $xml->close();
94
        } else {
95
            $erLast = error_get_last();
96
            echo '<div style="background-color: red; color: white;">'
97
            . utf8_encode(json_encode($erLast, JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT))
98
            . '</div>';
99
        }
100
    }
101
102
    private function getOvertimes()
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...
103
    {
104
        $mnth = 1;
105
        switch ($_REQUEST['pc']) {
106
            case 0:
107
                $mnth = $this->setMonthlyAverageWorkingHours($_REQUEST['ym'], true);
108
                break;
109
            case 1:
110
                $mnth = $this->setMonthlyAverageWorkingHours($_REQUEST['ym']);
111
                break;
112
        }
113
        return [
114
            'os175' => ceil($_REQUEST['os175'] * 1.75 * $_REQUEST['sn'] / $mnth),
115
            'os200' => ceil($_REQUEST['os200'] * 2 * $_REQUEST['sn'] / $mnth),
116
        ];
117
    }
118
119
    private function getValues($lngBase, $aryStngs)
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...
120
    {
121
        $wkDay            = $this->setWorkingDaysInMonth($_REQUEST['ym'], $_REQUEST['pc']);
122
        $nMealDays        = ($wkDay - $_REQUEST['zfb']);
123
        $unemploymentBase = $lngBase;
124
        if ($_REQUEST['ym'] < mktime(0, 0, 0, 1, 1, 2008)) {
125
            $unemploymentBase = $_REQUEST['sn'];
126
        }
127
        $aReturn           = [
128
            'ba'       => $this->setFoodTicketsValue($_REQUEST['ym'], $aryStngs['Meal Ticket Value']) * $nMealDays,
129
            'cas'      => $this->setHealthFundTax($_REQUEST['ym'], $lngBase),
130
            'sanatate' => $this->setHealthTax($_REQUEST['ym'], $lngBase),
131
            'somaj'    => $this->setUnemploymentTax($_REQUEST['ym'], $unemploymentBase),
132
        ];
133
        $pdVal             = [
134
            $_REQUEST['ym'],
135
            ($lngBase + $aReturn['ba']),
136
            $_REQUEST['pi'],
137
            $aryStngs['Personal Deduction'],
138
        ];
139
        $aReturn['pd']     = $this->setPersonalDeduction($pdVal[0], $pdVal[1], $pdVal[2], $pdVal[3]);
140
        $restArrayToDeduct = [
141
            $aReturn['cas'],
142
            $aReturn['sanatate'],
143
            $aReturn['somaj'],
144
            $aReturn['pd'],
145
        ];
146
        $rest              = $lngBase - array_sum($restArrayToDeduct);
147
        if ($_REQUEST['ym'] >= mktime(0, 0, 0, 7, 1, 2010)) {
148
            $rest += round($aReturn['ba'], -4);
149
        }
150
        if ($_REQUEST['ym'] >= mktime(0, 0, 0, 10, 1, 2010)) {
151
            $aReturn['gbns'] = $_REQUEST['gbns'] * pow(10, 4);
152
            $rest            += round($aReturn['gbns'], -4);
153
        }
154
        $rest               += $_REQUEST['afet'] * pow(10, 4);
155
        $aReturn['impozit'] = $this->setIncomeTax($_REQUEST['ym'], $rest);
156
        $aReturn['zile']    = $this->setWorkingDaysInMonth($_REQUEST['ym'], $_REQUEST['pc']);
157
        return $aReturn;
158
    }
159
160
    private function handleLocalizationSalariu()
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...
161
    {
162
        if (isset($_GET['lang'])) {
163
            $_SESSION['lang'] = filter_var($_GET['lang'], FILTER_SANITIZE_STRING);
164
        } elseif (!isset($_SESSION['lang'])) {
165
            $_SESSION['lang'] = $this->appFlags['default_language'];
166
        }
167
        /* to avoid potential language injections from other applications that do not applies here */
168
        if (!in_array($_SESSION['lang'], array_keys($this->appFlags['available_languages']))) {
169
            $_SESSION['lang'] = $this->appFlags['default_language'];
170
        }
171
        $localizationFile = 'Salariu/locale/' . $_SESSION['lang'] . '/LC_MESSAGES/salariu.mo';
172
        $translations     = new \Gettext\Translations;
173
        $translations->addFromMoFile($localizationFile);
174
        $this->tApp       = new \Gettext\Translator();
175
        $this->tApp->loadTranslations($translations);
176
    }
177
178
    private function refreshExchangeRatesFile()
179
    {
180
        if ((filemtime(EXCHANGE_RATES_LOCAL) + 90 * 24 * 60 * 60) < time()) {
181
            $xFile  = EXCHANGE_RATES_SOURCE;
0 ignored issues
show
Unused Code introduced by
$xFile is not used, you could remove the assignment.

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

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

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

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

Loading history...
182
            $fCntnt = file_get_contents(EXCHANGE_RATES_SOURCE);
183
            if ($fCntnt !== false) {
184
                chmod(EXCHANGE_RATES_LOCAL, 0666);
185
                file_put_contents(EXCHANGE_RATES_LOCAL, $fCntnt);
186
            }
187
        }
188
    }
189
190
    private function setFooterHtml()
191
    {
192
        $sReturn   = [];
193
        $sReturn[] = $this->setUpperRightBoxLanguages($this->appFlags['available_languages']);
194
        $sReturn[] = '<div class="resetOnly author">&copy; 2015 Daniel Popiniuc</div>';
195
        $sReturn[] = '<hr/>';
196
        $sReturn[] = '<div class="disclaimer">'
197
                . $this->tApp->gettext('i18n_Disclaimer')
198
                . '</div>';
199
        return $this->setFooterCommon(implode('', $sReturn));
0 ignored issues
show
Documentation introduced by
implode('', $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...
200
    }
201
202
    private function setFormInput()
0 ignored issues
show
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...
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...
203
    {
204
        $label     = $this->tApp->gettext('i18n_Form_Label_CalculationMonth');
205
        $sReturn[] = $this->setFormRow($label, $this->setFormInputSelect(), 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...
206
        $label     = $this->tApp->gettext('i18n_Form_Label_NegotiatedSalary');
207
        $sReturn[] = $this->setFormRow($label, $this->setStringIntoShortTag('input', [
208
                    'type'  => 'text',
209
                    'name'  => 'sn',
210
                    'value' => $_REQUEST['sn'],
211
                    'size'  => 10
212
                ]) . ' RON', 1);
213
        $label     = $this->tApp->gettext('i18n_Form_Label_CumulatedAddedValue');
214
        $sReturn[] = $this->setFormRow($label, $this->setStringIntoShortTag('input', [
215
                    'type'  => 'text',
216
                    'name'  => 'sc',
217
                    'value' => $_REQUEST['sc'],
218
                    'size'  => 2
219
                ]) . ' %', 1);
220
        $label     = $this->tApp->gettext('i18n_Form_Label_AdditionalBruttoAmount');
221
        $sReturn[] = $this->setFormRow($label, $this->setStringIntoShortTag('input', [
222
                    'type'  => 'text',
223
                    'name'  => 'pb',
224
                    'value' => $_REQUEST['pb'],
225
                    'size'  => 10
226
                ]) . ' RON', 1);
227
        $label     = $this->tApp->gettext('i18n_Form_Label_AdditionalNettoAmount');
228
        $sReturn[] = $this->setFormRow($label, $this->setStringIntoShortTag('input', [
229
                    'type'  => 'text',
230
                    'name'  => 'pn',
231
                    'value' => $_REQUEST['pn'],
232
                    'size'  => 10
233
                ]) . ' RON', 1);
234
        $pieces    = [
235
            $this->tApp->gettext('i18n_Form_Label_OvertimeHours'),
236
            $this->tApp->gettext('i18n_Form_Label_OvertimeChoice1'),
237
        ];
238
        $label     = sprintf($pieces[0], $pieces[1], '175%');
239
        $sReturn[] = $this->setFormRow($label, $this->setStringIntoShortTag('input', [
240
                    'type'  => 'text',
241
                    'name'  => 'os175',
242
                    'value' => $_REQUEST['os175'],
243
                    'size'  => 2
244
                ]), 1);
245
        $pieces    = [
246
            $this->tApp->gettext('i18n_Form_Label_OvertimeHours'),
247
            $this->tApp->gettext('i18n_Form_Label_OvertimeChoice2'),
248
        ];
249
        $label     = sprintf($pieces[0], $pieces[1], '200%');
250
        $sReturn[] = $this->setFormRow($label, $this->setStringIntoShortTag('input', [
251
                    'type'  => 'text',
252
                    'name'  => 'os200',
253
                    'value' => $_REQUEST['os200'],
254
                    'size'  => 2
255
                ]), 1);
256
        $temp2     = [];
257
        for ($counter = 0; $counter <= 4; $counter++) {
258
            $temp2[] = $counter;
259
        }
260
        $selectTemp = $this->setArrayToSelect($temp2, $_REQUEST['pi'], 'pi', ['size' => 1]);
261
        $sReturn[]  = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_PersonsSupported'), $selectTemp, 1);
262
        $choices    = [
263
            $this->tApp->gettext('i18n_Form_Label_CatholicEasterFree_ChoiceNo'),
264
            $this->tApp->gettext('i18n_Form_Label_CatholicEasterFree_ChoiceYes'),
265
        ];
266
        $label      = $this->tApp->gettext('i18n_Form_Label_CatholicEasterFree');
267
        $select     = $this->setArrayToSelect($choices, $_REQUEST['pc'], 'pc', ['size' => 1]);
268
        $sReturn[]  = $this->setFormRow($label, $select, 1);
269
        $label      = $this->tApp->gettext('i18n_Form_Label_SeisureAmout');
270
        $sReturn[]  = $this->setFormRow($label, $this->setStringIntoShortTag('input', [
271
                    'type'  => 'text',
272
                    'name'  => 'szamnt',
273
                    'value' => $_REQUEST['szamnt'],
274
                    'size'  => 10
275
                ]), 1);
276
        $label      = $this->tApp->gettext('i18n_Form_Label_WorkedDaysWithoutFoodBonuses');
277
        $sReturn[]  = $this->setFormRow($label, $this->setStringIntoShortTag('input', [
278
                    'type'  => 'text',
279
                    'name'  => 'zfb',
280
                    'value' => $_REQUEST['zfb'],
281
                    'size'  => 2
282
                ]), 1);
283
        $label      = $this->tApp->gettext('i18n_Form_Label_FoodBonusesValue');
284
        $sReturn[]  = $this->setFormRow($label, $this->setStringIntoShortTag('input', [
285
                    'type'  => 'text',
286
                    'name'  => 'gbns',
287
                    'value' => $_REQUEST['gbns'],
288
                    'size'  => 2]), 1);
289
        $label      = $this->tApp->gettext('i18n_Form_Label_AdvantagesForExciseTax');
290
        $sReturn[]  = $this->setFormRow($label, $this->setStringIntoShortTag('input', [
291
                    'type'  => 'text',
292
                    'name'  => 'afet',
293
                    'value' => $_REQUEST['afet'],
294
                    'size'  => 2]), 1);
295
        $label      = $this->tApp->gettext('i18n_Form_Disclaimer');
296
        $sReturn[]  = $this->setStringIntoTag($this->setStringIntoTag($label . $this->setStringIntoShortTag('input', [
297
                            'type'  => 'hidden',
298
                            'name'  => 'action',
299
                            'value' => $_SERVER['SERVER_NAME']
300
                        ]), 'td', ['colspan' => 2, 'style' => 'color: red;']), 'tr');
301
        if (isset($_REQUEST['ym'])) {
302
            $resetBtn     = '';
303
            $submitBtnTxt = $this->tApp->gettext('i18n_Form_Button_Recalculate');
304
        } else {
305
            $resetBtn     = $this->setStringIntoShortTag('input', [
306
                'type'  => 'reset',
307
                'id'    => 'reset',
308
                'value' => $this->tApp->gettext('i18n_Form_Button_Reset'),
309
                'style' => 'color:#000;'
310
            ]);
311
            $submitBtnTxt = $this->tApp->gettext('i18n_Form_Button_Calculate');
312
        }
313
        $sReturn[] = $this->setFormRow($resetBtn, $this->setStringIntoShortTag('input', [
314
                    'type'  => 'submit',
315
                    'id'    => 'submit',
316
                    'value' => $submitBtnTxt
317
                ]), 1);
318
        $frm       = $this->setStringIntoTag($this->setStringIntoTag(implode('', $sReturn), 'table'), 'form', [
319
            'method' => 'get',
320
            'action' => $_SERVER['SCRIPT_NAME']
321
        ]);
322
        return $this->setStringIntoTag(implode('', [
323
                    $this->setStringIntoTag($this->tApp->gettext('i18n_FieldsetLabel_Inputs'), 'legend'),
324
                    $frm
325
                        ]), 'fieldset', ['style' => 'float: left;']);
326
    }
327
328
    private function setFormInputSelect()
0 ignored issues
show
Coding Style introduced by
setFormInputSelect 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...
329
    {
330
        $temp = [];
331
        for ($counter = date('Y'); $counter >= 2001; $counter--) {
332
            for ($counter2 = 12; $counter2 >= 1; $counter2--) {
333
                if (($counter == date('Y')) && ($counter2 > date('m'))) {
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...
334
                    # se limiteaza pana la luna curenta
335
                } else {
336
                    $crtDate        = mktime(0, 0, 0, $counter2, 1, $counter);
337
                    $temp[$crtDate] = strftime('%Y, %m (%B)', $crtDate);
338
                }
339
            }
340
        }
341
        return $this->setArrayToSelect($temp, $_REQUEST['ym'], 'ym', ['size' => 1]);
342
    }
343
344
    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...
345
    {
346
        $overtime  = $this->getOvertimes();
347
        $additions = $_REQUEST['pb'] + $overtime['os175'] + $overtime['os200'];
348
        $brut      = ($_REQUEST['sn'] * (1 + $_REQUEST['sc'] / 100) + $additions) * pow(10, 4);
349
        $text      = $this->tApp->gettext('i18n_Form_Label_ExchangeRateAtDate');
350
        $xRate     = str_replace('%1', date('d.m.Y', $this->appFlags['currency_exchange_rate_date']), $text);
351
        $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...
352
        $text      = $this->tApp->gettext('i18n_Form_Label_NegotiatedSalary');
353
        $sReturn[] = $this->setFormRow($text, $_REQUEST['sn'] * 10000);
354
        $prima     = $_REQUEST['sn'] * $_REQUEST['sc'] * 100;
355
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_CumulatedAddedValue'), $prima);
356
        $text      = $this->tApp->gettext('i18n_Form_Label_AdditionalBruttoAmount');
357
        $sReturn[] = $this->setFormRow($text, $_REQUEST['pb'] * 10000);
358
        $ovTime    = [
359
            'main' => $this->tApp->gettext('i18n_Form_Label_OvertimeAmount'),
360
            1      => $this->tApp->gettext('i18n_Form_Label_OvertimeChoice1'),
361
            2      => $this->tApp->gettext('i18n_Form_Label_OvertimeChoice2'),
362
        ];
363
        $sReturn[] = $this->setFormRow(sprintf($ovTime['main'], $ovTime[1], '175%'), ($overtime['os175'] * pow(10, 4)));
364
        $sReturn[] = $this->setFormRow(sprintf($ovTime['main'], $ovTime[2], '200%'), ($overtime['os200'] * pow(10, 4)));
365
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_BruttoSalary'), $brut);
366
        $brut      += $_REQUEST['afet'] * pow(10, 4);
367
        $amount    = $this->getValues($brut, $aryStngs);
368
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_PensionFund'), $amount['cas']);
369
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_UnemploymentTax'), $amount['somaj']);
370
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_HealthTax'), $amount['sanatate']);
371
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_PersonalDeduction'), $amount['pd']);
372
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_ExciseTax'), $amount['impozit']);
373
        $retineri  = $amount['cas'] + $amount['somaj'] + $amount['sanatate'] + $amount['impozit'];
374
        $net       = $brut - $retineri + $_REQUEST['pn'] * 10000;
375
        $text      = $this->tApp->gettext('i18n_Form_Label_AdditionalNettoAmount');
376
        $sReturn[] = $this->setFormRow($text, $_REQUEST['pn'] * 10000);
377
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_NettoSalary'), $net);
378
        $text      = $this->tApp->gettext('i18n_Form_Label_SeisureAmout');
379
        $sReturn[] = $this->setFormRow($text, $_REQUEST['szamnt'] * 10000);
380
        $text      = $this->tApp->gettext('i18n_Form_Label_NettoSalaryCash');
381
        $sReturn[] = $this->setFormRow($text, ($net - $_REQUEST['szamnt'] * 10000));
382
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_WorkingDays'), $amount['zile'], 'value');
383
        $fBonus    = [
384
            'main'  => $this->tApp->gettext('i18n_Form_Label_FoodBonuses'),
385
            'no'    => $this->tApp->gettext('i18n_Form_Label_FoodBonusesChoiceNo'),
386
            'value' => $this->tApp->gettext('i18n_Form_Label_FoodBonusesChoiceValue')
387
        ];
388
        $fBonusTxt = sprintf($fBonus['main'], $fBonus['value'], $fBonus['no'], ($amount['zile'] - $_REQUEST['zfb']));
389
        $sReturn[] = $this->setFormRow($fBonusTxt, $amount['ba']);
390
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_FoodBonusesValue'), $amount['gbns']);
391
        $total     = ($net + $amount['ba'] + $amount['gbns'] - $_REQUEST['szamnt'] * 10000);
392
        $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_Total'), $total);
393
        setlocale(LC_TIME, explode('_', $_SESSION['lang'])[0]);
394
        $crtMonth  = strftime('%B', $_REQUEST['ym']);
395
        $legend    = sprintf($this->tApp->gettext('i18n_FieldsetLabel_Results'), $crtMonth, date('Y', $_REQUEST['ym']));
396
        return $this->setStringIntoTag(implode('', [
397
                    $this->setStringIntoTag($legend, 'legend'),
398
                    $this->setStringIntoTag(implode('', $sReturn), 'table')
399
                        ]), 'fieldset', ['style' => 'float: left;']);
400
    }
401
402
    private function setFormRow($text, $value, $type = 'amount')
403
    {
404
        $a                = '';
405
        $defaultCellStyle = ['class' => 'labelS'];
406
        switch ($text) {
407
            case $this->tApp->gettext('i18n_Form_Label_NegotiatedSalary'):
408
            case $this->tApp->gettext('i18n_Form_Label_BruttoSalary'):
409
            case $this->tApp->gettext('i18n_Form_Label_NettoSalaryCash'):
410
                $defaultCellStyle = array_merge($defaultCellStyle, [
411
                    'style' => 'color:#000000;font-weight:bold;'
412
                ]);
413
                break;
414
            case $this->tApp->gettext('i18n_Form_Label_SeisureAmout'):
415
            case $this->tApp->gettext('i18n_Form_Label_PensionFund'):
416
            case $this->tApp->gettext('i18n_Form_Label_HealthTax'):
417
            case $this->tApp->gettext('i18n_Form_Label_UnemploymentTax'):
418
            case $this->tApp->gettext('i18n_Form_Label_ExciseTax'):
419
                $defaultCellStyle = array_merge($defaultCellStyle, [
420
                    'style' => 'color:#ff9900;'
421
                ]);
422
                break;
423
            case $this->tApp->gettext('i18n_Form_Label_Total'):
424
                $defaultCellStyle = array_merge($defaultCellStyle, [
425
                    'style' => 'font-weight:bold;color:#009933;font-size:larger;'
426
                ]);
427
                break;
428
        }
429
        $defaultCellStyle['style'] = '';
430
        if ((is_numeric($value)) && ($value == 0)) {
431
            if (isset($defaultCellStyle['style'])) {
432
                $defaultCellStyle['style'] = 'color:#666;';
433
            } else {
434
                $defaultCellStyle = array_merge($defaultCellStyle, [
435
                    'style' => 'color:#666;'
436
                ]);
437
            }
438
        }
439
        switch ($type) {
440
            case 'amount':
441
                $value                      = $value / pow(10, 4);
442
                $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...
443
                $cellValue                  = [];
444
                foreach ($this->appFlags['currency_exchanges'] as $key2 => $value2) {
445
                    $fmt         = new \NumberFormatter($value2['locale'], \NumberFormatter::CURRENCY);
446
                    $fmt->setAttribute(\NumberFormatter::FRACTION_DIGITS, $value2['decimals']);
447
                    $x           = $this->appFlags['currency_exchange_rate_value'][$key2];
448
                    $finalValue  = $fmt->formatCurrency($value / $x, $key2);
449
                    $cellValue[] = $this->setStringIntoTag($finalValue, 'td', $defaultCellStyle2);
450
                }
451
                $value2show        = implode('', $cellValue);
452
                break;
453
            case 'value':
454
                $defaultCellStyle2 = array_merge($defaultCellStyle, [
455
                    'colspan' => count($this->appFlags['currency_exchanges'])
456
                ]);
457
                $value2show        = $this->setStringIntoTag($value . $a, 'td', $defaultCellStyle2);
458
                break;
459
            default:
460
                $value2show        = $this->setStringIntoTag($value, 'td');
461
                break;
462
        }
463
        if (!in_array($text, ['', '&nbsp;']) && (strpos($text, '<input') === false)) {
464
            $text .= ':';
465
        }
466
        return $this->setStringIntoTag($this->setStringIntoTag($text, 'td', $defaultCellStyle) . $value2show, 'tr');
467
    }
468
469
    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...
470
    {
471
        return $this->setHeaderCommon([
472
                    'lang'  => str_replace('_', '-', $_SESSION['lang']),
473
                    'title' => $this->tApp->gettext('i18n_ApplicationName'),
474
                    'css'   => [
475
                        'vendor/components/flag-icon-css/css/flag-icon.min.css',
476
                        'Salariu/css/salariu.css',
477
                    ],
478
                ])
479
                . '<h1>' . $this->tApp->gettext('i18n_ApplicationName') . '</h1>'
480
        ;
481
    }
482
}
483