| Total Complexity | 40 |
| Total Lines | 312 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Insert often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Insert, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | abstract class Insert extends InsertUpdateStatement { |
||
| 15 | /** @var array<string|int, DBParameterValueType> */ |
||
| 16 | private array $fields = []; |
||
| 17 | /** @var array<string|int, DBParameterValueType> */ |
||
| 18 | private array $update = []; |
||
| 19 | private ?string $table = null; |
||
| 20 | private ?string $keyField = null; |
||
| 21 | private bool $ignore = false; |
||
| 22 | private ?Select $from = null; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param string $table |
||
| 26 | * @return $this |
||
| 27 | */ |
||
| 28 | public function into(string $table) { |
||
| 29 | $this->table = $table; |
||
| 30 | |||
| 31 | return $this; |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param bool $value |
||
| 36 | * @return $this |
||
| 37 | */ |
||
| 38 | public function setIgnore(bool $value = true) { |
||
| 39 | $this->ignore = $value; |
||
| 40 | |||
| 41 | return $this; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Legt den Primaerschluessel fest. |
||
| 46 | * Wenn bei einem Insert der Primaerschluessel mitgegeben wird, dann wird dieser statt der LastInsertId |
||
| 47 | * zurueckgegeben |
||
| 48 | * |
||
| 49 | * @param string $field |
||
| 50 | * @return $this |
||
| 51 | */ |
||
| 52 | public function setKey(string $field) { |
||
| 53 | $this->keyField = $field; |
||
| 54 | |||
| 55 | return $this; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $field |
||
| 60 | * @param DBParameterValueType $value |
||
|
|
|||
| 61 | * @return $this |
||
| 62 | */ |
||
| 63 | public function add(string $field, $value) { |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param string $field |
||
| 71 | * @param DBParameterValueType $value |
||
| 72 | * @return $this |
||
| 73 | */ |
||
| 74 | public function update(string $field, $value) { |
||
| 75 | $this->update = $this->addTo($this->update, $field, $value); |
||
| 76 | |||
| 77 | return $this; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param string $field |
||
| 82 | * @param DBParameterValueType $value |
||
| 83 | * @return $this |
||
| 84 | */ |
||
| 85 | public function addOrUpdate(string $field, $value) { |
||
| 86 | $this->add($field, $value); |
||
| 87 | $this->update($field, $value); |
||
| 88 | |||
| 89 | return $this; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param string $str |
||
| 94 | * @param DBParameterValueType ...$args |
||
| 95 | * @return $this |
||
| 96 | */ |
||
| 97 | public function addExpr(string $str, ...$args) { |
||
| 98 | if(count($args) > 0) { |
||
| 99 | $this->fields[] = func_get_args(); |
||
| 100 | } else { |
||
| 101 | $this->fields[] = $str; |
||
| 102 | } |
||
| 103 | |||
| 104 | return $this; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $str |
||
| 109 | * @param DBParameterValueType ...$args |
||
| 110 | * @return $this |
||
| 111 | */ |
||
| 112 | public function updateExpr(string $str, ...$args) { |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param string $expr |
||
| 124 | * @param DBParameterValueType ...$args |
||
| 125 | * @return $this |
||
| 126 | */ |
||
| 127 | public function addOrUpdateExpr(string $expr, ...$args) { |
||
| 128 | if(count($args) > 0) { |
||
| 129 | $this->fields[] = func_get_args(); |
||
| 130 | $this->update[] = func_get_args(); |
||
| 131 | } else { |
||
| 132 | $this->fields[] = $expr; |
||
| 133 | $this->update[] = $expr; |
||
| 134 | } |
||
| 135 | |||
| 136 | return $this; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param array<string, DBParameterValueType> $data |
||
| 141 | * @param null|string[] $mask |
||
| 142 | * @param null|string[] $excludeFields |
||
| 143 | * @return $this |
||
| 144 | */ |
||
| 145 | public function addAll(array $data, ?array $mask = null, ?array $excludeFields = null) { |
||
| 146 | $this->addAllTo($data, $mask, $excludeFields, function($field, $value) { |
||
| 147 | $this->add($field, $value); |
||
| 148 | }); |
||
| 149 | |||
| 150 | return $this; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param array<string, DBParameterValueType> $data |
||
| 155 | * @param null|string[] $mask |
||
| 156 | * @param null|string[] $excludeFields |
||
| 157 | * @return $this |
||
| 158 | */ |
||
| 159 | public function updateAll(array $data, ?array $mask = null, ?array $excludeFields = null) { |
||
| 160 | $this->addAllTo($data, $mask, $excludeFields, function($field, $value) { |
||
| 161 | if($field !== $this->keyField) { |
||
| 162 | $this->update($field, $value); |
||
| 163 | } |
||
| 164 | }); |
||
| 165 | |||
| 166 | return $this; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param array<string, DBParameterValueType> $data |
||
| 171 | * @param null|string[] $mask |
||
| 172 | * @param array<int, string>|null $excludeFields |
||
| 173 | * @return $this |
||
| 174 | */ |
||
| 175 | public function addOrUpdateAll(array $data, ?array $mask = null, ?array $excludeFields = null) { |
||
| 176 | $this->addAll($data, $mask, $excludeFields); |
||
| 177 | $this->updateAll($data, $mask, $excludeFields); |
||
| 178 | |||
| 179 | return $this; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param Select $select |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | public function from(Select $select) { |
||
| 187 | $this->from = $select; |
||
| 188 | |||
| 189 | return $this; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param iterable<int, array<string, mixed>>|Traversable<int, array<string, mixed>> $rows |
||
| 194 | * @return int[] Insert IDs |
||
| 195 | */ |
||
| 196 | abstract public function insertRows(iterable $rows); |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param array<string, mixed> $params |
||
| 200 | * @return int |
||
| 201 | */ |
||
| 202 | abstract public function run(array $params = []): int; |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function __toString(): string { |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param array<string|int, mixed> $fields |
||
| 241 | * @param string $field |
||
| 242 | * @param DBParameterValueType $value |
||
| 243 | * @return array<string|int, mixed> |
||
| 244 | */ |
||
| 245 | private function addTo(array $fields, string $field, $value): array { |
||
| 246 | if(!$this->isFieldNameValid($field)) { |
||
| 247 | throw new UnexpectedValueException('Field name is invalid'); |
||
| 248 | } |
||
| 249 | $sqlField = $field; |
||
| 250 | $sqlValue = $this->db()->quote($value); |
||
| 251 | $fields[$sqlField] = $sqlValue; |
||
| 252 | |||
| 253 | return $fields; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param array<string, mixed> $data |
||
| 258 | * @param string[]|null $mask |
||
| 259 | * @param string[]|null $excludeFields |
||
| 260 | * @param callable(string, mixed): void $fn |
||
| 261 | */ |
||
| 262 | private function addAllTo(array $data, ?array $mask, ?array $excludeFields, $fn): void { |
||
| 263 | if($mask !== null) { |
||
| 264 | $data = array_intersect_key($data, array_combine($mask, $mask)); |
||
| 265 | } |
||
| 266 | if($excludeFields !== null) { |
||
| 267 | foreach($excludeFields as $excludeField) { |
||
| 268 | if(array_key_exists($excludeField, $data)) { |
||
| 269 | unset($data[$excludeField]); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | } |
||
| 273 | $data = $this->clearValues($data); |
||
| 274 | foreach($data as $field => $value) { |
||
| 275 | $fn($field, $value); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | private function buildUpdate(): string { |
||
| 283 | $queryArr = []; |
||
| 284 | if(!empty($this->update)) { |
||
| 285 | $queryArr[] = "ON DUPLICATE KEY UPDATE\n"; |
||
| 286 | $updateArr = []; |
||
| 287 | if($this->keyField !== null) { |
||
| 288 | $updateArr[] = "\t`{$this->keyField}` = LAST_INSERT_ID({$this->keyField})"; |
||
| 289 | } |
||
| 290 | $updateArr = $this->buildFieldList($this->update, $updateArr); |
||
| 291 | |||
| 292 | $queryArr[] = implode(",\n", $updateArr); |
||
| 293 | } |
||
| 294 | |||
| 295 | return implode('', $queryArr); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $fieldName |
||
| 300 | * @return bool |
||
| 301 | */ |
||
| 302 | private function isFieldNameValid(string $fieldName): bool { |
||
| 303 | return !(is_numeric($fieldName) || !is_scalar($fieldName)); |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param array<string, mixed> $values |
||
| 308 | * @return array<string, mixed> |
||
| 309 | */ |
||
| 310 | private function clearValues(array $values): array { |
||
| 326 | } |
||
| 327 | } |
||
| 328 |
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