Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
26 | class AttendeesReport extends JobHandlerFile { |
||
27 | |||
28 | |||
29 | public function create_job(JobParameters $job_parameters) { |
||
30 | if( ! \EE_Capabilities::instance()->current_user_can( 'ee_read_contacts', 'generating_report' ) ) { |
||
31 | throw new BatchRequestException( |
||
32 | __( 'You do not have permission to view contacts', 'event_espresso') |
||
33 | ); |
||
34 | } |
||
35 | $filepath = $this->create_file_from_job_with_name( |
||
36 | $job_parameters->job_id(), |
||
37 | __('contact-list-report.csv', 'event_espresso') |
||
38 | ); |
||
39 | $job_parameters->add_extra_data( 'filepath', $filepath ); |
||
40 | $job_parameters->set_job_size( $this->count_units_to_process() ); |
||
41 | //we should also set the header columns |
||
42 | $csv_data_for_row = $this->get_csv_data( 0, 1 ); |
||
43 | \EE_Registry::instance()->load_helper( 'Export' ); |
||
44 | \EEH_Export::write_data_array_to_csv( $filepath, $csv_data_for_row, true ); |
||
45 | //if we actually processed a row there, record it |
||
46 | if( $job_parameters->job_size() ) { |
||
47 | $job_parameters->mark_processed( 1 ); |
||
48 | } |
||
49 | return new JobStepResponse( |
||
50 | $job_parameters, |
||
51 | __( 'Contacts report started successfully...', 'event_espresso' ) |
||
52 | ); |
||
53 | } |
||
54 | |||
55 | |||
56 | public function continue_job(JobParameters $job_parameters, $batch_size = 50) { |
||
57 | $csv_data = $this->get_csv_data( $job_parameters->units_processed(), $batch_size ); |
||
58 | \EE_Registry::instance()->load_helper( 'Export' ); |
||
59 | \EEH_Export::write_data_array_to_csv( $job_parameters->extra_datum( 'filepath' ), $csv_data, false ); |
||
|
|||
60 | $units_processed = count( $csv_data ); |
||
61 | $job_parameters->mark_processed( $units_processed ); |
||
62 | $extra_response_data = array( |
||
63 | 'file_url' => '' |
||
64 | ); |
||
65 | View Code Duplication | if( $units_processed < $batch_size ) { |
|
66 | $job_parameters->set_status( JobParameters::status_complete ); |
||
67 | $extra_response_data[ 'file_url' ] = $this->get_url_to_file( $job_parameters->extra_datum( 'filepath' ) ); |
||
68 | } |
||
69 | return new JobStepResponse( |
||
70 | $job_parameters, |
||
71 | sprintf( |
||
72 | __( 'Wrote %1$s rows to report CSV file...', 'event_espresso' ), |
||
73 | count( $csv_data ) ), |
||
74 | $extra_response_data ); |
||
75 | } |
||
76 | |||
77 | |||
78 | View Code Duplication | public function cleanup_job(JobParameters $job_parameters) { |
|
79 | $this->_file_helper->delete( |
||
80 | \EEH_File::remove_filename_from_filepath( $job_parameters->extra_datum( 'filepath' ) ), |
||
81 | true, |
||
82 | 'd' |
||
83 | ); |
||
84 | return new JobStepResponse( $job_parameters, __( 'Cleaned up temporary file', 'event_espresso' ) ); |
||
85 | } |
||
86 | |||
87 | public function count_units_to_process() { |
||
90 | public function get_csv_data( $offset, $limit ) { |
||
91 | $attendee_rows = \EEM_Attendee::instance()->get_all_wpdb_results( |
||
92 | array( |
||
93 | 'limit' => array( $offset, $limit ), |
||
113 | |||
114 | } |
||
115 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.