| Conditions | 4 |
| Paths | 5 |
| Total Lines | 54 |
| Code Lines | 16 |
| 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 |
||
| 115 | public function get_jsonld( $is_homepage = false, $post_id = null ) { |
||
| 116 | |||
| 117 | // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
||
| 118 | // e-mails with a low apdex score. |
||
| 119 | // |
||
| 120 | // See https://github.com/insideout10/wordlift-plugin/issues/521 |
||
| 121 | Wordlift_NewRelic_Adapter::ignore_apdex(); |
||
| 122 | |||
| 123 | // Switch to Website converter if is home page. |
||
| 124 | if ( $is_homepage ) { |
||
| 125 | /** |
||
| 126 | * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
||
| 127 | * |
||
| 128 | * @since 3.14.0 |
||
| 129 | * @api bool $display_search Whether or not to display json+ld search on the frontend. |
||
| 130 | */ |
||
| 131 | if ( ! apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
||
| 132 | // Set a reference to the website_converter. |
||
| 133 | $website_converter = $this->website_converter; |
||
| 134 | |||
| 135 | // Send JSON-LD. |
||
| 136 | return $website_converter->create_schema(); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | // If no id has been provided return an empty array. |
||
| 141 | if ( ! isset( $post_id ) ) { |
||
| 142 | return array(); |
||
| 143 | } |
||
| 144 | |||
| 145 | // An array of references which is captured when converting an URI to a |
||
| 146 | // json which we gather to further expand our json-ld. |
||
| 147 | $references = array(); |
||
| 148 | |||
| 149 | // Set a reference to the entity_to_jsonld_converter to use in the closures. |
||
| 150 | $entity_to_jsonld_converter = $this->converter; |
||
| 151 | |||
| 152 | // Convert each URI to a JSON-LD array, while gathering referenced entities. |
||
| 153 | // in the references array. |
||
| 154 | $jsonld = array_merge( |
||
| 155 | array( $entity_to_jsonld_converter->convert( $post_id, $references ) ), |
||
| 156 | // Convert each URI in the references array to JSON-LD. We don't output |
||
| 157 | // entities already output above (hence the array_diff). |
||
| 158 | array_map( function ( $item ) use ( $entity_to_jsonld_converter, $references ) { |
||
| 159 | |||
| 160 | // "2nd level properties" may not output here, e.g. a post |
||
| 161 | // mentioning an event, located in a place: the place is referenced |
||
| 162 | // via the `@id` but no other properties are loaded. |
||
| 163 | return $entity_to_jsonld_converter->convert( $item, $references ); |
||
| 164 | }, $references ) ); |
||
| 165 | |||
| 166 | // Finally send the JSON-LD. |
||
| 167 | return $jsonld; |
||
| 168 | } |
||
| 169 | |||
| 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: