| Conditions | 3 |
| Paths | 4 |
| Total Lines | 58 |
| Lines | 36 |
| Ratio | 62.07 % |
| 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 |
||
| 36 | public function create_job(JobParameters $job_parameters) |
||
| 37 | { |
||
| 38 | // Set the "root" model objects we will want to delete (record their ID and model) |
||
| 39 | $event_ids = $job_parameters->request_datum('EVT_ID', array()); |
||
| 40 | // Find all the root nodes to delete (this isn't just events, because there's other data, like related tickets, |
||
| 41 | // prices, message templates, etc, whose model definition doesn't make them dependent on events. But, |
||
| 42 | // we have no UI to access them independent of events, so they may as well get deleted too.) |
||
| 43 | $model_objects_to_delete = []; |
||
| 44 | View Code Duplication | foreach ($event_ids as $event_id) { |
|
|
|
|||
| 45 | $event = EEM_Event::instance()->get_one_by_ID($event_id); |
||
| 46 | // $model_objects_to_delete[] = $event; |
||
| 47 | // Also, we want to delete their related, non-global, tickets, prices and message templates |
||
| 48 | $related_non_global_tickets = EEM_Ticket::instance()->get_all_deleted_and_undeleted( |
||
| 49 | [ |
||
| 50 | [ |
||
| 51 | 'TKT_is_default' => false, |
||
| 52 | 'Datetime.EVT_ID' => $event_id |
||
| 53 | ] |
||
| 54 | ] |
||
| 55 | ); |
||
| 56 | $related_non_global_prices = EEM_Price::instance()->get_all_deleted_and_undeleted( |
||
| 57 | [ |
||
| 58 | [ |
||
| 59 | 'PRC_is_default' => false, |
||
| 60 | 'Ticket.Datetime.EVT_ID' => $event_id |
||
| 61 | ] |
||
| 62 | ] |
||
| 63 | ); |
||
| 64 | $related_message_templates = $event->get_many_related( |
||
| 65 | 'Message_Template_Group', |
||
| 66 | [ |
||
| 67 | [ |
||
| 68 | 'MTP_is_global' => false |
||
| 69 | ] |
||
| 70 | ] |
||
| 71 | ); |
||
| 72 | $model_objects_to_delete = array_merge( |
||
| 73 | $model_objects_to_delete, |
||
| 74 | [$event], |
||
| 75 | $related_non_global_tickets, |
||
| 76 | $related_non_global_prices, |
||
| 77 | $related_message_templates |
||
| 78 | ); |
||
| 79 | } |
||
| 80 | $roots = []; |
||
| 81 | foreach($model_objects_to_delete as $model_object){ |
||
| 82 | $roots[] = new ModelObjNode($model_objects_to_delete); |
||
| 83 | } |
||
| 84 | $job_parameters->add_extra_data('roots', $roots); |
||
| 85 | // Set an estimate of how long this will take (we're discovering as we go, so it seems impossible to give |
||
| 86 | // an accurate count.) |
||
| 87 | $estimated_work_per_model_obj = 100; |
||
| 88 | $job_parameters->set_job_size(count($roots) * $estimated_work_per_model_obj); |
||
| 89 | return new JobStepResponse( |
||
| 90 | $job_parameters, |
||
| 91 | esc_html__('Identified main items for deletion...', 'event_espresso') |
||
| 92 | ); |
||
| 93 | } |
||
| 94 | |||
| 153 |
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.