Conditions | 11 |
Paths | 7 |
Total Lines | 62 |
Code Lines | 35 |
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 |
||
110 | protected function segmentBatches($source) |
||
111 | { |
||
112 | // Measure batch_size |
||
113 | $batchSize = static::config()->get('batch_size'); |
||
114 | if ($batchSize === 0) { |
||
115 | return array($source); |
||
116 | } |
||
117 | $softCap = static::config()->get('batch_soft_cap'); |
||
118 | |||
119 | // Clear batches |
||
120 | $batches = array(); |
||
121 | $current = array(); |
||
122 | $currentSize = 0; |
||
123 | |||
124 | // Build batches from data |
||
125 | foreach ($source as $base => $statefulids) { |
||
126 | if (!$statefulids) { |
||
127 | continue; |
||
128 | } |
||
129 | |||
130 | foreach ($statefulids as $stateKey => $statefulid) { |
||
131 | $state = $statefulid['state']; |
||
132 | $ids = $statefulid['ids']; |
||
133 | if (!$ids) { |
||
134 | continue; |
||
135 | } |
||
136 | |||
137 | // Extract items from $ids until empty |
||
138 | while ($ids) { |
||
139 | // Estimate maximum number of items to take for this iteration, allowing for the soft cap |
||
140 | $take = $batchSize - $currentSize; |
||
141 | if (count($ids) <= $take + $softCap) { |
||
142 | $take += $softCap; |
||
143 | } |
||
144 | $items = array_slice($ids, 0, $take, true); |
||
145 | $ids = array_slice($ids, count($items), null, true); |
||
146 | |||
147 | // Update batch |
||
148 | $currentSize += count($items); |
||
149 | $merge = array( |
||
150 | $base => array( |
||
151 | $stateKey => array( |
||
152 | 'state' => $state, |
||
153 | 'ids' => $items |
||
154 | ) |
||
155 | ) |
||
156 | ); |
||
157 | $current = $current ? array_merge_recursive($current, $merge) : $merge; |
||
158 | if ($currentSize >= $batchSize) { |
||
159 | $batches[] = $current; |
||
160 | $current = array(); |
||
161 | $currentSize = 0; |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | // Add incomplete batch |
||
167 | if ($currentSize) { |
||
168 | $batches[] = $current; |
||
169 | } |
||
170 | |||
171 | return $batches; |
||
172 | } |
||
185 |