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