Conditions | 8 |
Paths | 4 |
Total Lines | 58 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |
||
108 | public function runTable( $params ) { |
||
109 | $dbr = $this->getDB( DB_REPLICA ); |
||
110 | |||
111 | if ( array_diff( array_keys( $params ), |
||
112 | [ 'table', 'conds', 'index', 'callback' ] ) |
||
113 | ) { |
||
114 | throw new MWException( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) ); |
||
115 | } |
||
116 | |||
117 | $table = $params['table']; |
||
118 | // count(*) would melt the DB for huge tables, we can estimate here |
||
119 | $count = $dbr->estimateRowCount( $table, '*', '', __METHOD__ ); |
||
120 | $this->init( $count, $table ); |
||
121 | $this->output( "Processing $table...\n" ); |
||
122 | |||
123 | $index = (array)$params['index']; |
||
124 | $indexConds = []; |
||
125 | $options = [ |
||
126 | 'ORDER BY' => implode( ',', $index ), |
||
127 | 'LIMIT' => $this->batchSize |
||
128 | ]; |
||
129 | $callback = [ $this, $params['callback'] ]; |
||
130 | |||
131 | while ( true ) { |
||
132 | $conds = array_merge( $params['conds'], $indexConds ); |
||
133 | $res = $dbr->select( $table, '*', $conds, __METHOD__, $options ); |
||
134 | if ( !$res->numRows() ) { |
||
135 | // Done |
||
136 | break; |
||
137 | } |
||
138 | |||
139 | foreach ( $res as $row ) { |
||
140 | call_user_func( $callback, $row ); |
||
141 | } |
||
142 | |||
143 | if ( $res->numRows() < $this->batchSize ) { |
||
144 | // Done |
||
145 | break; |
||
146 | } |
||
147 | |||
148 | // Update the conditions to select the next batch. |
||
149 | // Construct a condition string by starting with the least significant part |
||
150 | // of the index, and adding more significant parts progressively to the left |
||
151 | // of the string. |
||
152 | $nextCond = ''; |
||
153 | foreach ( array_reverse( $index ) as $field ) { |
||
154 | $encValue = $dbr->addQuotes( $row->$field ); |
||
|
|||
155 | if ( $nextCond === '' ) { |
||
156 | $nextCond = "$field > $encValue"; |
||
157 | } else { |
||
158 | $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))"; |
||
159 | } |
||
160 | } |
||
161 | $indexConds = [ $nextCond ]; |
||
162 | } |
||
163 | |||
164 | $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" ); |
||
165 | } |
||
166 | |||
175 |
It seems like you are relying on a variable being defined by an iteration: