| Conditions | 9 |
| Paths | 64 |
| Total Lines | 60 |
| Code Lines | 26 |
| 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 |
||
| 60 | public function rebuild() { |
||
|
|
|||
| 61 | |||
| 62 | ob_clean(); |
||
| 63 | |||
| 64 | // Give ourselves some time to process the data. |
||
| 65 | set_time_limit( 21600 ); // 6 hours |
||
| 66 | |||
| 67 | // Send textual output. |
||
| 68 | header( 'Content-type: text/plain; charset=utf-8' ); |
||
| 69 | |||
| 70 | // We start at 0 by default and get to max. |
||
| 71 | $offset = $_GET['offset'] ?: 0; |
||
| 72 | $limit = $_GET['limit'] ?: 1; |
||
| 73 | $entity_only = isset( $_GET['entity_only'] ) && '1' === $_GET['entity_only']; |
||
| 74 | $max = $offset + $limit; |
||
| 75 | |||
| 76 | // If we're starting at offset 0, then delete existing URIs and data from |
||
| 77 | // the remote dataset. |
||
| 78 | if ( 0 === $offset ) { |
||
| 79 | |||
| 80 | // Clear out all generated URIs, since the dataset URI might have changed |
||
| 81 | // in the process. |
||
| 82 | $this->uri_service->delete_all(); |
||
| 83 | |||
| 84 | // Delete all the triples in the remote dataset. |
||
| 85 | $this->sparql_service->queue( 'DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };' ); |
||
| 86 | |||
| 87 | } |
||
| 88 | |||
| 89 | // Go through the list of published entities and posts and call the (legacy) |
||
| 90 | // `wl_linked_data_save_post` function for each one. We're using the `process` |
||
| 91 | // function which is provided by the parent `Wordlift_Listable` abstract class |
||
| 92 | // and will cycle through all the posts w/ a very small memory footprint |
||
| 93 | // in order to avoid memory errors. |
||
| 94 | |||
| 95 | $count = 0; |
||
| 96 | $this->process( function ( $post ) use ( &$count ) { |
||
| 97 | $count ++; |
||
| 98 | wl_linked_data_save_post( $post->ID ); |
||
| 99 | }, array( |
||
| 100 | 'post_status' => 'publish', |
||
| 101 | 'post_type' => $entity_only ? 'entity' : array( |
||
| 102 | 'entity', |
||
| 103 | 'post', |
||
| 104 | ), |
||
| 105 | ), $offset, $max ); |
||
| 106 | |||
| 107 | // Redirect to the next chunk. |
||
| 108 | if ( $count == $limit ) { |
||
| 109 | $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit . '&entity_only=' . ( $entity_only ? '1' : '0' ) ) ); |
||
| 110 | } |
||
| 111 | |||
| 112 | echo( "done [ count :: $count ][ limit :: $limit ]" ); |
||
| 113 | |||
| 114 | // If we're being called as AJAX, die here. |
||
| 115 | if ( DOING_AJAX ) { |
||
| 116 | wp_die(); |
||
| 117 | } |
||
| 118 | |||
| 119 | } |
||
| 120 | |||
| 171 |
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: