Completed
Pull Request — master (#7)
by Greg
02:06
created

ReorderFields   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 56
Duplicated Lines 25 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 0
cbo 0
dl 14
loc 56
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A reorder() 0 12 3
A reorderFieldLabels() 7 11 4
B getSelectedFieldKeys() 7 16 5

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
namespace Consolidation\OutputFormatters\Transformations;
3
4
/**
5
 * Reorder the field labels based on the user-selected fields
6
 * to display.
7
 */
8
class ReorderFields
9
{
10
    /**
11
     * Given a simple list of user-supplied field keys or field labels,
12
     * return a reordered version of the field labels matching the
13
     * user selection.
14
     *
15
     * @param string|array $fields The user-selected fields
16
     * @param array $fieldLabels An associative array mapping the field
17
     *   key to the field label
18
     * @param array $data The data that will be rendered.
19
     *
20
     * @return array
21
     */
22
    public function reorder($fields, $fieldLabels, $data)
23
    {
24
        $fields = $this->getSelectedFieldKeys($fields, $fieldLabels);
25
        $result = [];
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
26
        if (empty($fieldLabels)) {
27
            $fieldLabels = array_combine(array_keys($data[0]), array_keys($data[0]));
28
        }
29
        if (empty($fields)) {
30
            return $fieldLabels;
31
        }
32
        return $this->reorderFieldLabels($fields, $fieldLabels, $data);
33
    }
34
35
    protected function reorderFieldLabels($fields, $fieldLabels, $data)
36
    {
37 View Code Duplication
        foreach ($fields as $field) {
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...
38
            if (array_key_exists($field, $data[0])) {
39
                if (array_key_exists($field, $fieldLabels)) {
40
                    $result[$field] = $fieldLabels[$field];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = 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...
41
                }
42
            }
43
        }
44
        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...
45
    }
46
47
    protected function getSelectedFieldKeys($fields, $fieldLabels)
48
    {
49
        if (is_string($fields)) {
50
            $fields = explode(',', $fields);
51
        }
52
        $fieldLablesReverseMap = array_combine(array_values($fieldLabels), array_keys($fieldLabels));
53
        $selectedFields = [];
54 View Code Duplication
        foreach ($fields as $field) {
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...
55
            if (array_key_exists($field, $fieldLabels)) {
56
                $selectedFields[] = $field;
57
            } elseif (array_key_exists($field, $fieldLablesReverseMap)) {
58
                $selectedFields[] = $fieldLablesReverseMap[$field];
59
            }
60
        }
61
        return $selectedFields;
62
    }
63
}
64