| Conditions | 3 |
| Paths | 3 |
| Total Lines | 65 |
| Code Lines | 33 |
| Lines | 10 |
| Ratio | 15.38 % |
| 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 preload_uris( $uris ) { |
||
| 73 | |||
| 74 | // Bail out if there are no URIs. |
||
| 75 | if ( 0 === count( $uris ) ) { |
||
| 76 | return; |
||
| 77 | } |
||
| 78 | |||
| 79 | $this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' ); |
||
| 80 | |||
| 81 | $that = $this; |
||
| 82 | $external_uris = array_filter( $uris, function ( $item ) use ( $that ) { |
||
| 83 | return ! $that->is_internal( $item ); |
||
| 84 | } ); |
||
| 85 | |||
| 86 | $query_args = array( |
||
| 87 | // See https://github.com/insideout10/wordlift-plugin/issues/654. |
||
| 88 | 'ignore_sticky_posts' => 1, |
||
| 89 | 'cache_results' => false, |
||
| 90 | 'numberposts' => - 1, |
||
| 91 | 'post_status' => 'any', |
||
| 92 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
||
| 93 | 'meta_query' => array( |
||
| 94 | array( |
||
| 95 | 'key' => WL_ENTITY_URL_META_NAME, |
||
| 96 | 'value' => $uris, |
||
| 97 | 'compare' => 'IN', |
||
| 98 | ), |
||
| 99 | ), |
||
| 100 | ); |
||
| 101 | |||
| 102 | // Only if the current uri is not an internal uri, entity search is |
||
| 103 | // performed also looking at sameAs values. |
||
| 104 | // |
||
| 105 | // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
||
| 106 | View Code Duplication | if ( 0 < count( $external_uris ) ) { |
|
|
|
|||
| 107 | |||
| 108 | $query_args['meta_query']['relation'] = 'OR'; |
||
| 109 | $query_args['meta_query'][] = array( |
||
| 110 | 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
||
| 111 | 'value' => $external_uris, |
||
| 112 | 'compare' => 'IN', |
||
| 113 | ); |
||
| 114 | |||
| 115 | } |
||
| 116 | |||
| 117 | // Get the posts. |
||
| 118 | $posts = get_posts( $query_args ); |
||
| 119 | |||
| 120 | // Populate the array. We reinitialize the array on purpose because |
||
| 121 | // we don't want these data to long live. |
||
| 122 | $this->uri_to_post = array_reduce( $posts, function ( $carry, $item ) use ( $that ) { |
||
| 123 | $uris = get_post_meta( $item->ID, WL_ENTITY_URL_META_NAME ) |
||
| 124 | + get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_SAME_AS ); |
||
| 125 | |||
| 126 | return $carry |
||
| 127 | // Get the URI related to the post and fill them with the item id. |
||
| 128 | + array_fill_keys( $uris, $item ); |
||
| 129 | }, array() ); |
||
| 130 | |||
| 131 | // Add the not found URIs. |
||
| 132 | $this->uri_to_post += array_fill_keys( $uris, null ); |
||
| 133 | |||
| 134 | $this->log->debug( count( $this->uri_to_post ) . " URI(s) preloaded." ); |
||
| 135 | |||
| 136 | } |
||
| 137 | |||
| 225 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.