Completed
Push — development ( 261752...44c3a4 )
by
unknown
02:39
created

Media_Gallery_Field   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 133
Duplicated Lines 9.02 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 7
Bugs 1 Features 0
Metric Value
dl 12
loc 133
rs 10
c 7
b 1
f 0
wmc 10
lcom 2
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A set_type() 0 4 1
A get_duplicates_allowed() 0 3 1
A set_duplicates_allowed() 0 4 1
A set_value_from_input() 12 12 3
A value_to_json() 0 16 2
A to_json() 0 11 1

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\Field;
4
5
use Carbon_Fields\Value_Set\Value_Set;
6
use Carbon_Fields\Helper\Helper;
7
8
/**
9
 * Set field class.
10
 * Allows to create a set of checkboxes where multiple can be selected.
11
 */
12
class Media_Gallery_Field extends Field {
13
14
	/**
15
	 * File type filter. Leave a blank string for any file type.
16
	 * Available types: audio, video, image and all WordPress-recognized mime types
17
	 *
18
	 * @var string|array
19
	 */
20
	protected $file_type = '';
21
22
	/**
23
	 * What value to store
24
	 *
25
	 * @var string
26
	 */
27
	protected $value_type = 'id';
28
29
	/**
30
	 * Default field value
31
	 *
32
	 * @var array
33
	 */
34
	protected $default_value = array();
35
36
	/**
37
	 * Allow items to be added multiple times
38
	 *
39
	 * @var boolean
40
	 */
41
	protected $duplicates_allowed = true;
42
43
	/**
44
	 * Create a field from a certain type with the specified label.
45
	 *
46
	 * @param string $type  Field type
47
	 * @param string $name  Field name
48
	 * @param string $label Field label
49
	 */
50
	public function __construct( $type, $name, $label ) {
51
		$this->set_value_set( new Value_Set( Value_Set::TYPE_MULTIPLE_VALUES ) );
52
		parent::__construct( $type, $name, $label );
53
	}
54
55
	/**
56
	 * Change the type of the field
57
	 *
58
	 * @param string $type
59
	 */
60
	public function set_type( $type ) {
61
		$this->file_type = $type;
62
		return $this;
63
	}
64
65
	/**
66
	 * Get whether entry duplicates are allowed.
67
	 *
68
	 * @return boolean
69
	 */
70
	public function get_duplicates_allowed() {
71
		return $this->duplicates_allowed;
72
	}
73
74
	/**
75
	 * Set whether entry duplicates are allowed.
76
	 *
77
	 * @param  boolean $allowed
78
	 * @return Field   $this
79
	 */
80
	public function set_duplicates_allowed( $allowed ) {
81
		$this->duplicates_allowed = $allowed;
82
		return $this;
83
	}
84
85
	/**
86
	 * Load the field value from an input array based on it's name
87
	 *
88
	 * @param  array $input Array of field names and values.
89
	 * @return Field $this
90
	 */
91 View Code Duplication
	public function set_value_from_input( $input ) {
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...
92
		if ( ! isset( $input[ $this->name ] ) ) {
93
			$this->set_value( array() );
94
		} else {
95
			$value = stripslashes_deep( $input[ $this->name ] );
96
			if ( is_array( $value ) ) {
97
				$value = array_values( $value );
98
			}
99
			$this->set_value( $value );
100
		}
101
		return $this;
102
	}
103
104
	/**
105
	 * Converts the field values into a usable associative array.
106
	 *
107
	 * @access protected
108
	 * @return array
109
	 */
110
	protected function value_to_json() {
111
		$value_set  = $this->get_value();
112
		$value_meta = array();
113
114
		foreach ( $value_set as $attachment_id ) {
0 ignored issues
show
Bug introduced by
The expression $value_set of type string|array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
115
			$attachment_id     = absint( $attachment_id );
116
			$attachment_metata = Helper::get_attachment_metadata( $attachment_id, $this->value_type );
117
118
			$value_meta[ $attachment_id ] = $attachment_metata;
119
		}
120
121
		return array(
122
			'value'      => $value_set,
123
			'value_meta' => $value_meta,
124
		);
125
	}
126
127
	/**
128
	 * Returns an array that holds the field data, suitable for JSON representation.
129
	 *
130
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
131
	 * @return array
132
	 */
133
	public function to_json( $load ) {
134
		$field_data = parent::to_json( $load );
135
136
		$field_data = array_merge( $field_data, $this->value_to_json(), array(
137
			'value_type'          => $this->value_type,
138
			'type_filter'         => $this->file_type,
139
			'duplicates_allowed'  => $this->get_duplicates_allowed(),
140
		) );
141
142
		return $field_data;
143
	}
144
}
145