ArrayTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 44
c 0
b 0
f 0
wmc 7
lcom 0
cbo 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B inArray() 0 15 5
A toArray() 0 8 2
1
<?php
2
3
namespace Almendra\Validators\Traits;
4
5
/**
6
 * A group of helpers.
7
 *
8
 * @param type name 		description
9
 *
10
 * @return type description
11
 */
12
trait ArrayTrait
13
{
14
    /**
15
     * Performs an operation in a given array.
16
     * It stops if 'strict' is set to true and the result of the operation is false.
17
     *
18
     * @param array    $values The array
19
     * @param mixed    $args   The arguments to pass to the operation
20
     * @param callable $do     The operation
21
     *
22
     * @return bool
23
     */
24
    public function inArray($values, $args, $do, $strict = false)
25
    {
26
        if (!is_callable($do)) {
27
            throw new \InvalidArgumentException('Third argument must be a callable');
28
        }
29
30
        foreach ($values as $single) {
31
            $result = $do($single, $args);
32
            if ($strict && !$result) {
33
                break;
34
            }
35
        }
36
37
        return $result;
0 ignored issues
show
Bug introduced by
The variable $result 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...
38
    }
39
40
    /**
41
     * Eliminate nested array when passing parameters.
42
     *
43
     * @param array $value The array to eliminate layer up
44
     *
45
     * @return array
46
     */
47
    protected function toArray($value)
48
    {
49
        if (is_array($value[0])) {
50
            $value = $value[0];
51
        }
52
53
        return $value;
54
    }
55
}
56