| Total Complexity | 58 |
| Total Lines | 380 |
| Duplicated Lines | 0 % |
| Coverage | 90.31% |
| Changes | 0 | ||
Complex classes like StatementExecuteCommand 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 StatementExecuteCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class StatementExecuteCommand extends QueryCommand { |
||
| 17 | /** |
||
| 18 | * The identifier for this command. |
||
| 19 | * @var int |
||
| 20 | * @source |
||
| 21 | */ |
||
| 22 | const COMMAND_ID = 0x17; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var mixed |
||
| 26 | */ |
||
| 27 | protected $id; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $params; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Constructor. |
||
| 36 | * @param \Plasma\DriverInterface $driver |
||
| 37 | * @param mixed $id |
||
| 38 | * @param string $query |
||
| 39 | * @param array $params |
||
| 40 | */ |
||
| 41 | 37 | function __construct(\Plasma\DriverInterface $driver, $id, string $query, array $params) { |
|
| 42 | 37 | parent::__construct($driver, $query); |
|
| 43 | |||
| 44 | 37 | $this->id = $id; |
|
| 45 | 37 | $this->params = $params; |
|
| 46 | 37 | } |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Get the encoded message for writing to the database connection. |
||
| 50 | * @return string |
||
| 51 | */ |
||
| 52 | 36 | function getEncodedMessage(): string { |
|
| 53 | 36 | $packet = \chr(static::COMMAND_ID); |
|
| 54 | 36 | $packet .= \Plasma\BinaryBuffer::writeInt4($this->id); |
|
| 55 | |||
| 56 | 36 | $packet .= "\x00"; // Cursor type flag |
|
| 57 | 36 | $packet .= \Plasma\BinaryBuffer::writeInt4(1); // Iteration count is always 1 |
|
| 58 | |||
| 59 | 36 | $bitmapOffset = \strlen($packet); |
|
| 60 | 36 | $packet .= \str_repeat("\x00", ((\count($this->params) + 7) >> 3)); |
|
| 61 | |||
| 62 | 36 | $bound = 0; |
|
| 63 | |||
| 64 | 36 | $types = ''; |
|
| 65 | 36 | $values = ''; |
|
| 66 | |||
| 67 | 36 | foreach($this->params as $id => $param) { |
|
| 68 | 36 | if($param === null) { |
|
| 69 | 1 | $offset = $bitmapOffset + ($id >> 3); |
|
| 70 | 1 | $packet[$offset] = $packet[$offset] | \chr((1 << ($id % 8))); |
|
| 71 | } else { |
||
| 72 | 36 | $bound = 1; |
|
| 73 | } |
||
| 74 | |||
| 75 | try { |
||
| 76 | 36 | $manager = \Plasma\Types\TypeExtensionsManager::getManager('driver-mysql'); |
|
| 77 | $encode = $manager->encodeType($param); |
||
| 78 | |||
| 79 | $unsigned = $encode->isUnsigned(); |
||
| 80 | $type = $encode->getSQLType(); |
||
| 81 | $value = $encode->getValue(); |
||
| 82 | 36 | } catch (\Plasma\Exception $e) { |
|
| 83 | 36 | [ $unsigned, $type, $value ] = $this->stdEncodeValue($param); |
|
| 84 | } |
||
| 85 | |||
| 86 | 35 | $types .= \chr($type).($unsigned ? "\x80" : "\x00"); |
|
| 87 | 35 | $values .= $value; |
|
| 88 | } |
||
| 89 | |||
| 90 | 35 | $packet .= \chr($bound); |
|
| 91 | |||
| 92 | 35 | if($bound) { |
|
| 93 | 35 | $packet .= $types; |
|
| 94 | 35 | $packet .= $values; |
|
| 95 | } |
||
| 96 | |||
| 97 | 35 | return $packet; |
|
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Whether the sequence ID should be resetted. |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | 36 | function resetSequence(): bool { |
|
| 105 | 36 | return true; |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Parses the binary resultset row and returns the row. |
||
| 110 | * @param \Plasma\BinaryBuffer $buffer |
||
| 111 | * @return array |
||
| 112 | */ |
||
| 113 | 35 | protected function parseResultsetRow(\Plasma\BinaryBuffer $buffer): array { |
|
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Standard encode value, if type extensions failed. |
||
| 153 | * @param mixed $param |
||
| 154 | * @return array |
||
| 155 | * @throws \Plasma\Exception |
||
| 156 | */ |
||
| 157 | 36 | protected function stdEncodeValue($param): array { |
|
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Standard decode value, if type extensions failed. |
||
| 203 | * @param \Plasma\ColumnDefinitionInterface $column |
||
| 204 | * @param \Plasma\BinaryBuffer $buffer |
||
| 205 | * @return mixed |
||
| 206 | * @throws \Plasma\Exception |
||
| 207 | */ |
||
| 208 | 35 | protected function stdDecodeBinaryValue(\Plasma\ColumnDefinitionInterface $column, \Plasma\BinaryBuffer $buffer) { |
|
| 209 | 35 | $flags = $column->getFlags(); |
|
| 210 | 35 | $type = $column->getType(); |
|
| 211 | |||
| 212 | switch(true) { |
||
| 213 | 35 | case $this->isTypeString($type): |
|
|
1 ignored issue
–
show
|
|||
| 214 | 18 | $value = $buffer->readStringLength(); |
|
| 215 | 18 | break; |
|
| 216 | 17 | case ($type === 'TINY'): |
|
| 217 | 7 | $value = $buffer->readInt1(); |
|
| 218 | 7 | $value = $this->zeroFillInts($column, $value); |
|
| 219 | 7 | break; |
|
|
1 ignored issue
–
show
|
|||
| 220 | 17 | case $this->isTypeShortOrYear($type): |
|
|
1 ignored issue
–
show
|
|||
| 221 | 7 | $value = $buffer->readInt2(); |
|
| 222 | 7 | $value = $this->zeroFillInts($column, $value); |
|
| 223 | 7 | break; |
|
|
1 ignored issue
–
show
|
|||
| 224 | 17 | case $this->isTypeInt24orLong($type): |
|
|
1 ignored issue
–
show
|
|||
| 225 | 7 | $value = $buffer->readInt4(); |
|
| 226 | |||
| 227 | 7 | if(($flags & \Plasma\Drivers\MySQL\FieldFlags::UNSIGNED_FLAG) !== 0 && \PHP_INT_SIZE <= 4) { |
|
| 228 | $value = \bcadd($value, '18446744073709551616'); |
||
| 229 | } |
||
| 230 | |||
| 231 | 7 | $value = $this->zeroFillInts($column, $value); |
|
| 232 | 7 | break; |
|
| 233 | 17 | case ($type === 'LONGLONG'): |
|
| 234 | 7 | $value = $buffer->readInt8(); |
|
| 235 | |||
| 236 | 7 | if(($flags & \Plasma\Drivers\MySQL\FieldFlags::UNSIGNED_FLAG) !== 0) { |
|
| 237 | $value = \bcadd($value, '18446744073709551616'); |
||
| 238 | 7 | } elseif(\PHP_INT_SIZE > 4) { |
|
| 239 | 7 | $value = (int) $value; |
|
| 240 | } |
||
| 241 | |||
| 242 | 7 | $value = $this->zeroFillInts($column, $value); |
|
| 243 | 7 | break; |
|
| 244 | 10 | case ($type === 'FLOAT'): |
|
| 245 | 2 | $value = $buffer->readFloat(); |
|
| 246 | 2 | break; |
|
| 247 | 10 | case ($type === 'DOUBLE'): |
|
| 248 | 2 | $value = $buffer->readDouble(); |
|
| 249 | 2 | break; |
|
| 250 | 8 | case $this->isTypeDate($type): |
|
|
1 ignored issue
–
show
|
|||
| 251 | 8 | $length = $buffer->readIntLength(); |
|
| 252 | 8 | if($length > 0) { |
|
| 253 | 1 | $year = $buffer->readInt2(); |
|
| 254 | 1 | $month = $buffer->readInt1(); |
|
| 255 | 1 | $day = $buffer->readInt1(); |
|
| 256 | |||
| 257 | 1 | $value = \sprintf('%04d-%02d-%02d', $year, $month, $day); |
|
| 258 | } else { |
||
| 259 | 7 | $value = '0000-00-00'; |
|
| 260 | } |
||
|
1 ignored issue
–
show
|
|||
| 261 | 8 | break; |
|
|
1 ignored issue
–
show
|
|||
| 262 | 8 | case $this->isTypeDateTime($type): |
|
|
1 ignored issue
–
show
|
|||
| 263 | 8 | $length = $buffer->readIntLength(); |
|
| 264 | 8 | if($length > 0) { |
|
| 265 | 2 | $year = $buffer->readInt2(); |
|
| 266 | 2 | $month = $buffer->readInt1(); |
|
| 267 | 2 | $day = $buffer->readInt1(); |
|
| 268 | |||
| 269 | 2 | if($length > 4) { |
|
| 270 | 1 | $hour = $buffer->readInt1(); |
|
| 271 | 1 | $min = $buffer->readInt1(); |
|
| 272 | 1 | $sec = $buffer->readInt1(); |
|
| 273 | } else { |
||
| 274 | 1 | $hour = 0; |
|
| 275 | 1 | $min = 0; |
|
| 276 | 1 | $sec = 0; |
|
| 277 | } |
||
| 278 | |||
| 279 | 2 | if($length > 7) { |
|
| 280 | $microI = $buffer->readInt4(); |
||
| 281 | } else { |
||
| 282 | 2 | $microI = 0; |
|
| 283 | } |
||
| 284 | |||
| 285 | 2 | $micro = \str_pad($microI, 6, '0', \STR_PAD_LEFT); |
|
| 286 | 2 | $micro = \substr($micro, 0, 3).' '.\substr($micro, 3); |
|
| 287 | |||
| 288 | 2 | $value = \sprintf('%04d-%02d-%02d %02d:%02d:%02d'.($microI > 0 ? '.%s' : ''), $year, $month, $day, $hour, $min, $sec, $micro); |
|
| 289 | |||
| 290 | 2 | if($type === 'TIMESTAMP') { |
|
| 291 | 2 | $value = \DateTime::createFromFormat('Y-m-d H:i:s'.($microI > 0 ? '.u' : ''), $value)->getTimestamp(); |
|
| 292 | } |
||
| 293 | } else { |
||
| 294 | 8 | $value = '0000-00-00 00:00:00.000 000'; |
|
| 295 | } |
||
| 296 | 8 | break; |
|
| 297 | 8 | case ($type === 'TIME'): |
|
| 298 | 8 | $length = $buffer->readIntLength(); |
|
| 299 | 8 | if($length > 1) { |
|
| 300 | 1 | $sign = $buffer->readInt1(); |
|
| 301 | 1 | $days = $buffer->readInt4(); |
|
| 302 | |||
| 303 | 1 | if($sign === 1) { |
|
| 304 | $days *= -1; |
||
| 305 | } |
||
| 306 | |||
| 307 | 1 | $hour = $buffer->readInt1(); |
|
| 308 | 1 | $min = $buffer->readInt1(); |
|
| 309 | 1 | $sec = $buffer->readInt1(); |
|
| 310 | |||
| 311 | 1 | if($length > 8) { |
|
| 312 | $microI = $buffer->readInt4(); |
||
| 313 | } else { |
||
| 314 | 1 | $microI = 0; |
|
| 315 | } |
||
| 316 | |||
| 317 | 1 | $micro = \str_pad($microI, 6, '0', \STR_PAD_LEFT); |
|
| 318 | 1 | $micro = \substr($micro, 0, 3).' '.\substr($micro, 3); |
|
| 319 | |||
| 320 | 1 | $value = \sprintf('%dd %02d:%02d:%02d'.($microI > 0 ? '.%s' : ''), $days, $hour, $min, $sec, $micro); |
|
| 321 | } else { |
||
| 322 | 7 | $value = '0d 00:00:00'; |
|
| 323 | } |
||
| 324 | 8 | break; |
|
| 325 | default: |
||
| 326 | throw new \InvalidArgumentException('Unknown column type (flags: '.$flags.', type: '.$column->getType().')'); |
||
| 327 | break; |
||
| 328 | } |
||
| 329 | |||
| 330 | 35 | return $value; |
|
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param string $type |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | 35 | protected function isTypeString(string $type): bool { |
|
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param string $type |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | 17 | protected function isTypeShortOrYear(string $type): bool { |
|
| 352 | 17 | $types = array('SHORT', 'YEAR'); |
|
| 353 | 17 | return \in_array($type, $types, true); |
|
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param string $type |
||
| 358 | * @return bool |
||
| 359 | */ |
||
| 360 | 17 | protected function isTypeInt24orLong(string $type): bool { |
|
| 361 | 17 | $types = array('INT24', 'LONG'); |
|
| 362 | 17 | return \in_array($type, $types, true); |
|
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param string $type |
||
| 367 | * @return bool |
||
| 368 | */ |
||
| 369 | 8 | protected function isTypeDate(string $type): bool { |
|
| 370 | 8 | $types = array('DATE', 'NEWDATE'); |
|
| 371 | 8 | return \in_array($type, $types, true); |
|
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param string $type |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | 8 | protected function isTypeDateTime(string $type): bool { |
|
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param \Plasma\ColumnDefinitionInterface $column |
||
| 385 | * @param string|int $value |
||
| 386 | * @return string|int |
||
| 387 | */ |
||
| 388 | 7 | protected function zeroFillInts(\Plasma\ColumnDefinitionInterface $column, $value) { |
|
| 396 | } |
||
| 397 | } |
||
| 398 |