Conditions | 3 |
Paths | 4 |
Total Lines | 57 |
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 |
||
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_IDs', 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 | foreach ($event_ids as $event_id) { |
||
|
|||
45 | $event = EEM_Event::instance()->get_one_by_ID($event_id); |
||
46 | // Also, we want to delete their related, non-global, tickets, prices and message templates |
||
47 | $related_non_global_tickets = EEM_Ticket::instance()->get_all_deleted_and_undeleted( |
||
48 | [ |
||
49 | [ |
||
50 | 'TKT_is_default' => false, |
||
51 | 'Datetime.EVT_ID' => $event_id |
||
52 | ] |
||
53 | ] |
||
54 | ); |
||
55 | $related_non_global_prices = EEM_Price::instance()->get_all_deleted_and_undeleted( |
||
56 | [ |
||
57 | [ |
||
58 | 'PRC_is_default' => false, |
||
59 | 'Ticket.Datetime.EVT_ID' => $event_id |
||
60 | ] |
||
61 | ] |
||
62 | ); |
||
63 | $related_message_templates = $event->get_many_related( |
||
64 | 'Message_Template_Group', |
||
65 | [ |
||
66 | [ |
||
67 | 'MTP_is_global' => false |
||
68 | ] |
||
69 | ] |
||
70 | ); |
||
71 | $model_objects_to_delete = array_merge( |
||
72 | $model_objects_to_delete, |
||
73 | [$event], |
||
74 | $related_non_global_tickets, |
||
75 | $related_non_global_prices, |
||
76 | $related_message_templates |
||
77 | ); |
||
78 | } |
||
79 | $roots = []; |
||
80 | foreach ($model_objects_to_delete as $model_object) { |
||
81 | $roots[] = new ModelObjNode($model_object->ID(), $model_object->get_model()); |
||
82 | } |
||
83 | $job_parameters->add_extra_data('roots', $roots); |
||
84 | // Set an estimate of how long this will take (we're discovering as we go, so it seems impossible to give |
||
85 | // an accurate count.) |
||
86 | $estimated_work_per_model_obj = 100; |
||
87 | $job_parameters->set_job_size(count($roots) * $estimated_work_per_model_obj); |
||
88 | return new JobStepResponse( |
||
89 | $job_parameters, |
||
90 | esc_html__('Generating preview of data to be deleted...', 'event_espresso') |
||
91 | ); |
||
92 | } |
||
93 | |||
165 |
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.