Conditions | 5 |
Paths | 5 |
Total Lines | 51 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
45 | public function handle(Schedule $schedule) |
||
|
|||
46 | { |
||
47 | $tableName = $this->getTable(); |
||
48 | |||
49 | try { |
||
50 | $table = $this->getOts()->describeTable(['table_name' => $tableName]); |
||
51 | } catch (OTSServerException $exception) { |
||
52 | $this->error(sprintf("RequestId: %s", $exception->getRequestId())); |
||
53 | $this->error(sprintf("%s: %s", $exception->getOTSErrorCode(), $exception->getOTSErrorMessage())); |
||
54 | return; |
||
55 | } |
||
56 | |||
57 | if (!$this->confirm(sprintf('Are you sure you want to clear the %s table?', $tableName))) { |
||
58 | return; |
||
59 | } |
||
60 | |||
61 | /** @var array $pks 表主键 */ |
||
62 | $pks = Arr::get($table, 'table_meta.primary_key_schema'); |
||
63 | |||
64 | /** 起始主键 */ |
||
65 | list($startPk, $endPk) = $this->parseRangePk($pks); |
||
66 | |||
67 | $rowCount = 0; |
||
68 | while (!empty ($startPk)) { |
||
69 | $request = [ |
||
70 | 'table_name' => $tableName, 'max_versions' => 1, |
||
71 | 'direction' => DirectionConst::CONST_FORWARD, |
||
72 | 'inclusive_start_primary_key' => $startPk, |
||
73 | 'exclusive_end_primary_key' => $endPk, |
||
74 | 'limit' => 200, |
||
75 | ]; |
||
76 | $response = $this->getOts()->getRange($request); |
||
77 | |||
78 | /** 删除查询出来的数据 */ |
||
79 | if (!empty($rows = $this->parseDeleteRows($response))) { |
||
80 | $this->getOts()->batchWriteRow([ |
||
81 | 'tables' => [['table_name' => $tableName, 'rows' => $rows]] |
||
82 | ]); |
||
83 | } |
||
84 | |||
85 | /** 下一次轮询的key */ |
||
86 | $startPk = $response['next_start_primary_key']; |
||
87 | |||
88 | $rowCount += count($rows); |
||
89 | $this->comment(sprintf('%s Delete %s rows from the %s table', |
||
90 | Carbon::now()->format('Y-m-d H:i:s.u'), count($rows), $tableName |
||
91 | )); |
||
92 | } |
||
93 | |||
94 | $this->info(sprintf('%s The %s table has been cleared and %s data items have been deleted', |
||
95 | Carbon::now()->format('Y-m-d H:i:s.u'), $tableName, $rowCount |
||
96 | )); |
||
148 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.