| Conditions | 4 |
| Paths | 6 |
| Total Lines | 64 |
| Code Lines | 18 |
| 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 |
||
| 66 | public function get() { |
||
|
|
|||
| 67 | |||
| 68 | // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
||
| 69 | // e-mails with a low apdex score. |
||
| 70 | // |
||
| 71 | // See https://github.com/insideout10/wordlift-plugin/issues/521 |
||
| 72 | Wordlift_NewRelic_Adapter::ignore_apdex(); |
||
| 73 | |||
| 74 | // Clear the buffer to be sure someone doesn't mess with our response. |
||
| 75 | // |
||
| 76 | // See https://github.com/insideout10/wordlift-plugin/issues/406. |
||
| 77 | // See https://codex.wordpress.org/AJAX_in_Plugins. |
||
| 78 | ob_clean(); |
||
| 79 | |||
| 80 | // Switch to Website converter if is home page. |
||
| 81 | if ( isset( $_REQUEST['homepage'] ) ) { |
||
| 82 | /** |
||
| 83 | * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
||
| 84 | * |
||
| 85 | * @since 3.14.0 |
||
| 86 | * @api bool $display_search Whether or not to display json+ld search on the frontend. |
||
| 87 | */ |
||
| 88 | if ( ! apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
||
| 89 | // Set a reference to the website_converter. |
||
| 90 | $website_converter = $this->website_converter; |
||
| 91 | |||
| 92 | // Send JSON-LD. |
||
| 93 | wp_send_json( $website_converter->create_schema() ); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | // If no id has been provided return an empty array. |
||
| 98 | if ( ! isset( $_REQUEST['id'] ) ) { |
||
| 99 | wp_send_json( array() ); |
||
| 100 | } |
||
| 101 | |||
| 102 | // Get the id. |
||
| 103 | $id = $_REQUEST['id']; |
||
| 104 | |||
| 105 | // An array of references which is captured when converting an URI to a |
||
| 106 | // json which we gather to further expand our json-ld. |
||
| 107 | $references = array(); |
||
| 108 | |||
| 109 | // Set a reference to the entity_to_jsonld_converter to use in the closures. |
||
| 110 | $entity_to_jsonld_converter = $this->converter; |
||
| 111 | |||
| 112 | // Convert each URI to a JSON-LD array, while gathering referenced entities. |
||
| 113 | // in the references array. |
||
| 114 | $jsonld = array_merge( |
||
| 115 | array( $entity_to_jsonld_converter->convert( $id, $references ) ), |
||
| 116 | // Convert each URI in the references array to JSON-LD. We don't output |
||
| 117 | // entities already output above (hence the array_diff). |
||
| 118 | array_map( function ( $item ) use ( $entity_to_jsonld_converter, $references ) { |
||
| 119 | |||
| 120 | // "2nd level properties" may not output here, e.g. a post |
||
| 121 | // mentioning an event, located in a place: the place is referenced |
||
| 122 | // via the `@id` but no other properties are loaded. |
||
| 123 | return $entity_to_jsonld_converter->convert( $item, $references ); |
||
| 124 | }, $references ) ); |
||
| 125 | |||
| 126 | // Finally send the JSON-LD. |
||
| 127 | wp_send_json( $jsonld ); |
||
| 128 | |||
| 129 | } |
||
| 130 | |||
| 132 |
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: