ReplaceIntoBuilder::replace()   B
last analyzed

Complexity

Conditions 6
Paths 1

Size

Total Lines 47

Duplication

Lines 13
Ratio 27.66 %

Importance

Changes 0
Metric Value
dl 13
loc 47
rs 8.5341
c 0
b 0
f 0
cc 6
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
 */
11
class ReplaceIntoBuilder
12
{
13
    /**
14
     * @return \Closure
15
     */
16
    public function replace()
17
    {
18
        return function (array $values) {
19
20
            /* @var Builder $this  */
21
22
            if (empty($values)) {
23
                return true;
24
            }
25
26
            // Since every insert gets treated like a batch insert, we will make sure the
27
            // bindings are structured in a way that is convenient for building these
28
            // inserts statements by verifying the elements are actually an array.
29 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...
30
                $values = [$values];
31
            }
32
33
            // Since every insert gets treated like a batch insert, we will make sure the
34
            // bindings are structured in a way that is convenient for building these
35
            // inserts statements by verifying the elements are actually an array.
36
            else {
37
                foreach ($values as $key => $value) {
38
                    ksort($value);
39
                    $values[$key] = $value;
40
                }
41
            }
42
            // We'll treat every insert like a batch insert so we can easily insert each
43
            // of the records into the database consistently. This will make it much
44
            // easier on the grammars to just handle one type of record insertion.
45
            $bindings = [];
46
47
            foreach ($values as $record) {
48
                foreach ($record as $value) {
49
                    $bindings[] = $value;
50
                }
51
            }
52
53
            $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...
54
55
            // Once we have compiled the insert statement's SQL we can execute it on the
56
            // connection and return a result as a boolean success indicator as that
57
            // is the same type of result returned by the raw connection instance.
58
            $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...
59
60
            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...
61
        };
62
    }
63
64
    /**
65
     * @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...
66
     * @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...
67
     * @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...
68
     *
69
     * @return string
70
     */
71
    public static function compileReplace()
72
    {
73
        return function (array $values) {
74
            $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...
75
            // Essentially we will force every insert to be treated as a batch insert which
76
            // simply makes creating the SQL easier for us since we can utilize the same
77
            // basic routine regardless of an amount of records given to us to insert.
78
            $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...
79
80
            if (!\is_array(reset($values))) {
81
                $values = [$values];
82
            }
83
84
            $columns = $grammar->columnize(array_keys(reset($values)));
85
86
            // We need to build a list of parameter place-holders of values that are bound
87
            // to the query. Each insert should have the exact same amount of parameter
88
            // bindings so we will loop through the record and parameterize them all.
89
            $parameters = [];
90
91
            foreach ($values as $record) {
92
                $parameters[] = '('.$grammar->parameterize($record).')';
93
            }
94
95
            $parameters = implode(', ', $parameters);
96
97
            return "replace into {$table} ({$columns}) values {$parameters}";
98
        };
99
    }
100
}
101