Conditions | 7 |
Paths | 12 |
Total Lines | 65 |
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 |
||
90 | public function continue_job(JobParameters $job_parameters, $batch_size = 50) |
||
91 | { |
||
92 | // We already have the items IDs. So deleting is really fast. Let's speed it up. |
||
93 | $batch_size *= 10; |
||
94 | $units_processed = 0; |
||
95 | $models_and_ids_to_delete = $job_parameters->extra_datum('models_and_ids_to_delete', []); |
||
96 | // Build a new list of everything leftover after this request's of deletions. |
||
97 | $models_and_ids_remaining = []; |
||
98 | foreach ($models_and_ids_to_delete as $model_name => $ids_to_delete) { |
||
|
|||
99 | if ($units_processed < $batch_size) { |
||
100 | $model = EE_Registry::instance()->load_model($model_name); |
||
101 | $ids_to_delete_this_query = array_slice($ids_to_delete, 0, $batch_size - $units_processed, true); |
||
102 | if ($model->has_primary_key_field()) { |
||
103 | $where_conditions = [ |
||
104 | $model->primary_key_name() => [ |
||
105 | 'IN', |
||
106 | $ids_to_delete_this_query |
||
107 | ] |
||
108 | ]; |
||
109 | } else { |
||
110 | $where_conditions = [ |
||
111 | 'OR' => [] |
||
112 | ]; |
||
113 | foreach ($ids_to_delete_this_query as $index_primary_key_string) { |
||
114 | $keys_n_values = $model->parse_index_primary_key_string($index_primary_key_string); |
||
115 | $where_conditions['OR'][ 'AND*' . $index_primary_key_string ] = $keys_n_values; |
||
116 | } |
||
117 | } |
||
118 | // Deleting time! |
||
119 | // The model's deletion method reports every ROW deleted, and in the case of CPT models that will be |
||
120 | // two rows deleted for event CPT item. So don't rely on it for the count of items deleted. |
||
121 | $model->delete_permanently( |
||
122 | [ |
||
123 | $where_conditions |
||
124 | ], |
||
125 | false |
||
126 | ); |
||
127 | $units_processed += count($ids_to_delete_this_query); |
||
128 | $remaining_ids = array_diff_key($ids_to_delete, $ids_to_delete_this_query); |
||
129 | // If there's any more from this model, we'll do them next time. |
||
130 | if (count($remaining_ids) > 0) { |
||
131 | $models_and_ids_remaining[ $model_name ] = $remaining_ids; |
||
132 | } |
||
133 | } else { |
||
134 | $models_and_ids_remaining[ $model_name ] = $models_and_ids_to_delete[ $model_name ]; |
||
135 | } |
||
136 | } |
||
137 | $job_parameters->mark_processed($units_processed); |
||
138 | // All done deleting for this request. Is there anything to do next time? |
||
139 | if (empty($models_and_ids_remaining)) { |
||
140 | $job_parameters->set_status(JobParameters::status_complete); |
||
141 | return new JobStepResponse( |
||
142 | $job_parameters, |
||
143 | esc_html__('Deletion complete.', 'event_espresso') |
||
144 | ); |
||
145 | } |
||
146 | $job_parameters->add_extra_data('models_and_ids_to_delete', $models_and_ids_remaining); |
||
147 | return new JobStepResponse( |
||
148 | $job_parameters, |
||
149 | sprintf( |
||
150 | esc_html__('Deleted %d items.', 'event_espresso'), |
||
151 | $units_processed |
||
152 | ) |
||
153 | ); |
||
154 | } |
||
155 | |||
190 |
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.