Completed
Push — master ( d942bb...bdf8d4 )
by Michael
63:24 queued 38:29
created

ReplaceIntoBuilder::compileReplace()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 30
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace Napp\Core\Dbal\Builder;
4
5
use Illuminate\Database\Grammar;
6
use Illuminate\Database\Query\Builder;
7
8
/**
9
 * Class ReplaceIntoBuilder
10
 * @package Napp\Core\Dbal\Builder
11
 */
12
class ReplaceIntoBuilder
13
{
14
    /**
15
     * @return \Closure
16
     */
17
    public function replace()
18
    {
19
        return function (array $values)  {
20
21
            /** @var Builder $this  */
22
23
            if (empty($values)) {
24
                return true;
25
            }
26
27
            // Since every insert gets treated like a batch insert, we will make sure the
28
            // bindings are structured in a way that is convenient for building these
29
            // inserts statements by verifying the elements are actually an array.
30 View Code Duplication
            if (! \is_array(reset($values))) {
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...
31
                $values = [$values];
32
            }
33
34
            // Since every insert gets treated like a batch insert, we will make sure the
35
            // bindings are structured in a way that is convenient for building these
36
            // inserts statements by verifying the elements are actually an array.
37
            else {
38
                foreach ($values as $key => $value) {
39
                    ksort($value);
40
                    $values[$key] = $value;
41
                }
42
            }
43
            // We'll treat every insert like a batch insert so we can easily insert each
44
            // of the records into the database consistently. This will make it much
45
            // easier on the grammars to just handle one type of record insertion.
46
            $bindings = [];
47
48
            foreach ($values as $record) {
49
                foreach ($record as $value) {
50
                    $bindings[] = $value;
51
                }
52
            }
53
54
            $sql = $this->compileReplace($values);
0 ignored issues
show
Unused Code introduced by
The call to ReplaceIntoBuilder::compileReplace() has too many arguments starting with $values.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
55
56
            // Once we have compiled the insert statement's SQL we can execute it on the
57
            // connection and return a result as a boolean success indicator as that
58
            // is the same type of result returned by the raw connection instance.
59
            $bindings = $this->cleanBindings($bindings);
0 ignored issues
show
Bug introduced by
The method cleanBindings() does not seem to exist on object<Napp\Core\Dbal\Builder\ReplaceIntoBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
61
62
            return $this->connection->insert($sql, $bindings);
0 ignored issues
show
Bug introduced by
The property connection does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
63
        };
64
    }
65
66
    /**
67
     * @param Builder $query
0 ignored issues
show
Bug introduced by
There is no parameter named $query. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
68
     * @param Grammar $grammar
0 ignored issues
show
Bug introduced by
There is no parameter named $grammar. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
69
     * @param array $values
0 ignored issues
show
Bug introduced by
There is no parameter named $values. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
70
     * @return string
71
     */
72
    public static function compileReplace()
73
    {
74
        return function (array $values) {
75
76
            $grammar = $this->getGrammar();
0 ignored issues
show
Bug introduced by
The method getGrammar() does not seem to exist on object<Napp\Core\Dbal\Builder\ReplaceIntoBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
            // Essentially we will force every insert to be treated as a batch insert which
78
            // simply makes creating the SQL easier for us since we can utilize the same
79
            // basic routine regardless of an amount of records given to us to insert.
80
            $table = $grammar->wrapTable($this->from);
0 ignored issues
show
Bug introduced by
The property from does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
81
82
            if (! \is_array(reset($values))) {
83
                $values = [$values];
84
            }
85
86
            $columns = $grammar->columnize(array_keys(reset($values)));
87
88
            // We need to build a list of parameter place-holders of values that are bound
89
            // to the query. Each insert should have the exact same amount of parameter
90
            // bindings so we will loop through the record and parameterize them all.
91
            $parameters = [];
92
93
            foreach ($values as $record) {
94
                $parameters[] = '('.$grammar->parameterize($record).')';
95
            }
96
97
            $parameters = implode(', ', $parameters);
98
99
            return "replace into {$table} ({$columns}) values {$parameters}";
100
        };
101
    }
102
}
103