Completed
Push — master ( bf4509...0aeec2 )
by Daniel
03:40
created

InputValidation::applyYMvalidations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 8
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
trait InputValidation
32
{
33
34
    private function applyYMvalidations()
35
    {
36
        $validOpt = [
37
            'options' => [
38
                'default'   => mktime(0, 0, 0, date('m'), 1, date('Y')),
39
                'max_range' => mktime(0, 0, 0, date('m'), 1, date('Y')),
40
                'min_range' => mktime(0, 0, 0, 1, 1, 2001),
41
            ]
42
        ];
43
        $validYM  = filter_var($this->tCmnSuperGlobals->request->get('ym'), FILTER_VALIDATE_INT, $validOpt);
0 ignored issues
show
Bug introduced by
The property tCmnSuperGlobals does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
        $this->tCmnSuperGlobals->request->set('ym', $validYM);
45
    }
46
47
    private function establishValidValue($key, $value, $inValuesFilterRules)
48
    {
49
        $validOpts                       = [];
50
        $validOpts['options']['default'] = $value;
51
        switch ($inValuesFilterRules['validation_filter']) {
52
            case "int":
53
                $inVFR                             = $inValuesFilterRules['validation_options'];
54
                $validOpts['options']['max_range'] = $this->getValidOption($value, $inVFR, 'max_range');
55
                $validOpts['options']['min_range'] = $this->getValidOption($value, $inVFR, 'min_range');
56
                $validation                        = FILTER_VALIDATE_INT;
57
                break;
58
            case "float":
59
                $validOpts['options']['decimal']   = 2;
60
                $validation                        = FILTER_VALIDATE_FLOAT;
61
                break;
62
        }
63
        $validValue = filter_var($this->tCmnSuperGlobals->get($key), $validation, $validOpts);
0 ignored issues
show
Bug introduced by
The variable $validation 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...
64
        return $validValue;
65
    }
66
67
    private function getValidOption($value, $inValuesFilterRules, $optionLabel)
68
    {
69
        $valReturn = $inValuesFilterRules[$optionLabel];
70
        if ($valReturn == 'default') {
71
            $valReturn = $value;
72
        }
73
        return $valReturn;
74
    }
75
76
    private function processFormInputDefaults($inValuesFilterRules)
77
    {
78
        $this->applyYMvalidations();
79
        foreach ($inValuesFilterRules as $key => $value) {
80
            $validValue = trim($this->tCmnSuperGlobals->get($key));
81
            if (array_key_exists('validation_options', $value)) {
82
                $validValue = $this->establishValidValue($key, $value['default'], $value);
83
            }
84
            $this->tCmnSuperGlobals->request->set($key, $validValue);
85
        }
86
    }
87
}
88