| Total Complexity | 40 |
| Total Lines | 416 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Statement 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 Statement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | abstract class Statement |
||
| 11 | { |
||
| 12 | use PrepareTrait; |
||
| 13 | |||
| 14 | const WRITE = 0; |
||
| 15 | const READ = 1; |
||
| 16 | |||
| 17 | const FETCH_ONE = 0; |
||
| 18 | const FETCH_ALL = 1; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * 语句类型 |
||
| 22 | * |
||
| 23 | * @var int|null |
||
| 24 | */ |
||
| 25 | protected $type = null; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * 获取类型 |
||
| 29 | * |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | protected $fetch = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * 滚动 |
||
| 36 | * |
||
| 37 | * @var boolean|null |
||
| 38 | */ |
||
| 39 | protected $scroll = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * 绑定 |
||
| 43 | * |
||
| 44 | * @var Binder[] |
||
| 45 | */ |
||
| 46 | protected $binder = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * SQL语句 |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $string; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * PDOStatement |
||
| 57 | * |
||
| 58 | * @var PDOStatement|null |
||
| 59 | */ |
||
| 60 | protected $statement = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Query |
||
| 64 | * |
||
| 65 | * @var Query |
||
| 66 | */ |
||
| 67 | protected $query; |
||
| 68 | |||
| 69 | const RET_ROWS = 1; |
||
| 70 | const RET_LAST_INSERT_ID = 2; |
||
| 71 | const RET_BOOL = 3; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * 返回类型 |
||
| 75 | * |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | protected $returnType = Statement::RET_BOOL; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * 类 |
||
| 82 | * |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $fetchClass; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * 参数 |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $fetchClassArgs = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * 数据处理中间件 |
||
| 96 | * |
||
| 97 | * @var Middleware |
||
| 98 | */ |
||
| 99 | protected $middleware; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Statement constructor. |
||
| 103 | * @param string $sql |
||
| 104 | * @param mixed ...$args |
||
| 105 | */ |
||
| 106 | public function __construct(string $sql, ...$args) |
||
| 107 | { |
||
| 108 | if (count($args) === 1 && is_array($args[0])) { |
||
| 109 | $this->create($sql, $args[0]); |
||
| 110 | } else { |
||
| 111 | list($this->string, $this->binder) = $this->prepareQueryMark($sql, $args); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | protected function create(string $sql, array $parameter) |
||
| 116 | { |
||
| 117 | $this->string = $sql; |
||
| 118 | $this->binder = $this->mergeBinder($this->binder, $parameter); |
||
| 119 | } |
||
| 120 | |||
| 121 | public function isRead(bool $set = null): bool |
||
| 122 | { |
||
| 123 | if ($set !== null) { |
||
| 124 | $this->type = self::READ; |
||
| 125 | } |
||
| 126 | return $this->type === self::READ; |
||
| 127 | } |
||
| 128 | |||
| 129 | public function isWrite(bool $set = null): bool |
||
| 130 | { |
||
| 131 | if ($set !== null) { |
||
| 132 | $this->type = self::WRITE; |
||
| 133 | } |
||
| 134 | return $this->type === self::WRITE; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * 判断是否为一条 |
||
| 139 | * |
||
| 140 | * @param bool|null $set |
||
| 141 | * @return boolean |
||
| 142 | */ |
||
| 143 | public function isFetchOne(bool $set = null): bool |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * 判断是否为一条 |
||
| 153 | * |
||
| 154 | * @return boolean |
||
| 155 | */ |
||
| 156 | public function isFetch(): bool |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param bool|null $scroll |
||
| 163 | */ |
||
| 164 | public function setScroll(?bool $scroll): void |
||
| 165 | { |
||
| 166 | $this->scroll = $scroll; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * 判断是否获取多条 |
||
| 171 | * |
||
| 172 | * @param bool|null $set |
||
| 173 | * @return boolean |
||
| 174 | */ |
||
| 175 | public function isFetchAll(bool $set = null): bool |
||
| 176 | { |
||
| 177 | if ($set !== null) { |
||
| 178 | $this->fetch = self::FETCH_ALL; |
||
| 179 | } |
||
| 180 | return $this->fetch === self::FETCH_ALL; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * 设置记录类 |
||
| 185 | * |
||
| 186 | * @param string|null $class |
||
| 187 | * @param array $args |
||
| 188 | * @return $this |
||
| 189 | */ |
||
| 190 | public function setFetchType(?string $class = null, array $args = []) |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * 获取取值类 |
||
| 199 | * |
||
| 200 | * @return string|null |
||
| 201 | */ |
||
| 202 | public function getFetchClass(): ?string |
||
| 203 | { |
||
| 204 | return $this->fetchClass ?? null; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * 是否滚动 |
||
| 209 | * |
||
| 210 | * @param bool|null $set |
||
| 211 | * @return boolean|null |
||
| 212 | */ |
||
| 213 | public function isScroll(bool $set = null): ?bool |
||
| 214 | { |
||
| 215 | if ($set !== null) { |
||
| 216 | $this->scroll = true; |
||
| 217 | } |
||
| 218 | return $this->scroll; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * 获取SQL字符串 |
||
| 223 | * |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function getString() |
||
| 227 | { |
||
| 228 | return $this->getQuery()->getQuery(); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * 准备查询对象 |
||
| 233 | * |
||
| 234 | * @return Query |
||
| 235 | */ |
||
| 236 | protected function prepareQuery(): Query |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * 准备查询对象 |
||
| 243 | * |
||
| 244 | * @return Query |
||
| 245 | */ |
||
| 246 | public function prepare(): Query |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * 获取查询对象 |
||
| 253 | * |
||
| 254 | * @return Query |
||
| 255 | */ |
||
| 256 | public function getQuery(): Query |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param Query $query |
||
| 266 | */ |
||
| 267 | public function setQuery(Query $query): void |
||
| 268 | { |
||
| 269 | $this->query = $query; |
||
| 270 | $this->string = $query->getQuery(); |
||
| 271 | $this->binder = $query->getBinder(); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * 获取绑定信息 |
||
| 276 | * |
||
| 277 | * @return Binder[] |
||
| 278 | */ |
||
| 279 | public function getBinder() |
||
| 280 | { |
||
| 281 | return $this->binder; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function __toString() |
||
| 288 | { |
||
| 289 | return $this->getString(); |
||
| 290 | } |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * Get PDOStatement |
||
| 295 | * |
||
| 296 | * @return PDOStatement |
||
| 297 | */ |
||
| 298 | public function getStatement(): ?PDOStatement |
||
| 299 | { |
||
| 300 | return $this->statement; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Set PDOStatement |
||
| 305 | * |
||
| 306 | * @param PDOStatement $statement PDOStatement |
||
| 307 | * |
||
| 308 | * @return $this |
||
| 309 | */ |
||
| 310 | public function setStatement(PDOStatement $statement) |
||
| 311 | { |
||
| 312 | $this->statement = $statement; |
||
| 313 | |||
| 314 | return $this; |
||
| 315 | } |
||
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * Get 返回类型 |
||
| 320 | * |
||
| 321 | * @return int |
||
| 322 | */ |
||
| 323 | public function getReturnType() |
||
| 324 | { |
||
| 325 | return $this->returnType; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Set 返回类型 |
||
| 330 | * |
||
| 331 | * @param int $returnType 返回类型 |
||
| 332 | * @return $this |
||
| 333 | */ |
||
| 334 | public function setReturnType(int $returnType) |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get 参数 |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | public function getFetchClassArgs() |
||
| 346 | { |
||
| 347 | return $this->fetchClassArgs; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get 数据处理中间件 |
||
| 352 | * @return Middleware |
||
| 353 | */ |
||
| 354 | public function getMiddleware() |
||
| 355 | { |
||
| 356 | return $this->middleware; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Set 数据处理中间件 |
||
| 361 | * |
||
| 362 | * @param Middleware $middleware 数据处理中间件 |
||
| 363 | * @return $this |
||
| 364 | */ |
||
| 365 | public function setMiddleware(Middleware $middleware) |
||
| 366 | { |
||
| 367 | $this->middleware = $middleware; |
||
| 368 | return $this; |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param Binder $binder |
||
| 373 | * @return $this |
||
| 374 | */ |
||
| 375 | public function addBinder(Binder $binder) |
||
| 376 | { |
||
| 377 | if (!in_array($binder, $this->binder)) { |
||
| 378 | $this->binder[] = $binder; |
||
| 379 | } |
||
| 380 | return $this; |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * 添加参数 |
||
| 385 | * |
||
| 386 | * @param string $name |
||
| 387 | * @param mixed $value |
||
| 388 | * @param string|null $key |
||
| 389 | * @return $this |
||
| 390 | */ |
||
| 391 | public function addValue(string $name, $value, ?string $key = null) |
||
| 392 | { |
||
| 393 | return $this->addBinder(new Binder($name, $value, $key)); |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param int|null $type |
||
| 398 | */ |
||
| 399 | public function setType(?int $type): void |
||
| 400 | { |
||
| 401 | $this->type = $type; |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @return int |
||
| 406 | */ |
||
| 407 | public function getFetch(): int |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @return int|null |
||
| 414 | */ |
||
| 415 | public function getType(): ?int |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param int $fetch |
||
| 422 | */ |
||
| 423 | public function setFetch(int $fetch): void |
||
| 426 | } |
||
| 427 | } |
||
| 428 |