| Conditions | 5 |
| Paths | 5 |
| Total Lines | 58 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 48 | public function handle(Schedule $schedule) |
||
|
|
|||
| 49 | { |
||
| 50 | $tableName = $this->getTable(); |
||
| 51 | |||
| 52 | try { |
||
| 53 | $table = $this->getOts()->describeTable(['table_name' => $tableName]); |
||
| 54 | } catch (OTSServerException $exception) { |
||
| 55 | $this->error(sprintf('RequestId: %s', $exception->getRequestId())); |
||
| 56 | $this->error(sprintf('%s: %s', $exception->getOTSErrorCode(), $exception->getOTSErrorMessage())); |
||
| 57 | |||
| 58 | return; |
||
| 59 | } |
||
| 60 | |||
| 61 | if (!$this->confirm(sprintf('Are you sure you want to clear the "%s" table?', $tableName))) { |
||
| 62 | return; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** @var array $pks 表主键 */ |
||
| 66 | $pks = Arr::get($table, 'table_meta.primary_key_schema'); |
||
| 67 | |||
| 68 | /** 起始主键 */ |
||
| 69 | list($startPk, $endPk) = $this->parseRangePk($pks); |
||
| 70 | |||
| 71 | $rowCount = 0; |
||
| 72 | while (!empty($startPk)) { |
||
| 73 | $request = [ |
||
| 74 | 'table_name' => $tableName, 'max_versions' => 1, |
||
| 75 | 'direction' => DirectionConst::CONST_FORWARD, |
||
| 76 | 'inclusive_start_primary_key' => $startPk, |
||
| 77 | 'exclusive_end_primary_key' => $endPk, |
||
| 78 | 'limit' => 200, |
||
| 79 | ]; |
||
| 80 | $response = $this->getOts()->getRange($request); |
||
| 81 | |||
| 82 | /** 删除查询出来的数据 */ |
||
| 83 | if (!empty($rows = $this->parseDeleteRows($response))) { |
||
| 84 | $this->getOts()->batchWriteRow([ |
||
| 85 | 'tables' => [['table_name' => $tableName, 'rows' => $rows]], |
||
| 86 | ]); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** 下一次轮询的key */ |
||
| 90 | $startPk = $response['next_start_primary_key']; |
||
| 91 | |||
| 92 | $rowCount += count($rows); |
||
| 93 | $this->comment(sprintf( |
||
| 94 | '%s Delete %s rows from the "%s" table.', |
||
| 95 | Carbon::now()->format('Y-m-d H:i:s.u'), |
||
| 96 | count($rows), |
||
| 97 | $tableName |
||
| 98 | )); |
||
| 99 | } |
||
| 100 | |||
| 101 | $this->info(sprintf( |
||
| 102 | '%s The "%s" table has been cleared and %s data items have been deleted.', |
||
| 103 | Carbon::now()->format('Y-m-d H:i:s.u'), |
||
| 104 | $tableName, |
||
| 105 | $rowCount |
||
| 106 | )); |
||
| 163 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.