| Conditions | 8 |
| Paths | 64 |
| Total Lines | 62 |
| Code Lines | 27 |
| 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 |
||
| 72 | public function rebuild() { |
||
|
|
|||
| 73 | |||
| 74 | ob_clean(); |
||
| 75 | |||
| 76 | // Give ourselves some time to process the data. |
||
| 77 | set_time_limit( 21600 ); // 6 hours |
||
| 78 | |||
| 79 | // Send textual output. |
||
| 80 | header( 'Content-type: text/plain; charset=utf-8' ); |
||
| 81 | |||
| 82 | // We start at 0 by default and get to max. |
||
| 83 | $offset = $_GET['offset'] ?: 0; |
||
| 84 | $limit = $_GET['limit'] ?: 1; |
||
| 85 | $max = $offset + $limit; |
||
| 86 | |||
| 87 | // Whether we should run queries asynchronously, this is handled in `wordlift_constants.php`. |
||
| 88 | $asynchronous = isset( $_GET['wl-async'] ) && 'true' === $_GET['wl-async']; |
||
| 89 | |||
| 90 | // If we're starting at offset 0, then delete existing URIs and data from |
||
| 91 | // the remote dataset. |
||
| 92 | if ( 0 === $offset ) { |
||
| 93 | |||
| 94 | // Clear out all generated URIs, since the dataset URI might have changed |
||
| 95 | // in the process. |
||
| 96 | $this->uri_service->delete_all(); |
||
| 97 | |||
| 98 | // Delete all the triples in the remote dataset. |
||
| 99 | $this->sparql_service->execute( 'DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };' ); |
||
| 100 | |||
| 101 | } |
||
| 102 | |||
| 103 | // Go through the list of published entities and posts and call the (legacy) |
||
| 104 | // `wl_linked_data_save_post` function for each one. We're using the `process` |
||
| 105 | // function which is provided by the parent `Wordlift_Listable` abstract class |
||
| 106 | // and will cycle through all the posts w/ a very small memory footprint |
||
| 107 | // in order to avoid memory errors. |
||
| 108 | |||
| 109 | $count = 0; |
||
| 110 | $log = $this->log; |
||
| 111 | $this->process( function ( $post ) use ( &$count, $log ) { |
||
| 112 | $count ++; |
||
| 113 | $log->trace( "Going to save post $count, ID $post->ID..." ); |
||
| 114 | wl_linked_data_save_post( $post->ID ); |
||
| 115 | }, array( |
||
| 116 | 'post_status' => 'publish', |
||
| 117 | ), $offset, $max ); |
||
| 118 | |||
| 119 | // Redirect to the next chunk. |
||
| 120 | if ( $count == $limit ) { |
||
| 121 | $log->trace( 'Redirecting to post #' . ( $offset + 1 ) . '...' ); |
||
| 122 | $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit . '&wl-async=' . ( $asynchronous ? 'true' : 'false' ) ) ); |
||
| 123 | } |
||
| 124 | |||
| 125 | $this->log->info( "Rebuild complete [ count :: $count ][ limit :: $limit ]" ); |
||
| 126 | echo( "Rebuild complete [ count :: $count ][ limit :: $limit ]" ); |
||
| 127 | |||
| 128 | // If we're being called as AJAX, die here. |
||
| 129 | if ( DOING_AJAX ) { |
||
| 130 | wp_die(); |
||
| 131 | } |
||
| 132 | |||
| 133 | } |
||
| 134 | |||
| 188 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: