| Conditions | 9 |
| Paths | 7 |
| Total Lines | 53 |
| 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 |
||
| 103 | public function continue_job(JobParameters $job_parameters, $batch_size = 50) |
||
| 104 | { |
||
| 105 | // Serializing and unserializing is what really makes this drag on (eg on localhost, the ajax requests took |
||
| 106 | // about 4 seconds when the batch size was 250, but 3 seconds when the batch size was 50. So like |
||
| 107 | // 50% of the request is just serializing and unserializing.) So, make the batches much bigger. |
||
| 108 | $batch_size *= 3; |
||
| 109 | $units_processed = 0; |
||
| 110 | foreach ($job_parameters->extra_datum('roots', array()) as $root_node) { |
||
| 111 | if ($units_processed >= $batch_size) { |
||
| 112 | break; |
||
| 113 | } |
||
| 114 | if (! $root_node instanceof ModelObjNode) { |
||
| 115 | throw new InvalidClassException('ModelObjNode'); |
||
| 116 | } |
||
| 117 | if ($root_node->isComplete()) { |
||
| 118 | continue; |
||
| 119 | } |
||
| 120 | $units_processed += $root_node->visit($batch_size - $units_processed); |
||
| 121 | } |
||
| 122 | $job_parameters->mark_processed($units_processed); |
||
| 123 | // If the most-recently processed root node is complete, we must be all done because we're doing them |
||
| 124 | // sequentially. |
||
| 125 | if (isset($root_node) && $root_node instanceof ModelObjNode && $root_node->isComplete()) { |
||
| 126 | $job_parameters->set_status(JobParameters::status_complete); |
||
| 127 | // Show a full progress bar. |
||
| 128 | $job_parameters->set_units_processed($job_parameters->job_size()); |
||
| 129 | $deletion_job_code = $job_parameters->request_datum('deletion_job_code'); |
||
| 130 | $this->model_obj_node_group_persister->persistModelObjNodesGroup( |
||
| 131 | $job_parameters->extra_datum('roots'), |
||
| 132 | $deletion_job_code |
||
| 133 | ); |
||
| 134 | return new JobStepResponse( |
||
| 135 | $job_parameters, |
||
| 136 | esc_html__('Finished identifying items for deletion.', 'event_espresso'), |
||
| 137 | [ |
||
| 138 | 'deletion_job_code' => $deletion_job_code |
||
| 139 | ] |
||
| 140 | ); |
||
| 141 | } else { |
||
| 142 | // Because the job size was a guess, it may have likely been provden wrong. We don't want to show more work |
||
| 143 | // done than we originally said there would be. So adjust the estimate. |
||
| 144 | if (($job_parameters->units_processed() / $job_parameters->job_size()) > .8) { |
||
| 145 | $job_parameters->set_job_size($job_parameters->job_size() * 2); |
||
| 146 | } |
||
| 147 | return new JobStepResponse( |
||
| 148 | $job_parameters, |
||
| 149 | sprintf( |
||
| 150 | esc_html__('Identified %d items for deletion.', 'event_espresso'), |
||
| 151 | $units_processed |
||
| 152 | ) |
||
| 153 | ); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 173 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.