Csv::get()   B
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 33
c 0
b 0
f 0
ccs 25
cts 25
cp 1
rs 7.6666
cc 10
nc 10
nop 0
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Maketok\DataMigration\Input;
4
5
use Maketok\DataMigration\ArrayUtilsTrait;
6
use Maketok\DataMigration\Storage\Exception\ParsingException;
7
8
class Csv extends AbstractFile
9
{
10
    use ArrayUtilsTrait;
11
12
    /**
13
     * @var array
14
     */
15
    protected $header;
16
    /**
17
     * @var ShaperInterface
18
     */
19
    private $shaper;
20
21
    /**
22
     * @param string $fileName
23
     * @param string $mode
24
     * @param ShaperInterface $shaper
25
     */
26 12
    public function __construct($fileName, $mode = 'r', ShaperInterface $shaper = null)
27
    {
28 12
        parent::__construct($fileName, $mode);
29 12
        $this->shaper = $shaper;
30 12
    }
31
32
    /**
33
     * @return array - hashmap of current entity
34
     * @throws ParsingException
35
     */
36 7
    public function get()
37
    {
38 7
        $shaper = $this->getShaper();
39
        do {
40 7
            $row = $this->descriptor->fgetcsv();
41 7
            if (!isset($this->header) || !is_array($this->header)) {
42 7
                $this->header = $row;
43 7
                $row = $this->descriptor->fgetcsv();
44 7
            }
45 7
            if ($row === false || $row === null || $this->isEmptyData($row)) {
46 5
                if ($shaper) {
47 4
                    return $shaper->feed([]);
48
                } else {
49 1
                    return false;
50
                }
51
            }
52 7
            if (count($this->header) != count($row)) {
53 1
                throw new ParsingException(
54 1
                    sprintf(
55 1
                        "Row contains wrong number of columns compared to header: %s",
56 1
                        json_encode($row)
57 1
                    )
58 1
                );
59
            }
60 6
            $combinedRow = array_combine($this->header, $row);
61 6
            if ($shaper) {
62 4
                $entity = $shaper->feed($combinedRow);
63 4
            } else {
64 2
                $entity = $combinedRow;
65
            }
66 6
        } while ($entity === false);
67 6
        return $entity;
68
    }
69
70
    /**
71
     * add row to input resource
72
     * @param array $entity
73
     * @return void
74
     */
75 5
    public function add(array $entity)
76
    {
77 5
        if ($shaper = $this->getShaper()) {
78 4
            $rows = $shaper->parse($entity);
79 4
        } else {
80 1
            $rows = [$entity];
81
        }
82 5
        if (!isset($this->header) && count($rows)) {
83 5
            $currentRow = current($rows);
84 5
            $this->header = array_keys($currentRow);
85 5
            $this->descriptor->fputcsv($this->header);
86 5
        }
87 5
        foreach ($rows as $row) {
88 5
            $this->descriptor->fputcsv($row);
89 5
        }
90 5
    }
91
92
    /**
93
     * reset internal counter
94
     * @return mixed
95
     */
96 5
    public function reset()
97
    {
98 5
        $this->header = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $header.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
99 5
        $this->descriptor->rewind();
100 5
        if ($shaper = $this->getShaper()) {
101 4
            $shaper->clear();
102 4
        }
103 5
    }
104
105
    /**
106
     * @return ShaperInterface
107
     */
108 12
    public function getShaper()
109
    {
110 12
        return $this->shaper;
111
    }
112
113
    /**
114
     * @param ShaperInterface $shaper
115
     */
116
    public function setShaper($shaper)
117
    {
118
        $this->shaper = $shaper;
119
    }
120
}
121