| Conditions | 16 |
| Paths | 28 |
| Total Lines | 70 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 125 | public static function get_instance( $term_id, $taxonomy = null ) { |
||
| 126 | global $wpdb; |
||
| 127 | |||
| 128 | $term_id = (int) $term_id; |
||
| 129 | if ( ! $term_id ) { |
||
| 130 | return false; |
||
| 131 | } |
||
| 132 | |||
| 133 | $_term = wp_cache_get( $term_id, 'terms' ); |
||
| 134 | |||
| 135 | // If there isn't a cached version, hit the database. |
||
| 136 | if ( ! $_term || ( $taxonomy && $taxonomy !== $_term->taxonomy ) ) { |
||
|
|
|||
| 137 | // Grab all matching terms, in case any are shared between taxonomies. |
||
| 138 | $terms = $wpdb->get_results( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id = %d", $term_id ) ); |
||
| 139 | if ( ! $terms ) { |
||
| 140 | return false; |
||
| 141 | } |
||
| 142 | |||
| 143 | // If a taxonomy was specified, find a match. |
||
| 144 | if ( $taxonomy ) { |
||
| 145 | foreach ( $terms as $match ) { |
||
| 146 | if ( $taxonomy === $match->taxonomy ) { |
||
| 147 | $_term = $match; |
||
| 148 | break; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | // If only one match was found, it's the one we want. |
||
| 153 | } elseif ( 1 === count( $terms ) ) { |
||
| 154 | $_term = reset( $terms ); |
||
| 155 | |||
| 156 | // Otherwise, the term must be shared between taxonomies. |
||
| 157 | } else { |
||
| 158 | // If the term is shared only with invalid taxonomies, return the one valid term. |
||
| 159 | foreach ( $terms as $t ) { |
||
| 160 | if ( ! taxonomy_exists( $t->taxonomy ) ) { |
||
| 161 | continue; |
||
| 162 | } |
||
| 163 | |||
| 164 | // Only hit if we've already identified a term in a valid taxonomy. |
||
| 165 | if ( $_term ) { |
||
| 166 | return new WP_Error( 'ambiguous_term_id', __( 'Term ID is shared between multiple taxonomies' ), $term_id ); |
||
| 167 | } |
||
| 168 | |||
| 169 | $_term = $t; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | if ( ! $_term ) { |
||
| 174 | return false; |
||
| 175 | } |
||
| 176 | |||
| 177 | // Don't return terms from invalid taxonomies. |
||
| 178 | if ( ! taxonomy_exists( $_term->taxonomy ) ) { |
||
| 179 | return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
||
| 180 | } |
||
| 181 | |||
| 182 | $_term = sanitize_term( $_term, $_term->taxonomy, 'raw' ); |
||
| 183 | |||
| 184 | // Don't cache terms that are shared between taxonomies. |
||
| 185 | if ( 1 === count( $terms ) ) { |
||
| 186 | wp_cache_add( $term_id, $_term, 'terms' ); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | $term_obj = new WP_Term( $_term ); |
||
| 191 | $term_obj->filter( $term_obj->filter ); |
||
| 192 | |||
| 193 | return $term_obj; |
||
| 194 | } |
||
| 195 | |||
| 256 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: