Arrays   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 27
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B moveElement() 0 24 6
1
<?php
2
3
namespace Scaffolder\Support;
4
5
class Arrays
6
{
7
	public static function moveElement($array, $toMove, $targetIndex) {
8
		if (count($array) == 1)
9
			return $array;
10
		if (is_int($toMove)) {
11
			$tmp = array_splice($array, $toMove, 1);
12
			array_splice($array, $targetIndex, 0, $tmp);
13
			$output = $array;
14
		}
15
		elseif (is_string($toMove)) {
16
			$indexToMove = array_search($toMove, array_keys($array));
17
			$itemToMove = $array[$toMove];
18
			array_splice($array, $indexToMove, 1);
19
			$i = 0;
20
			$output = Array();
21
			foreach($array as $key => $item) {
22
				if ($i == $targetIndex) {
23
					$output[$toMove] = $itemToMove;
24
				}
25
				$output[$key] = $item;
26
				$i++;
27
			}
28
		}
29
		return $output;
0 ignored issues
show
Bug introduced by
The variable $output 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...
30
	}
31
}
32
33