| Total Complexity | 42 |
| Total Lines | 259 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AbstractPdoCommand 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 AbstractPdoCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | abstract class AbstractPdoCommand extends AbstractCommand implements PdoCommandInterface, LoggerAwareInterface, ProfilerAwareInterface |
||
| 36 | { |
||
| 37 | use LoggerAwareTrait; |
||
| 38 | use ProfilerAwareTrait; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var PDOStatement|null Represents a prepared statement and, after the statement is executed, an associated |
||
| 42 | * result set. |
||
| 43 | * |
||
| 44 | * @link https://www.php.net/manual/en/class.pdostatement.php |
||
| 45 | */ |
||
| 46 | protected PDOStatement|null $pdoStatement = null; |
||
| 47 | |||
| 48 | public function __construct(protected PdoConnectionInterface $db) |
||
| 49 | { |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * This method mainly sets {@see pdoStatement} to be `null`. |
||
| 54 | */ |
||
| 55 | public function cancel(): void |
||
| 56 | { |
||
| 57 | $this->pdoStatement = null; |
||
| 58 | } |
||
| 59 | |||
| 60 | public function getPdoStatement(): PDOStatement|null |
||
| 61 | { |
||
| 62 | return $this->pdoStatement; |
||
| 63 | } |
||
| 64 | |||
| 65 | public function bindParam( |
||
| 66 | int|string $name, |
||
| 67 | mixed &$value, |
||
| 68 | int|null $dataType = null, |
||
| 69 | int|null $length = null, |
||
| 70 | mixed $driverOptions = null |
||
| 71 | ): static { |
||
| 72 | $this->prepare(); |
||
| 73 | |||
| 74 | if ($dataType === null) { |
||
| 75 | $dataType = $this->db->getSchema()->getDataType($value); |
||
| 76 | } |
||
| 77 | |||
| 78 | if ($length === null) { |
||
| 79 | $this->pdoStatement?->bindParam($name, $value, $dataType); |
||
| 80 | } elseif ($driverOptions === null) { |
||
| 81 | $this->pdoStatement?->bindParam($name, $value, $dataType, $length); |
||
| 82 | } else { |
||
| 83 | $this->pdoStatement?->bindParam($name, $value, $dataType, $length, $driverOptions); |
||
| 84 | } |
||
| 85 | |||
| 86 | return $this; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function bindValue(int|string $name, mixed $value, int|null $dataType = null): static |
||
| 90 | { |
||
| 91 | if ($dataType === null) { |
||
| 92 | $dataType = $this->db->getSchema()->getDataType($value); |
||
| 93 | } |
||
| 94 | |||
| 95 | $this->params[$name] = new Param($value, $dataType); |
||
| 96 | |||
| 97 | return $this; |
||
| 98 | } |
||
| 99 | |||
| 100 | public function bindValues(array $values): static |
||
| 101 | { |
||
| 102 | if (empty($values)) { |
||
| 103 | return $this; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @psalm-var array<string, int>|ParamInterface|int $value |
||
| 108 | */ |
||
| 109 | foreach ($values as $name => $value) { |
||
| 110 | if ($value instanceof ParamInterface) { |
||
| 111 | $this->params[$name] = $value; |
||
| 112 | } else { |
||
| 113 | $type = $this->db->getSchema()->getDataType($value); |
||
| 114 | $this->params[$name] = new Param($value, $type); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | return $this; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function prepare(bool|null $forRead = null): void |
||
| 122 | { |
||
| 123 | if (isset($this->pdoStatement)) { |
||
| 124 | $this->bindPendingParams(); |
||
| 125 | |||
| 126 | return; |
||
| 127 | } |
||
| 128 | |||
| 129 | $sql = $this->getSql(); |
||
| 130 | |||
| 131 | /** |
||
| 132 | * If SQL is empty, there will be {@see \ValueError} on prepare pdoStatement. |
||
| 133 | * |
||
| 134 | * @link https://php.watch/versions/8.0/ValueError |
||
| 135 | */ |
||
| 136 | if ($sql === '') { |
||
| 137 | return; |
||
| 138 | } |
||
| 139 | |||
| 140 | $pdo = $this->db->getActivePDO($sql, $forRead); |
||
| 141 | |||
| 142 | try { |
||
| 143 | $this->pdoStatement = $pdo?->prepare($sql); |
||
| 144 | $this->bindPendingParams(); |
||
| 145 | } catch (PDOException $e) { |
||
| 146 | $message = $e->getMessage() . "\nFailed to prepare SQL: $sql"; |
||
| 147 | /** @psalm-var array|null $errorInfo */ |
||
| 148 | $errorInfo = $e->errorInfo ?? null; |
||
| 149 | |||
| 150 | throw new Exception($message, $errorInfo, $e); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Binds pending parameters registered via {@see bindValue()} and {@see bindValues()}. |
||
| 156 | * |
||
| 157 | * Note that this method requires an active {@see pdoStatement}. |
||
| 158 | */ |
||
| 159 | protected function bindPendingParams(): void |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | protected function getQueryBuilder(): QueryBuilderInterface |
||
| 169 | } |
||
| 170 | |||
| 171 | protected function getQueryMode(int $queryMode): string |
||
| 172 | { |
||
| 173 | return match ($queryMode) { |
||
| 174 | self::QUERY_MODE_EXECUTE => 'execute', |
||
| 175 | self::QUERY_MODE_ROW => 'queryOne', |
||
| 176 | self::QUERY_MODE_ALL => 'queryAll', |
||
| 177 | self::QUERY_MODE_COLUMN => 'queryColumn', |
||
| 178 | self::QUERY_MODE_CURSOR => 'query', |
||
| 179 | self::QUERY_MODE_SCALAR => 'queryScalar', |
||
| 180 | self::QUERY_MODE_ROW | self::QUERY_MODE_EXECUTE => 'insertWithReturningPks' |
||
| 181 | }; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Executes a prepared statement. |
||
| 186 | * |
||
| 187 | * It's a wrapper around {@see PDOStatement::execute()} to support transactions and retry handlers. |
||
| 188 | * |
||
| 189 | * @param string|null $rawSql Deprecated. Use `null` value. Will be removed in version 2.0.0. |
||
| 190 | * |
||
| 191 | * @throws Exception |
||
| 192 | * @throws Throwable |
||
| 193 | */ |
||
| 194 | protected function internalExecute(string|null $rawSql): void |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @throws InvalidParamException |
||
| 226 | */ |
||
| 227 | protected function internalGetQueryResult(int $queryMode): mixed |
||
| 256 | } |
||
| 257 | |||
| 258 | protected function queryInternal(int $queryMode): mixed |
||
| 259 | { |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Refreshes table schema, which was marked by {@see requireTableSchemaRefresh()}. |
||
| 289 | */ |
||
| 290 | protected function refreshTableSchema(): void |
||
| 297 |