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

File_Field::admin_init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 9.4285
1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
use Carbon_Fields\Helper\Helper;
6
7
8
/**
9
 * File upload field class.
10
 *
11
 * Allows selecting and saving a media attachment file,
12
 * where the file ID is saved in the database.
13
 */
14
class File_Field extends Field {
15
16
	// empty for all types. available types: audio, video, image and all WordPress-recognized mime types
17
	public $field_type = '';
18
19
	// alt, author, caption, dateFormatted, description, editLink, filename, height, icon, id, link, menuOrder, mime, name, status, subtype, title, type, uploadedTo, url, width
20
	public $value_type = 'id';
21
22
	/**
23
	 * Change the type of the field
24
	 *
25
	 * @param string $type
26
	 */
27
	public function set_type( $type ) {
28
		$this->field_type = $type;
29
		return $this;
30
	}
31
32
	/**
33
	 * Change the value type of the field.
34
	 *
35
	 * @param string $value_type
36
	 */
37
	public function set_value_type( $value_type ) {
38
		$this->value_type = $value_type;
39
		return $this;
40
	}
41
42
	/**
43
	 * Returns an array that holds the field data, suitable for JSON representation.
44
	 *
45
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
46
	 * @return array
47
	 */
48
	public function to_json( $load ) {
49
		$field_data = parent::to_json( $load );
50
51
		$value = $this->get_value();
52
53
		$attachment_metadata = array();
54
		if ( $value ) {
55
			$attachment_metadata = Helper::get_attachment_metadata( $value, $this->value_type );
0 ignored issues
show
Bug introduced by
It seems like $value defined by $this->get_value() on line 51 can also be of type array; however, Carbon_Fields\Helper\Hel...t_attachment_metadata() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
56
		}
57
58
		$field_data = array_merge( $field_data, $attachment_metadata, array(
59
			'type_filter' => $this->field_type,
60
			'value_type'  => $this->value_type,
61
		) );
62
63
		return $field_data;
64
	}
65
}
66