| Total Complexity | 42 | 
| Total Lines | 445 | 
| 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 | * 运行结果  | 
            ||
| 103 | *  | 
            ||
| 104 | * @var bool  | 
            ||
| 105 | */  | 
            ||
| 106 | protected $success;  | 
            ||
| 107 | |||
| 108 | /**  | 
            ||
| 109 | * Statement constructor.  | 
            ||
| 110 | * @param string $sql  | 
            ||
| 111 | * @param mixed ...$args  | 
            ||
| 112 | * @throws \suda\database\exception\SQLException  | 
            ||
| 113 | */  | 
            ||
| 114 | public function __construct(string $sql, ...$args)  | 
            ||
| 115 |     { | 
            ||
| 116 |         if (count($args) === 1 && is_array($args[0])) { | 
            ||
| 117 | $this->create($sql, $args[0]);  | 
            ||
| 118 |         } else { | 
            ||
| 119 | list($this->string, $this->binder) = $this->prepareQueryMark($sql, $args);  | 
            ||
| 120 | }  | 
            ||
| 121 | }  | 
            ||
| 122 | |||
| 123 | /**  | 
            ||
| 124 | * @param string $sql  | 
            ||
| 125 | * @param array $parameter  | 
            ||
| 126 | * @throws \suda\database\exception\SQLException  | 
            ||
| 127 | */  | 
            ||
| 128 | protected function create(string $sql, array $parameter)  | 
            ||
| 129 |     { | 
            ||
| 130 | list($this->string, $binder) = $this->prepareWhereString($sql, $parameter);  | 
            ||
| 131 | $this->binder = $this->mergeBinder($this->binder, $binder);  | 
            ||
| 132 | }  | 
            ||
| 133 | |||
| 134 | public function isRead(bool $set = null): bool  | 
            ||
| 135 |     { | 
            ||
| 136 |         if ($set !== null) { | 
            ||
| 137 | $this->type = self::READ;  | 
            ||
| 138 | }  | 
            ||
| 139 | return $this->type === self::READ;  | 
            ||
| 140 | }  | 
            ||
| 141 | |||
| 142 | public function isWrite(bool $set = null): bool  | 
            ||
| 143 |     { | 
            ||
| 144 |         if ($set !== null) { | 
            ||
| 145 | $this->type = self::WRITE;  | 
            ||
| 146 | }  | 
            ||
| 147 | return $this->type === self::WRITE;  | 
            ||
| 148 | }  | 
            ||
| 149 | |||
| 150 | /**  | 
            ||
| 151 | * 判断是否为一条  | 
            ||
| 152 | *  | 
            ||
| 153 | * @param bool|null $set  | 
            ||
| 154 | * @return boolean  | 
            ||
| 155 | */  | 
            ||
| 156 | public function isFetchOne(bool $set = null): bool  | 
            ||
| 157 |     { | 
            ||
| 158 |         if ($set !== null) { | 
            ||
| 159 | $this->fetch = self::FETCH_ONE;  | 
            ||
| 160 | }  | 
            ||
| 161 | return $this->fetch === self::FETCH_ONE;  | 
            ||
| 162 | }  | 
            ||
| 163 | |||
| 164 | /**  | 
            ||
| 165 | * 判断是否为一条  | 
            ||
| 166 | *  | 
            ||
| 167 | * @return boolean  | 
            ||
| 168 | */  | 
            ||
| 169 | public function isFetch(): bool  | 
            ||
| 170 |     { | 
            ||
| 171 | return $this->fetch !== null;  | 
            ||
| 172 | }  | 
            ||
| 173 | |||
| 174 | /**  | 
            ||
| 175 | * @param bool|null $scroll  | 
            ||
| 176 | */  | 
            ||
| 177 | public function setScroll(?bool $scroll): void  | 
            ||
| 178 |     { | 
            ||
| 179 | $this->scroll = $scroll;  | 
            ||
| 180 | }  | 
            ||
| 181 | |||
| 182 | /**  | 
            ||
| 183 | * 判断是否获取多条  | 
            ||
| 184 | *  | 
            ||
| 185 | * @param bool|null $set  | 
            ||
| 186 | * @return boolean  | 
            ||
| 187 | */  | 
            ||
| 188 | public function isFetchAll(bool $set = null): bool  | 
            ||
| 189 |     { | 
            ||
| 190 |         if ($set !== null) { | 
            ||
| 191 | $this->fetch = self::FETCH_ALL;  | 
            ||
| 192 | }  | 
            ||
| 193 | return $this->fetch === self::FETCH_ALL;  | 
            ||
| 194 | }  | 
            ||
| 195 | |||
| 196 | /**  | 
            ||
| 197 | * 设置记录类  | 
            ||
| 198 | *  | 
            ||
| 199 | * @param string|null $class  | 
            ||
| 200 | * @param array $args  | 
            ||
| 201 | * @return $this  | 
            ||
| 202 | */  | 
            ||
| 203 | public function setFetchType(?string $class = null, array $args = [])  | 
            ||
| 204 |     { | 
            ||
| 205 | $this->fetchClass = $class;  | 
            ||
| 206 | $this->fetchClassArgs = $args;  | 
            ||
| 207 | return $this;  | 
            ||
| 208 | }  | 
            ||
| 209 | |||
| 210 | /**  | 
            ||
| 211 | * 获取取值类  | 
            ||
| 212 | *  | 
            ||
| 213 | * @return string|null  | 
            ||
| 214 | */  | 
            ||
| 215 | public function getFetchClass(): ?string  | 
            ||
| 216 |     { | 
            ||
| 217 | return $this->fetchClass ?? null;  | 
            ||
| 218 | }  | 
            ||
| 219 | |||
| 220 | /**  | 
            ||
| 221 | * 是否滚动  | 
            ||
| 222 | *  | 
            ||
| 223 | * @param bool|null $set  | 
            ||
| 224 | * @return boolean|null  | 
            ||
| 225 | */  | 
            ||
| 226 | public function isScroll(bool $set = null): ?bool  | 
            ||
| 227 |     { | 
            ||
| 228 |         if ($set !== null) { | 
            ||
| 229 | $this->scroll = true;  | 
            ||
| 230 | }  | 
            ||
| 231 | return $this->scroll;  | 
            ||
| 232 | }  | 
            ||
| 233 | |||
| 234 | /**  | 
            ||
| 235 | * 获取SQL字符串  | 
            ||
| 236 | *  | 
            ||
| 237 | * @return string  | 
            ||
| 238 | */  | 
            ||
| 239 | public function getString()  | 
            ||
| 240 |     { | 
            ||
| 241 | return $this->getQuery()->getQuery();  | 
            ||
| 242 | }  | 
            ||
| 243 | |||
| 244 | /**  | 
            ||
| 245 | * 准备查询对象  | 
            ||
| 246 | *  | 
            ||
| 247 | * @return Query  | 
            ||
| 248 | */  | 
            ||
| 249 | protected function prepareQuery(): Query  | 
            ||
| 250 |     { | 
            ||
| 251 | return new Query($this->string, $this->binder);  | 
            ||
| 252 | }  | 
            ||
| 253 | |||
| 254 | /**  | 
            ||
| 255 | * 准备查询对象  | 
            ||
| 256 | *  | 
            ||
| 257 | * @return Query  | 
            ||
| 258 | */  | 
            ||
| 259 | public function prepare(): Query  | 
            ||
| 260 |     { | 
            ||
| 261 | return $this->query = $this->prepareQuery();  | 
            ||
| 262 | }  | 
            ||
| 263 | |||
| 264 | /**  | 
            ||
| 265 | * 获取查询对象  | 
            ||
| 266 | *  | 
            ||
| 267 | * @return Query  | 
            ||
| 268 | */  | 
            ||
| 269 | public function getQuery(): Query  | 
            ||
| 270 |     { | 
            ||
| 271 |         if ($this->query === null) { | 
            ||
| 272 | $this->query = $this->prepare();  | 
            ||
| 273 | }  | 
            ||
| 274 | return $this->query;  | 
            ||
| 275 | }  | 
            ||
| 276 | |||
| 277 | /**  | 
            ||
| 278 | * @param Query $query  | 
            ||
| 279 | */  | 
            ||
| 280 | public function setQuery(Query $query): void  | 
            ||
| 281 |     { | 
            ||
| 282 | $this->query = $query;  | 
            ||
| 283 | $this->string = $query->getQuery();  | 
            ||
| 284 | $this->binder = $query->getBinder();  | 
            ||
| 285 | }  | 
            ||
| 286 | |||
| 287 | /**  | 
            ||
| 288 | * 获取绑定信息  | 
            ||
| 289 | *  | 
            ||
| 290 | * @return Binder[]  | 
            ||
| 291 | */  | 
            ||
| 292 | public function getBinder()  | 
            ||
| 293 |     { | 
            ||
| 294 | return $this->binder;  | 
            ||
| 295 | }  | 
            ||
| 296 | |||
| 297 | /**  | 
            ||
| 298 | * @return string  | 
            ||
| 299 | */  | 
            ||
| 300 | public function __toString()  | 
            ||
| 301 |     { | 
            ||
| 302 | return $this->getString();  | 
            ||
| 303 | }  | 
            ||
| 304 | |||
| 305 | |||
| 306 | /**  | 
            ||
| 307 | * Get PDOStatement  | 
            ||
| 308 | *  | 
            ||
| 309 | * @return PDOStatement  | 
            ||
| 310 | */  | 
            ||
| 311 | public function getStatement(): ?PDOStatement  | 
            ||
| 312 |     { | 
            ||
| 313 | return $this->statement;  | 
            ||
| 314 | }  | 
            ||
| 315 | |||
| 316 | /**  | 
            ||
| 317 | * Set PDOStatement  | 
            ||
| 318 | *  | 
            ||
| 319 | * @param PDOStatement $statement PDOStatement  | 
            ||
| 320 | *  | 
            ||
| 321 | * @return $this  | 
            ||
| 322 | */  | 
            ||
| 323 | public function setStatement(PDOStatement $statement)  | 
            ||
| 324 |     { | 
            ||
| 325 | $this->statement = $statement;  | 
            ||
| 326 | |||
| 327 | return $this;  | 
            ||
| 328 | }  | 
            ||
| 329 | |||
| 330 | |||
| 331 | /**  | 
            ||
| 332 | * Get 返回类型  | 
            ||
| 333 | *  | 
            ||
| 334 | * @return int  | 
            ||
| 335 | */  | 
            ||
| 336 | public function getReturnType()  | 
            ||
| 337 |     { | 
            ||
| 338 | return $this->returnType;  | 
            ||
| 339 | }  | 
            ||
| 340 | |||
| 341 | /**  | 
            ||
| 342 | * Set 返回类型  | 
            ||
| 343 | *  | 
            ||
| 344 | * @param int $returnType 返回类型  | 
            ||
| 345 | * @return $this  | 
            ||
| 346 | */  | 
            ||
| 347 | public function setReturnType(int $returnType)  | 
            ||
| 348 |     { | 
            ||
| 349 | $this->returnType = $returnType;  | 
            ||
| 350 | return $this;  | 
            ||
| 351 | }  | 
            ||
| 352 | |||
| 353 | /**  | 
            ||
| 354 | * Get 参数  | 
            ||
| 355 | *  | 
            ||
| 356 | * @return array  | 
            ||
| 357 | */  | 
            ||
| 358 | public function getFetchClassArgs()  | 
            ||
| 359 |     { | 
            ||
| 360 | return $this->fetchClassArgs;  | 
            ||
| 361 | }  | 
            ||
| 362 | |||
| 363 | /**  | 
            ||
| 364 | * Get 数据处理中间件  | 
            ||
| 365 | * @return Middleware  | 
            ||
| 366 | */  | 
            ||
| 367 | public function getMiddleware()  | 
            ||
| 368 |     { | 
            ||
| 369 | return $this->middleware;  | 
            ||
| 370 | }  | 
            ||
| 371 | |||
| 372 | /**  | 
            ||
| 373 | * Set 数据处理中间件  | 
            ||
| 374 | *  | 
            ||
| 375 | * @param Middleware $middleware 数据处理中间件  | 
            ||
| 376 | * @return $this  | 
            ||
| 377 | */  | 
            ||
| 378 | public function setMiddleware(Middleware $middleware)  | 
            ||
| 379 |     { | 
            ||
| 380 | $this->middleware = $middleware;  | 
            ||
| 381 | return $this;  | 
            ||
| 382 | }  | 
            ||
| 383 | |||
| 384 | /**  | 
            ||
| 385 | * @param Binder $binder  | 
            ||
| 386 | * @return $this  | 
            ||
| 387 | */  | 
            ||
| 388 | public function addBinder(Binder $binder)  | 
            ||
| 389 |     { | 
            ||
| 390 |         if (!in_array($binder, $this->binder)) { | 
            ||
| 391 | $this->binder[] = $binder;  | 
            ||
| 392 | }  | 
            ||
| 393 | return $this;  | 
            ||
| 394 | }  | 
            ||
| 395 | |||
| 396 | /**  | 
            ||
| 397 | * 添加参数  | 
            ||
| 398 | *  | 
            ||
| 399 | * @param string $name  | 
            ||
| 400 | * @param mixed $value  | 
            ||
| 401 | * @param string|null $key  | 
            ||
| 402 | * @return $this  | 
            ||
| 403 | */  | 
            ||
| 404 | public function addValue(string $name, $value, ?string $key = null)  | 
            ||
| 405 |     { | 
            ||
| 406 | return $this->addBinder(new Binder($name, $value, $key));  | 
            ||
| 407 | }  | 
            ||
| 408 | |||
| 409 | /**  | 
            ||
| 410 | * @param int|null $type  | 
            ||
| 411 | */  | 
            ||
| 412 | public function setType(?int $type): void  | 
            ||
| 413 |     { | 
            ||
| 414 | $this->type = $type;  | 
            ||
| 415 | }  | 
            ||
| 416 | |||
| 417 | /**  | 
            ||
| 418 | * @return int  | 
            ||
| 419 | */  | 
            ||
| 420 | public function getFetch(): int  | 
            ||
| 421 |     { | 
            ||
| 422 | return $this->fetch;  | 
            ||
| 423 | }  | 
            ||
| 424 | |||
| 425 | /**  | 
            ||
| 426 | * @return int|null  | 
            ||
| 427 | */  | 
            ||
| 428 | public function getType(): ?int  | 
            ||
| 429 |     { | 
            ||
| 430 | return $this->type;  | 
            ||
| 431 | }  | 
            ||
| 432 | |||
| 433 | /**  | 
            ||
| 434 | * @param int $fetch  | 
            ||
| 435 | */  | 
            ||
| 436 | public function setFetch(int $fetch): void  | 
            ||
| 437 |     { | 
            ||
| 438 | $this->fetch = $fetch;  | 
            ||
| 439 | }  | 
            ||
| 440 | |||
| 441 | /**  | 
            ||
| 442 | * @return bool  | 
            ||
| 443 | */  | 
            ||
| 444 | public function isSuccess(): bool  | 
            ||
| 447 | }  | 
            ||
| 448 | |||
| 449 | /**  | 
            ||
| 450 | * @param bool $success  | 
            ||
| 451 | */  | 
            ||
| 452 | public function setSuccess(bool $success): void  | 
            ||
| 453 |     { | 
            ||
| 454 | $this->success = $success;  | 
            ||
| 455 | }  | 
            ||
| 456 | }  | 
            ||
| 457 |