FixedLengthImport   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 6.67 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 1
dl 5
loc 75
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A import() 0 11 1
C importBody() 5 37 8

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 CsvCombine\Import;
3
4
use CsvCombine\Import\AppImport;
5
6
class FixedLengthImport extends AppImport
7
{
8
9
    /*
10
     * loadData 固定長読み込みアクション
11
     *
12
     * @param string $fileName 固定長テキストファイ
13
     * @param array $columnList 各カラム情報(name:カラム名,length:バイト数)
14
     * @param array $options 下記パラメータを必要に応じて設定
15
     * line_feed_code 改行コード(デフォルトは\r\n)
16
     * array_encoding 出力するする配列のエンコード(デフォルトはUTF-8
17
     * import_encoding 入力するテキストのエンコード(デフォルトはSJIS-win
18
     * extra_fixed_options 出力のための固定長の設定(列によって桁数が異なる場合の設定)
19
     */
20
    public function import($fileName, $columnList, $options = [])
21
    {
22
        $options = array_merge($this->_defaultOptions,$options);
23
        extract($options);
24
25
        $fp = fopen($fileName,'r');
26
        $data = fread($fp, filesize($fileName));
27
        fclose($fp);
28
29
        return $this->importBody($data, $columnList, $options);
30
    }
31
32
    /*
33
     * importBody 固定長内容読み込みアクション
34
     *
35
     * @param string $data 固定長テキストデータ
36
     * @param array $baseColumnList 各カラム情報(name:カラム名,length:バイト数)
37
     * @param array $options 下記パラメータを必要に応じて設定
38
     * line_feed_code 改行コード(デフォルトは\r\n)
39
     * array_encoding 出力するする配列のエンコード(デフォルトはUTF-8
40
     * import_encoding 入力するテキストのエンコード(デフォルトはSJIS-win
41
     * extra_fixed_options 出力のための固定長の設定(列によって桁数が異なる場合の設定)
42
     */
43
    public function importBody($data, $baseColumnList, $options = [])
44
    {
45
        $options = $this->getOptions($options);
46
        extract($options);
47
48
        $returnInfo = [];
49
        //まずは分割
50
        $dataExplode = explode($lineFeedCode, $data);
51
        $listCount = count($dataExplode);
52
        foreach ($dataExplode as $row => $text) {
53
            //空行は無視
54
            if (strlen($text) === 0) {
55
                continue;
56
            }
57
            $startPoint = 0;
58
            $columnList = $baseColumnList;
59 View Code Duplication
            if (array_key_exists($row + 1, $extraFixedOptions)) {
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...
60
                $columnList = $extraFixedOptions[$row + 1];
61
            } elseif (array_key_exists($row - $listCount + 1, $extraFixedOptions)) {
62
                $columnList = $extraFixedOptions[$row - $listCount + 1];
63
            }
64
65
            foreach ($columnList as $columnInfo) {
66
                $returnInfo[$row][$columnInfo['name']] = rtrim(substr($text, $startPoint, $columnInfo['length']));
67
                $startPoint += $columnInfo['length'];
68
            }
69
        }
70
71
        //最後にまとめて文字コードを変換
72
        foreach ($returnInfo as $row => $returnInfoRow) {
73
            foreach ($returnInfoRow as $columnName => $returnInfoVal) {
74
                $returnInfo[$row][$columnName] = mb_convert_encoding($returnInfoVal, $arrayEncoding, $importEncoding);
75
            }
76
        }
77
78
        return $returnInfo;
79
    }
80
}
81