Conditions | 7 |
Paths | 11 |
Total Lines | 58 |
Lines | 16 |
Ratio | 27.59 % |
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 | $units_processed = 0; |
||
78 | $ids_to_delete = $job_parameters->extra_datum('ids_to_delete', []); |
||
79 | $ids_remaining = []; |
||
80 | foreach($ids_to_delete as $model_name => $ids){ |
||
|
|||
81 | if($units_processed < $batch_size){ |
||
82 | $model = EE_Registry::instance()->load_model($model_name); |
||
83 | $ids_to_delete_this_query = array_slice($ids, 0, $batch_size, true); |
||
84 | View Code Duplication | if ($model->has_primary_key_field()) { |
|
85 | $where_conditions = [ |
||
86 | $model->primary_key_name() => [ |
||
87 | 'IN', |
||
88 | $ids |
||
89 | ] |
||
90 | ]; |
||
91 | } else { |
||
92 | $where_conditions = [ |
||
93 | 'OR' => [] |
||
94 | ]; |
||
95 | foreach ($ids as $index_primary_key_string) { |
||
96 | $keys_n_values = $model->parse_index_primary_key_string($index_primary_key_string); |
||
97 | $where_conditions['OR'][ 'AND*' . $index_primary_key_string ] = $keys_n_values; |
||
98 | } |
||
99 | } |
||
100 | $deletion_count = $model->delete_permanently( |
||
101 | [ |
||
102 | $where_conditions |
||
103 | ], |
||
104 | false |
||
105 | ); |
||
106 | $units_processed += $deletion_count; |
||
107 | $remaining_ids = array_diff_key($ids, $ids_to_delete_this_query); |
||
108 | // If there's any more from this model, we'll do them next time. |
||
109 | if(count($remaining_ids) > 0){ |
||
110 | $ids_remaining[$model_name] = $remaining_ids; |
||
111 | } |
||
112 | } else { |
||
113 | $ids_remaining[$model_name] = $ids_to_delete[$model_name]; |
||
114 | } |
||
115 | $job_parameters->mark_processed($units_processed); |
||
116 | if(empty($ids_remaining)){ |
||
117 | $job_parameters->set_status(JobParameters::status_complete); |
||
118 | return new JobStepResponse( |
||
119 | $job_parameters, |
||
120 | esc_html__('Deletion complete.', 'event_espresso') |
||
121 | ); |
||
122 | } |
||
123 | $job_parameters->add_extra_data('ids_to_delete', $ids_remaining); |
||
124 | return new JobStepResponse( |
||
125 | $job_parameters, |
||
126 | sprintf( |
||
127 | esc_html__('Deleted %d items.', 'event_espresso'), |
||
128 | $units_processed |
||
129 | ) |
||
130 | ); |
||
131 | } |
||
132 | } |
||
133 | |||
149 |
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.