Conditions | 11 |
Paths | 192 |
Total Lines | 62 |
Lines | 14 |
Ratio | 22.58 % |
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 |
||
124 | public function get( $args = array(), $single = false ) { |
||
125 | $array_terms = array(); |
||
126 | |||
127 | $default_args = array( |
||
128 | 'hide_empty' => false, |
||
129 | 'taxonomy' => $this->get_type(), |
||
130 | ); |
||
131 | |||
132 | // Si le paramètre "id" est passé on le transforme en "include" qui est la paramètre attendu par WP_Term_Query. |
||
133 | // Dans un soucis d'homogénéité du code, le paramètre "id" remplace le paramètre "include" dans les appels de la fonction. |
||
134 | $args['id'] = ! empty( $args['term_id'] ) ? $args['term_id'] : ( isset( $args['id'] ) ? $args['id'] : null ); |
||
135 | if ( ! empty( $args['id'] ) ) { |
||
136 | if ( isset( $args['term_id'] ) ) { |
||
137 | unset( $args['term_id'] ); |
||
138 | } |
||
139 | if ( ! isset( $args['include'] ) ) { |
||
140 | $args['include'] = array(); |
||
141 | } |
||
142 | $args['include'] = array_merge( $args['include'], (array) $args['id'] ); |
||
143 | } elseif ( isset( $args['id'] ) ) { |
||
144 | $args['schema'] = true; |
||
145 | } |
||
146 | unset( $args['id'] ); |
||
147 | |||
148 | // @Todo: a voir pourquoi wp_get_post_terms et pas wp_get_object_terms et si pas d'autre moyen que ici. |
||
149 | // elseif ( isset( $args['post_id'] ) ) { |
||
150 | // $array_term = wp_get_post_terms( $args['post_id'], $this->get_type(), $term_final_args ); |
||
151 | // |
||
152 | // if ( empty( $array_term ) ) { |
||
153 | // $array_term[] = array(); |
||
154 | // } |
||
155 | // } |
||
156 | |||
157 | $args_cb = array( |
||
158 | 'args' => $args, |
||
159 | 'default_args' => $default_args, |
||
160 | ); |
||
161 | $final_args = apply_filters( 'eo_model_term_before_get', wp_parse_args( $args, $default_args ), $args_cb ); |
||
162 | // Il ne faut pas lancer plusieurs fois pour term. |
||
163 | if ( 'term' !== $this->get_type() ) { |
||
164 | $final_args = apply_filters( 'eo_model_' . $this->get_type() . '_before_get', $final_args, $args_cb ); |
||
165 | } |
||
166 | |||
167 | // Si l'argument "schema" est présent c'est lui qui prend le dessus et ne va pas récupérer d'élément dans la base de données. |
||
168 | if ( isset( $args['schema'] ) ) { |
||
169 | $array_terms[] = $final_args; |
||
170 | } else { // On lance la requête pour récupèrer les "terms" demandés. |
||
171 | $query_terms = new \WP_Term_Query( $final_args ); |
||
172 | $array_terms = $query_terms->terms; |
||
173 | unset( $query_terms->terms ); |
||
174 | } |
||
175 | |||
176 | // Traitement de la liste des résultats pour le retour. |
||
177 | $array_terms = $this->prepare_items_for_response( $array_terms, 'term', $this->meta_key, 'term_id' ); |
||
178 | |||
179 | // Si on a demandé qu'une seule entrée et qu'il n'y a bien qu'une seule entrée correspondant à la demande alors on ne retourne que cette entrée. |
||
180 | if ( true === $single && 1 === count( $array_terms ) ) { |
||
181 | $array_terms = $array_terms[0]; |
||
182 | } |
||
183 | |||
184 | return $array_terms; |
||
185 | } |
||
186 | |||
247 |