Passed
Push — master ( ded3fd...8acf3f )
by Aleksandar
03:45
created

UpdateBuilder::buildTo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 * Copyright 2021 Aleksandar Panic
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace ArekX\PQL\Drivers\MySql\Builder\Builders;
19
20
use ArekX\PQL\Contracts\StructuredQuery;
21
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\JoinTrait;
22
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\NumberPartTrait;
23
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\QuoteNameTrait;
24
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\SubQueryTrait;
25
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\WhereTrait;
26
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\WrapValueTrait;
27
use ArekX\PQL\Drivers\MySql\Builder\MySqlQueryBuilderState;
28
29
/**
30
 * Represents a query builder for building UPDATE query.
31
 */
32
class UpdateBuilder extends QueryPartBuilder
33
{
34
    use QuoteNameTrait;
35
    use WrapValueTrait;
36
    use SubQueryTrait;
37
    use JoinTrait;
38
    use WhereTrait;
39
    use NumberPartTrait;
40
41
    /**
42
     * @inheritDoc
43
     */
44 7
    protected function getInitialParts(): array
45
    {
46 7
        return ['UPDATE'];
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52 7
    protected function getRequiredParts(): array
53
    {
54 7
        return ['to', 'set'];
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60 7
    protected function getPartBuilders(): array
61
    {
62
        return [
63 7
            'to' => fn($parts, $state) => $this->buildTo($parts, $state),
64 7
            'join' => fn($parts, $state) => $this->buildJoin($parts, $state),
65 7
            'set' => fn($parts, $state) => $this->buildSet($parts, $state),
66 7
            'where' => fn($parts, $state) => $this->buildWhere($parts, $state),
67 7
            'limit' => fn($parts, $state) => $this->buildLimit($parts),
0 ignored issues
show
Unused Code introduced by
The parameter $state is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

67
            'limit' => fn($parts, /** @scrutinizer ignore-unused */ $state) => $this->buildLimit($parts),

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68 7
            'offset' => fn($parts, $state) => $this->buildOffset($parts),
0 ignored issues
show
Unused Code introduced by
The parameter $state is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

68
            'offset' => fn($parts, /** @scrutinizer ignore-unused */ $state) => $this->buildOffset($parts),

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
        ];
70
    }
71
72
    /**
73
     * Builds table which will be updated.
74
     *
75
     * @param array|StructuredQuery $parts Part which will be updated.
76
     * @param MySqlQueryBuilderState $state Query builder state.
77
     * @return string
78
     */
79 7
    protected function buildTo($parts, MySqlQueryBuilderState $state)
80
    {
81 7
        if ($parts instanceof StructuredQuery) {
82 1
            return $this->buildQuery($parts, $state);
83
        }
84
85 7
        return $this->quoteName($parts);
0 ignored issues
show
Bug introduced by
$parts of type array is incompatible with the type string expected by parameter $name of ArekX\PQL\Drivers\MySql\...ateBuilder::quoteName(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
        return $this->quoteName(/** @scrutinizer ignore-type */ $parts);
Loading history...
86
    }
87
88
    /**
89
     * Build SET part.
90
     *
91
     * @param array|StructuredQuery $parts Part to be built
92
     * @param MySqlQueryBuilderState $state Query builder state
93
     * @return string
94
     */
95 7
    protected function buildSet($parts, MySqlQueryBuilderState $state)
96
    {
97 7
        if ($parts instanceof StructuredQuery) {
98 1
            return 'SET ' . $this->buildQuery($parts, $state);
99
        }
100
101 7
        $results = [];
102
103 7
        foreach ($parts as $key => $value) {
104 7
            $results[] = $this->quoteName($key) . ' = ' . $this->buildWrapValue($value, $state);
105
        }
106
107 7
        return 'SET ' . implode(', ', $results);
108
    }
109
}