Completed
Push — master ( c4606f...2cda3d )
by
unknown
15:54 queued 06:31
created

Term_Ancestor_Condition   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 35.48 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 11
loc 31
rs 10
wmc 6
lcom 1
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B is_fulfilled() 11 28 6

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 Carbon_Fields\Container\Condition;
4
5
/**
6
 * Check if term has a specific ancestor
7
 *
8
 * Accepts the following values:
9
 *     Operators "=" and "!=":
10
 *         array(
11
 *             'value'=>...,
12
 *             'taxonomy'=>...,
13
 *             ['field'=>...] // "slug", "term_id" etc. - see get_term_by()
14
 *         )
15
 *
16
 *     Operators "IN" and "NOT IN":
17
 *         array(
18
 *             array(
19
 *                 'value'=>...,
20
 *                 'taxonomy'=>...,
21
 *                 ['field'=>...]
22
 *             ),
23
 *             ...
24
 *         )
25
 *
26
 *     Operator "CUSTOM" is passed an array of ancestor term ids
27
 */
28
class Term_Ancestor_Condition extends Term_Condition {
29
30
	public function is_fulfilled( $environment ) {
31
		$term_id = $environment['term_id'];
32
		$term = $environment['term'];
33
		$ancestors = array();
34
35
		if ( $term ) {
36
			$ancestors = array_map( 'intval', get_ancestors( $term_id, $term->taxonomy ) );
37
		}
38
39
		$value = $this->get_value();
40 View Code Duplication
		switch ( $this->get_comparison_operator() ) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
41
			case '=': // fallthrough intended
42
			case '!=':
43
				$value = $this->get_term_id_from_full_term_descriptor( $value );
44
				break;
45
46
			case 'IN': // fallthrough intended
47
			case 'NOT IN':
48
				$value = $this->get_term_ids_from_full_term_descriptors( $value );
49
				break;
50
		}
51
52
		return $this->compare(
53
			$ancestors,
54
			$this->get_comparison_operator(),
55
			$value
56
		);
57
	}
58
}