Completed
Pull Request — develop (#1328)
by Naveen
03:20
created

Default_Term_Count   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 21.95 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 9
loc 41
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get_term_count() 0 22 1
A get_terms_compat() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Wordlift\Vocabulary\Data\Term_Count;
4
5
use Wordlift\Vocabulary\Analysis_Background_Service;
6
use Wordlift\Vocabulary\Api\Entity_Rest_Endpoint;
7
8
/**
9
 * This class is used for getting default term count without cache.
10
 * @since 3.30.0
11
 * @author Naveen Muthusamy <[email protected]>
12
 */
13
class Default_Term_Count implements Term_Count {
14
15
	/**
16
	 * Return count of terms which have entities and also not matched by editor, currently
17
	 * it is used for the menu icon badge.
18
	 * @return int
19
	 */
20
	public function get_term_count() {
21
		/**
22
		 * @todo: add support for all terms, currently we add only
23
		 * post_tag.
24
		 */
25
		return count( $this->get_terms_compat( 'post_tag', array(
26
			'taxonomy'   => 'post_tag',
27
			'hide_empty' => false,
28
			'fields'     => 'ids',
29
			'meta_query' => array(
30
				array(
31
					'key'     => Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING,
32
					'compare' => 'NOT EXISTS',
33
				),
34
				array(
35
					'key'     => Analysis_Background_Service::ENTITIES_PRESENT_FOR_TERM,
36
					'compare' => 'EXISTS'
37
				)
38
			),
39
		) ) );
40
41
	}
42
43 View Code Duplication
	private function get_terms_compat( $taxonomy, $args_with_taxonomy_key ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
44
		global $wp_version;
45
46
		if ( version_compare( $wp_version, '4.5', '<' ) ) {
47
			return get_terms( $taxonomy, $args_with_taxonomy_key );
48
		} else {
49
			return get_terms( $args_with_taxonomy_key );
50
		}
51
	}
52
53
}
54