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 = []; |
|
|
|
|
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) { |
|
|
|
|
38
|
|
|
if (array_key_exists($field, $data[0])) { |
39
|
|
|
if (array_key_exists($field, $fieldLabels)) { |
40
|
|
|
$result[$field] = $fieldLabels[$field]; |
|
|
|
|
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
return $result; |
|
|
|
|
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) { |
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.