ExcelTrait   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 19
lcom 0
cbo 0
dl 0
loc 122
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A rotate() 0 12 3
A trimExcel() 0 32 4
C arrangeExcel() 0 54 12
1
<?php
2
3
namespace Fabrica\Http\Api;
4
5
trait ExcelTrait
6
{
7
    /**
8
     * remove the empty row and trim the cell value
9
     *
10
     * @param  array $data
11
     * @return array
12
     */
13
    public function trimExcel($data)
14
    {
15
        if (!is_array($data)) {
16
            return [];
17
        }
18
19
        // trim the cell value
20
        foreach($data as $k1 => $val)
21
        {
22
            foreach($val as $k2 => $val2)
23
            {
24
                $data[$k1][$k2] = trim($val2);
25
            }
26
        }
27
28
        // delete the empty row
29
        $data = array_filter(
30
            $data, function ($v) {
31
                return array_filter($v); 
32
            }
33
        );
34
        // delete the empty column
35
        $data = array_filter(
36
            $this->rotate($data), function ($v) {
37
                return array_filter($v); 
38
            }
39
        );
40
        // rotate the array
41
        $data = $this->rotate($data);
42
43
        return $data;
44
    }
45
46
    /**
47
     * arrange the data 
48
     *
49
     * @param  array $data
50
     * @param  array $fields
51
     * @return array
52
     */
53
    public function arrangeExcel($data, $fields)
54
    {
55
        $data = $this->trimExcel($data);
56
57
        $header_index = 0;
58
        while(true)
59
        {
60
            $header = array_shift($data);
61
            if (!$header) {
62
                throw new \UnexpectedValueException('表头定位错误。', -11142);
63
            }
64
65
            if (is_array($header)) {
66
                if (in_array($header[0], $fields)) {
67
                    break;
68
                }
69
                if (++$header_index > 5) {
70
                    throw new \UnexpectedValueException('表头定位错误。', -11142);
71
                }
72
            }
73
        }
74
75
        // the first row is used for the issue keys
76
        if (array_search('', $header) !== false) {
0 ignored issues
show
Bug introduced by
The variable $header 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...
77
            throw new \UnexpectedValueException('表头不能有空值。', -11143);
78
        }
79
        // check the header title
80
        if (count($header) !== count(array_unique($header))) {
81
            throw new \UnexpectedValueException('表头不能有重复列。', -11144);
82
        }
83
84
        $field_keys = [];
85
        foreach($header as $field)
86
        {
87
            $tmp = array_search($field, $fields);
88
            if ($tmp === false) {
89
                throw new \UnexpectedValueException('表头有不明确列。', -11145);
90
            }
91
            $field_keys[] = $tmp;
92
        }
93
94
        $new_data = [];
95
        foreach ($data as $item)
96
        {
97
            $tmp = [];
98
            foreach ($item as $key => $val)
99
            {
100
                $tmp[$field_keys[$key]] = $val;
101
            }
102
            $new_data[] = $tmp;
103
        }
104
105
        return $new_data;
106
    }
107
108
    /**
109
     * rotate the matrix 
110
     *
111
     * @param  array $matrix
112
     * @return array
113
     */
114
    public function rotate(array $matrix)
115
    {
116
        $ret = [];
117
        foreach($matrix as $val)
118
        {
119
            foreach($val as $k => $val2)
120
            {
121
                $ret[$k][] = $val2;
122
            }
123
        }
124
        return $ret;
125
    }
126
}
127