Completed
Push — milestone/2_0/container-condit... ( af96ef...c41f81 )
by
unknown
03:01
created

Current_User_Role_Condition   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 41.18 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 14
loc 34
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B is_fulfilled() 14 25 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 the currently logged in user has a specific role
7
 * 
8
 * Operator "CUSTOM" is passed an array of all user roles
9
 */
10
class Current_User_Role_Condition extends Condition {
11
	
12
	/**
13
	 * Check if the condition is fulfilled
14
	 * 
15
	 * @param  array $environment
16
	 * @return bool
17
	 */
18
	public function is_fulfilled( $environment ) {
19
		$user = wp_get_current_user();
20
		$roles = $user ? $user->roles : array();
21
		
22 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...
23
			case '=':
24
				return in_array( $this->get_value(), $roles );
25
				break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
26
			case '!=':
27
				return ! in_array( $this->get_value(), $roles );
28
				break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
29
			case 'IN':
30
				return count( array_intersect( $roles, $this->get_value() ) ) > 0;
31
				break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
32
			case 'NOT IN':
33
				return count( array_intersect( $roles, $this->get_value() ) ) === 0;
34
				break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
35
		}
36
37
		return $this->first_supported_comparer_is_correct(
38
			$roles,
39
			$this->get_comparison_operator(),
40
			$this->get_value()
41
		);
42
	}
43
}