CsvImport   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B import() 0 54 8
B fgetcsv_reg() 0 24 6
1
<?php
2
namespace CsvCombine\Import;
3
4
use CsvCombine\Import\AppImport;
5
use Exception;
6
7
class CsvImport extends AppImport
8
{
9
    /*
10
     * loadDataCsv CSV読み込みアクション
11
     *
12
     * @param string $fileName 固定長テキストファイ
13
     * @param array $columnList 各カラム情報(name:カラム名,length:バイト数)
14
     * @param array $options
15
     */
16
    public function import($fileName, $columnList = [], $options = [])
17
    {
18
        $options = $this->getOptions($options);
19
        extract($options);
20
21
        //保存をするのでモデルを読み込み
22
        try {
23
            $data = array();
24
            $csvData = array();
25
            $file = fopen($fileName,"r");
26
            while($data = $this->fgetcsv_reg($file, 65536, $delimiter)) {//CSVファイルを","区切りで配列に
27
                mb_convert_variables($arrayEncoding, $importEncoding, $data);
28
                $csvData[] = $data;
29
            }
30
31
            $i = 0;
32
            foreach ($csvData as $line) {
33
                $thisData = array();
34
                if (empty($columnList)) {
35
                    $thisColumnList = array();
36
                    $lineCount = 0;
37
                    foreach ($line as $line_v) {
38
                        $thisColumnList[] = $lineCount;
39
                        $lineCount++;
40
                    }
41
                } else {
42
                    $thisColumnList = $columnList;
43
                }
44
                foreach ($thisColumnList as $k => $v) {
45
                    if (isset($line[$k])) {
46
                        //先頭と末尾の"を削除
47
                        $b = $line[$k];
48
                        //カラムの数だけセット
49
                        $thisData = array_merge(
50
                                        $thisData,
51
                                        array($v => $b)
52
                        );
53
                    } else {
54
                        $thisData = array_merge(
55
                                        $thisData,
56
                                        array($v => '')
57
                        );
58
                    }
59
                }
60
61
                $data[$i] = $thisData;
62
                $i++;
63
            }
64
        } catch (Exception $e) {
65
            return false;
66
        }
67
68
        return $data;
69
    }
70
71
    /**
72
     * fgetcsv_reg
73
     *
74
     * this is a port of the original code written by yossy.
75
     *
76
     * @author yossy
77
     * @author hagiwara
78
     *
79
     * @param resource $handle
80
     * @param integer $length
81
     * @param string $d
82
     * @param string $e
83
     * @see http://yossy.iimp.jp/wp/?p=56
84
     * @return array
85
     */
86
    private function fgetcsv_reg (&$handle, $length = null, $d = ',', $e = '"')
87
    {
88
        $d = preg_quote($d);
89
        $e = preg_quote($e);
90
        $_line = "";
91
        $eof = false; // Added for PHP Warning.
92
        while ( $eof != true ) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
93
            $_line .= (empty($length) ? fgets($handle) : fgets($handle, $length));
94
            $itemcnt = preg_match_all('/'.$e.'/', $_line, $dummy);
95
            if ($itemcnt % 2 == 0) $eof = true;
96
        }
97
        $_csv_line = preg_replace('/(?:\\r\\n|[\\r\\n])?$/', $d, trim($_line));
98
        $_csv_pattern = '/('.$e.'[^'.$e.']*(?:'.$e.$e.'[^'.$e.']*)*'.$e.'|[^'.$d.']*)'.$d.'/';
99
100
        preg_match_all($_csv_pattern, $_csv_line, $_csv_matches);
101
102
        $_csv_data = $_csv_matches[1];
103
104
        for ( $_csv_i=0; $_csv_i<count($_csv_data); $_csv_i++ ) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
105
            $_csv_data[$_csv_i] = preg_replace('/^'.$e.'(.*)'.$e.'$/s', '$1', $_csv_data[$_csv_i]);
106
            $_csv_data[$_csv_i] = str_replace($e.$e, $e, $_csv_data[$_csv_i]);
107
        }
108
        return empty($_line) ? false : $_csv_data;
109
    }
110
111
}
112