InputValidation   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 145
Duplicated Lines 6.21 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 2
dl 9
loc 145
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A applyCurrencyValidations() 0 10 3
A applyYMvalidations() 0 15 2
A buildYMvalues() 0 12 2
A dateRangesInScope() 0 20 2
A determineCrtMinWage() 9 16 4
A establishFilterParameters() 0 20 3
A establishValidValue() 0 15 3
A getValidOption() 0 8 2
A processFormInputDefaults() 0 17 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2017 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
trait InputValidation
32
{
33
34
    private function applyCurrencyValidations(\Symfony\Component\HttpFoundation\Request $tCSG, $defaults, $allAllowed)
35
    {
36
        $valuesCurrency = $tCSG->get('xMoney'); // 0 or 1 or multiple values
37
        if (is_array($valuesCurrency)) {
38
            $tCSG->request->set('xMoney', array_intersect($valuesCurrency, $allAllowed));
39
        }
40
        if (is_null($valuesCurrency)) {
41
            $tCSG->request->set('xMoney', $defaults);
42
        }
43
    }
44
45
    private function applyYMvalidations(\Symfony\Component\HttpFoundation\Request $tCSG, $ymValues, $dtR)
46
    {
47
        $validOpt = [
48
            'options' => [
49
                'default'   => $dtR['default']->format('Ymd'),
50
                'max_range' => $dtR['maximum']->format('Ymd'),
51
                'min_range' => $dtR['minimum']->format('Ymd'),
52
            ]
53
        ];
54
        $validYM  = filter_var($tCSG->get('ym'), FILTER_VALIDATE_INT, $validOpt);
55
        if (!array_key_exists($validYM, $ymValues)) {
56
            $validYM = $validOpt['options']['default'];
57
        }
58
        $tCSG->request->set('ym', $validYM);
59
    }
60
61
    private function buildYMvalues($dtR)
62
    {
63
        $startDate = $dtR['minimum'];
64
        $endDate   = $dtR['maximumYM'];
65
        $temp      = [];
66
        while ($endDate >= $startDate) {
67
            $temp[$endDate->format('Ymd')] = $endDate->format('Y, m (')
68
                . strftime('%B', mktime(0, 0, 0, $endDate->format('n'), 1, $endDate->format('Y'))) . ')';
69
            $endDate->sub(new \DateInterval('P1M'));
70
        }
71
        return $temp;
72
    }
73
74
    private function dateRangesInScope()
75
    {
76
        $defaultDate = new \DateTime('first day of this month');
77
        $maxDate     = new \DateTime('first day of next month');
78
        $maxDateYM   = new \DateTime('first day of next month');
79
        $maxDate->add(new \DateInterval('P1M'));
80
        $maxDateYM->add(new \DateInterval('P1M'));
81
        if (date('d') <= 15) {
82
            $defaultDate->sub(new \DateInterval('P1M'));
83
            $maxDate->sub(new \DateInterval('P1M'));
84
            $maxDateYM->sub(new \DateInterval('P1M'));
85
        }
86
        return [
87
            'default'    => $defaultDate,
88
            'maximum'    => $maxDate,
89
            'maximumInt' => $maxDate->format('Ymd'),
90
            'maximumYM'  => $maxDateYM,
91
            'minimum'    => new \DateTime('2001-01-01'),
92
        ];
93
    }
94
95
    private function determineCrtMinWage(\Symfony\Component\HttpFoundation\Request $tCSG, $inMny)
96
    {
97
        $lngDate    = $tCSG->get('ym');
98
        $intValue   = 0;
99
        $maxCounter = count($inMny['EMW']);
100 View Code Duplication
        for ($counter = 0; $counter < $maxCounter; $counter++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
            $crtVal         = $inMny['EMW'][$counter];
102
            $crtDV          = \DateTime::createFromFormat('Y-n-j', $crtVal['Year'] . '-' . $crtVal['Month'] . '-1');
103
            $crtDateOfValue = (int) $crtDV->format('Ymd');
104
            if (($lngDate <= $inMny['YM range']['maximumInt']) && ($lngDate >= $crtDateOfValue)) {
105
                $intValue = $crtVal['Value'];
106
                $counter  = $maxCounter;
107
            }
108
        }
109
        return $intValue;
110
    }
111
112
    private function establishFilterParameters($inMny, $validOpts)
113
    {
114
        $validation = FILTER_DEFAULT;
115
        switch ($inMny['VFR']['validation_filter']) {
116
            case 'int':
117
                $inVFR                             = $inMny['VFR']['validation_options'];
118
                $validation                        = FILTER_VALIDATE_INT;
119
                $validOpts['options']['max_range'] = $this->getValidOption($inMny['value'], $inVFR, 'max_range');
120
                $validOpts['options']['min_range'] = $this->getValidOption($inMny['value'], $inVFR, 'min_range');
121
                break;
122
            case 'float':
123
                $validOpts['options']['decimal']   = $inMny['VFR']['validation_options']['decimals'];
124
                $validation                        = FILTER_VALIDATE_FLOAT;
125
                break;
126
        }
127
        return [
128
            'validation'         => $validation,
129
            'validation_options' => $validOpts,
130
        ];
131
    }
132
133
    private function establishValidValue(\Symfony\Component\HttpFoundation\Request $tCSG, $key, $inMny)
134
    {
135
        $validOpts                       = [];
136
        $validOpts['options']['default'] = $inMny['value'];
137
        if (in_array($key, ['fb', 'fc'])) {
138
            $validOpts['options']['default'] = $inMny[$key];
139
        }
140
        if (in_array($key, ['sm', 'sn'])) {
141
            $validOpts['options']['default']                 = $inMny['MW'];
142
            $inMny['VFR']['validation_options']['min_range'] = $inMny['MW'];
143
        }
144
        $vOptions   = $this->establishFilterParameters($inMny, $validOpts);
145
        $validValue = filter_var($tCSG->get($key), $vOptions['validation'], $vOptions['validation_options']);
146
        return $validValue;
147
    }
148
149
    private function getValidOption($value, $inValuesFilterRules, $optionLabel)
150
    {
151
        $valReturn = $inValuesFilterRules[$optionLabel];
152
        if ($valReturn == 'default') {
153
            $valReturn = $value;
154
        }
155
        return $valReturn;
156
    }
157
158
    protected function processFormInputDefaults(\Symfony\Component\HttpFoundation\Request $tCSG, $aMultiple)
159
    {
160
        foreach ($aMultiple['VFR'] as $key => $value) {
161
            $validValue = trim($tCSG->get($key));
162
            if (array_key_exists('validation_options', $value)) {
163
                $validValue = $this->establishValidValue($tCSG, $key, [
164
                    'value'    => $aMultiple['VFR'][$key]['default'],
165
                    'MW'       => $aMultiple['MW'],
166
                    'VFR'      => $aMultiple['VFR'][$key],
167
                    'YM range' => $aMultiple['YM range'],
168
                    'fb'       => $aMultiple['fb'],
169
                    'fc'       => $aMultiple['fc'],
170
                ]);
171
            }
172
            $tCSG->request->set($key, $validValue);
173
        }
174
    }
175
}
176