Completed
Push — master ( ed54f6...6ff9a3 )
by Oleg
10s
created

DBALMysqlResourceInsertNoLoad::loadData()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 48
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 7.0199

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 25
cts 27
cp 0.9259
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 41
nc 8
nop 10
crap 7.0199

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Maketok\DataMigration\Storage\Db;
4
5
class DBALMysqlResourceInsertNoLoad extends DBALMysqlResource
6
{
7
    /**
8
     * {@inheritdoc}
9
     * override, add support for inserts
10
     * if server does not support Local Infile option
11
     */
12 2
    public function loadData(
0 ignored issues
show
Complexity introduced by
This method has 10 parameters which exceeds the configured maximum of 10.

A high number of parameters is generally an indication that you should consider creating a dedicated object for the parameters.

Let’s take a look at an example:

<?php

class SomeClass
{
    public function doSomething(A $a, B $b, C $c, D $d, E $e, F $f)
    {
        // ..
    }
}

class AnotherClass
{
    public function doSomething(A $a, B $b, C $c, D $d, E $e, F $f)
    {
        // ...
    }
}

could be refactored to:

class Context
{
    private $a;
    private $b;
    private $c;
    private $d;
    private $e;
    private $f;

    public function __construct(A $a, B $b, C $c, D $d, E $e, F $f)
    {
        // ...
    }
}

class SomeClass
{
    public function doSomething(Context $context)
    {
        // ...
    }
}

class AnotherClass
{
    public function doSomething(Context $context)
    {
        // ...
    }
}
Loading history...
13
        $table,
14
        $file,
15
        $local = false,
16
        array $columns = [],
17
        array $set = [],
18
        $delimiter = ",",
19
        $enclosure = '"',
20
        $escape = '\\',
21
        $termination = '\n',
22
        $optionallyEnclosed = true
23
    ) {
24 2
        $csv = new \SplFileObject($file, 'r');
25 2
        $table = $this->connection->quoteIdentifier($table);
26 2
        $columnsPart = '';
27 2
        if (!empty($columns)) {
28
            $columns = array_map([$this->connection, 'quoteIdentifier'], $columns);
29
            $columnsPart = '(' . implode(',', $columns) . ')';
30
        }
31 2
        $valuesPart = '';
32 2
        $row = $csv->fgetcsv($delimiter, $enclosure, $escape);
33 2
        $csv->rewind();
34 2
        if (!empty($row)) {
35
            $row = array_map(function () {
36 2
                return '?';
37 2
            }, $row);
38 2
            $valuesPart = '(' . implode(',', $row) . ')';
39
        }
40
        $sql = <<<MYSQL
41 2
INSERT INTO $table $columnsPart
42 2
VALUES $valuesPart
43
MYSQL;
44 2
        $count = 0;
45
        while (
46 2
            ($row = $csv->fgetcsv($delimiter, $enclosure, $escape)) !== null &&
47 2
            $row != [null] &&
48 2
            $row !== false
49
        ) {
50 2
            $row = array_map(function ($var) {
51 2
                if ($var === '\N') {
52 1
                    return null;
53
                }
54 2
                return $var;
55 2
            }, $row);
56 2
            $count += $this->connection->executeUpdate($sql, $row);
57
        }
58 2
        return $count;
59
    }
60
}
61