Completed
Pull Request — master (#193)
by
unknown
02:22
created

Association_Value_Parser::parse()   C

Complexity

Conditions 10
Paths 2

Size

Total Lines 50
Code Lines 29

Duplication

Lines 16
Ratio 32 %

Importance

Changes 0
Metric Value
cc 10
eloc 29
nc 2
nop 2
dl 16
loc 50
rs 5.7647
c 0
b 0
f 0

How to fix   Complexity   

Long Method

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:

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
}