| Conditions | 4 |
| Paths | 4 |
| Total Lines | 56 |
| 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 |
||
| 136 | public function get_jsonld( $is_homepage = false, $post_id = null ) { |
||
| 137 | |||
| 138 | // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
||
| 139 | // e-mails with a low apdex score. |
||
| 140 | // |
||
| 141 | // See https://github.com/insideout10/wordlift-plugin/issues/521 |
||
| 142 | Wordlift_NewRelic_Adapter::ignore_apdex(); |
||
| 143 | |||
| 144 | // Switch to Website converter if is home page. |
||
| 145 | if ( $is_homepage ) { |
||
| 146 | /** |
||
| 147 | * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
||
| 148 | * |
||
| 149 | * @since 3.14.0 |
||
| 150 | * @api bool $display_search Whether or not to display json+ld search on the frontend. |
||
| 151 | */ |
||
| 152 | if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
||
| 153 | return array(); |
||
| 154 | } |
||
| 155 | |||
| 156 | // Set a reference to the website_converter. |
||
| 157 | $website_converter = $this->website_converter; |
||
| 158 | |||
| 159 | // Send JSON-LD. |
||
| 160 | return $website_converter->create_schema(); |
||
| 161 | } |
||
| 162 | |||
| 163 | // If no id has been provided return an empty array. |
||
| 164 | if ( ! isset( $post_id ) ) { |
||
| 165 | return array(); |
||
| 166 | } |
||
| 167 | |||
| 168 | // An array of references which is captured when converting an URI to a |
||
| 169 | // json which we gather to further expand our json-ld. |
||
| 170 | $references = array(); |
||
| 171 | |||
| 172 | // Set a reference to the entity_to_jsonld_converter to use in the closures. |
||
| 173 | $entity_to_jsonld_converter = $this->converter; |
||
| 174 | |||
| 175 | // Convert each URI to a JSON-LD array, while gathering referenced entities. |
||
| 176 | // in the references array. |
||
| 177 | $jsonld = array_merge( |
||
| 178 | array( $entity_to_jsonld_converter->convert( $post_id, $references ) ), |
||
| 179 | // Convert each URI in the references array to JSON-LD. We don't output |
||
| 180 | // entities already output above (hence the array_diff). |
||
| 181 | array_filter( array_map( function ( $item ) use ( $entity_to_jsonld_converter, $references ) { |
||
| 182 | |||
| 183 | // "2nd level properties" may not output here, e.g. a post |
||
| 184 | // mentioning an event, located in a place: the place is referenced |
||
| 185 | // via the `@id` but no other properties are loaded. |
||
| 186 | return $entity_to_jsonld_converter->convert( $item, $references ); |
||
| 187 | }, $references ) ) ); |
||
| 188 | |||
| 189 | // Finally send the JSON-LD. |
||
| 190 | return $jsonld; |
||
| 191 | } |
||
| 192 | |||
| 213 |
If you suppress an error, we recommend checking for the error condition explicitly: