Conditions | 13 |
Paths | 109 |
Total Lines | 88 |
Code Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
36 | public function upsert(string $table, $insertColumns, $updateColumns, array &$params = []): string |
||
37 | { |
||
38 | $constraints = []; |
||
39 | |||
40 | /** @var Constraint[] $constraints */ |
||
41 | [$uniqueNames, $insertNames, $updateNames] = $this->queryBuilder->prepareUpsertColumns( |
||
42 | $table, |
||
43 | $insertColumns, |
||
44 | $updateColumns, |
||
45 | $constraints |
||
46 | ); |
||
47 | |||
48 | if (empty($uniqueNames)) { |
||
49 | return $this->insert($table, $insertColumns, $params); |
||
50 | } |
||
51 | |||
52 | if ($updateNames === []) { |
||
53 | /** there are no columns to update */ |
||
54 | $updateColumns = false; |
||
55 | } |
||
56 | |||
57 | $onCondition = ['or']; |
||
58 | $quotedTableName = $this->queryBuilder->quoter()->quoteTableName($table); |
||
59 | |||
60 | foreach ($constraints as $constraint) { |
||
61 | $constraintCondition = ['and']; |
||
62 | foreach ($constraint->getColumnNames() as $name) { |
||
63 | $quotedName = $this->queryBuilder->quoter()->quoteColumnName($name); |
||
64 | $constraintCondition[] = "$quotedTableName.$quotedName=\"EXCLUDED\".$quotedName"; |
||
65 | } |
||
66 | |||
67 | $onCondition[] = $constraintCondition; |
||
68 | } |
||
69 | |||
70 | $on = $this->queryBuilder->buildCondition($onCondition, $params); |
||
71 | [, $placeholders, $values, $params] = $this->queryBuilder->prepareInsertValues($table, $insertColumns, $params); |
||
72 | |||
73 | if (!empty($placeholders)) { |
||
74 | $usingSelectValues = []; |
||
75 | foreach ($insertNames as $index => $name) { |
||
76 | $usingSelectValues[$name] = new Expression($placeholders[$index]); |
||
77 | } |
||
78 | |||
79 | /** @psalm-suppress UndefinedInterfaceMethod */ |
||
80 | $usingSubQuery = $this->queryBuilder->query()->select($usingSelectValues)->from('DUAL'); |
||
81 | [$usingValues, $params] = $this->queryBuilder->build($usingSubQuery, $params); |
||
82 | } |
||
83 | |||
84 | $insertValues = []; |
||
85 | $mergeSql = 'MERGE INTO ' |
||
86 | . $this->queryBuilder->quoter()->quoteTableName($table) |
||
87 | . ' ' |
||
88 | . 'USING (' . ($usingValues ?? ltrim($values, ' ')) |
||
89 | . ') "EXCLUDED" ' |
||
90 | . "ON ($on)"; |
||
91 | |||
92 | foreach ($insertNames as $name) { |
||
93 | $quotedName = $this->queryBuilder->quoter()->quoteColumnName($name); |
||
94 | |||
95 | if (strrpos($quotedName, '.') === false) { |
||
96 | $quotedName = '"EXCLUDED".' . $quotedName; |
||
97 | } |
||
98 | |||
99 | $insertValues[] = $quotedName; |
||
100 | } |
||
101 | |||
102 | $insertSql = 'INSERT (' . implode(', ', $insertNames) . ')' . ' VALUES (' . implode(', ', $insertValues) . ')'; |
||
103 | |||
104 | if ($updateColumns === false) { |
||
105 | return "$mergeSql WHEN NOT MATCHED THEN $insertSql"; |
||
106 | } |
||
107 | |||
108 | if ($updateColumns === true) { |
||
109 | $updateColumns = []; |
||
110 | foreach ($updateNames as $name) { |
||
111 | $quotedName = $this->queryBuilder->quoter()->quoteColumnName($name); |
||
112 | |||
113 | if (strrpos($quotedName, '.') === false) { |
||
114 | $quotedName = '"EXCLUDED".' . $quotedName; |
||
115 | } |
||
116 | $updateColumns[$name] = new Expression($quotedName); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | [$updates, $params] = $this->queryBuilder->prepareUpdateSets($table, $updateColumns, $params); |
||
121 | $updateSql = 'UPDATE SET ' . implode(', ', $updates); |
||
122 | |||
123 | return "$mergeSql WHEN MATCHED THEN $updateSql WHEN NOT MATCHED THEN $insertSql"; |
||
124 | } |
||
126 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths