functions.php ➔ hasKeyPost()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Get value from GET variable or return default value.
4
 *
5
 * @param string $key     to look for
6
 * @param mixed  $default value to set if key does not exists
7
 *
8
 * @return mixed value from GET or the default value
9
 */
10
function getGet($key, $default = null)
11
{
12
    return isset($_GET[$key])
13
        ? $_GET[$key]
14
        : $default;
15
}
16
17
18
19
/**
20
 * Get value from POST variable or return default value.
21
 *
22
 * @param mixed $key     to look for, or value array
23
 * @param mixed $default value to set if key does not exists
24
 *
25
 * @return mixed value from POST or the default value
26
 */
27
function getPost($key, $default = null)
28
{
29
    if (is_array($key)) {
30
        // $key = array_flip($key);
31
        // return array_replace($key, array_intersect_key($_POST, $key));
32
        foreach ($key as $val) {
33
            $post[$val] = getPost($val);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$post was never initialized. Although not strictly required by PHP, it is generally a good practice to add $post = 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...
34
        }
35
        return $post;
0 ignored issues
show
Bug introduced by
The variable $post 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...
36
    }
37
38
    return isset($_POST[$key])
39
        ? $_POST[$key]
40
        : $default;
41
}
42
43
44
45
/**
46
 * Check if key is set in POST.
47
 *
48
 * @param mixed $key     to look for
49
 *
50
 * @return boolean true if key is set, otherwise false
51
 */
52
function hasKeyPost($key)
53
{
54
    return array_key_exists($key, $_POST);
55
}
56