These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php declare(strict_types=1); |
||
2 | |||
3 | namespace Compolomus\LSQLQueryBuilder\Parts; |
||
4 | |||
5 | use Compolomus\LSQLQueryBuilder\System\Traits\{ |
||
6 | Helper, |
||
7 | Caller, |
||
8 | GetParts, |
||
9 | Placeholders, |
||
10 | Limit as TLimit, |
||
11 | Where as TWhere, |
||
12 | Order as TOrder |
||
13 | }; |
||
14 | |||
15 | /** |
||
16 | * @method string table() |
||
17 | * @method void addPlaceholders($placeholders) |
||
18 | */ |
||
19 | class Update extends Insert |
||
20 | { |
||
21 | use Caller, TLimit, TWhere, TOrder, GetParts, Placeholders, Helper; |
||
22 | |||
23 | public function set(array $values): string |
||
24 | { |
||
25 | $result = []; |
||
26 | View Code Duplication | foreach ($values as $value) { |
|
0 ignored issues
–
show
|
|||
27 | $key = $this->uid('u'); |
||
28 | $result[] = ':' . $key; |
||
29 | $this->placeholders()->set($key, $value); |
||
30 | } |
||
31 | return implode(',', |
||
32 | array_map(function($field, $value) { |
||
33 | return $field . ' = ' . $value; |
||
34 | } |
||
35 | , $this->escapeField($this->fields), $result)); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @return string |
||
40 | */ |
||
41 | public function get(): string |
||
42 | { |
||
43 | $this->addPlaceholders($this->placeholders()->get()); |
||
44 | return 'UPDATE ' . $this->table() . ' SET ' |
||
45 | . (count($this->values) |
||
46 | ? implode(',', $this->values) |
||
47 | : (count($this->fields) & !count($this->values) |
||
48 | ? implode(',', array_map(function ($field) { |
||
49 | return $this->escapeField($field) . ' = ?'; |
||
50 | ;}, $this->fields)) |
||
51 | : '') |
||
52 | ) |
||
53 | . $this->getParts(); |
||
54 | } |
||
55 | } |
||
56 |
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.