Completed
Pull Request — master (#193)
by
unknown
09:54
created

Association_Value_Parser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 26.67 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 16
loc 60
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
C parse() 16 50 10

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
namespace Carbon_Fields\Updater;
3
4
/**
5
* Class for parsing association input
6
*/
7
class Association_Value_Parser extends Value_Parser {
8
	
9
	/**
10
	 * Prepare $input for association field
11
	 * 
12
	 * @param  array $input 
13
	 * @param  bool $is_option
14
	 * @return array $parsed_data
15
	 */
16
	public static function parse( $input, $is_option ) {
17
		if ( empty( $input ) ) {
18
			return null;
19
		} 
20
21
		$parsed_data = array_map( function( $item ) {
22
23
			if ( ! self::is_key_present( $item['id'], $item ) ) {
24
				self::throw_exception( __( 'Please make sure you have provided ids', 'crb' ) );
25
			}
26
27
			if ( ! self::is_key_present( $item['type'], $item ) ) {
28
				self::throw_exception( __( 'Please make sure you have provided types', 'crb' ) );
29
			}
30
31
			switch ( $item['type'] ) {
32
				case 'user':
33
					return 'user:user:' . $item['id'];
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
				case 'comment':
37
					return 'comment:comment:' . $item['id'];
38
					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...
39
40 View Code Duplication
				case 'post':
0 ignored issues
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...
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
41
					
42
					if ( ! self::is_key_present( 'post_type', $item ) ) {
43
						self::throw_exception( __( 'Please provide post_type', 'crb' ) );
44
					}
45
					
46
					return 'post:' . $item['post_type'] . ':' . $item['id'];
47
					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...
48
49 View Code Duplication
				case 'term':
0 ignored issues
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...
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
50
					
51
					if ( ! self::is_key_present( 'taxonomy', $item ) ) {
52
						self::throw_exception( __( 'Please provide taxonomy', 'crb' ) );
53
					}
54
55
					return 'term:' . $item['taxonomy'] . ':' . $item['id'];
56
					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...
57
58
				default:
59
					self::throw_exception( __( 'Unknown type used!', 'crb' ) );
60
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
61
62
		}, $input );
63
		
64
		return $parsed_data;
65
	}
66
}