Completed
Push — master ( 3ac4b1...21157a )
by Zack
05:18
created

GravityView_Fields::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @file class-gravityview-fields.php
4
 * @package GravityView
5
 * @subpackage includes\fields
6
 */
7
8
/**
9
 * Wanted to extend GF_Fields, but couldn't because static variables are inherited,
10
 * so $_fields would always be GF results
11
 *
12
 * @see GF_Fields
13
 */
14
final class GravityView_Fields {
15
16
	/* @var GravityView_Field[] */
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
17
	protected static $_fields = array();
18
19
	/**
20
	 * @param GravityView_Field $field Field to register
21
	 *
22
	 * @throws Exception If requirements aren't met
23
	 *
24
	 * @return void
25
	 */
26
	public static function register( $field ) {
27
		if ( ! is_subclass_of( $field, 'GravityView_Field' ) ) {
28
			throw new Exception( 'Must be a subclass of GravityView_Field' );
29
		}
30
		if ( empty( $field->name ) ) {
31
			throw new Exception( 'The name must be set' );
32
		}
33
		if ( isset( self::$_fields[ $field->name ] ) && ! defined( 'DOING_GRAVITYVIEW_TESTS' ) ) {
34
			throw new Exception( 'Field type already registered: ' . $field->name );
35
		}
36
		self::$_fields[ $field->name ] = $field;
37
	}
38
39
	/**
40
	 * @param array $properties
41
	 *
42
	 * @return GravityView_Field | bool
43
	 */
44
	public static function create( $properties ) {
45
		$type = isset( $properties['type'] ) ? $properties['type'] : '';
46
		$type = empty( $properties['inputType'] ) ? $type : $properties['inputType'];
47
		if ( empty( $type ) || ! isset( self::$_fields[ $type ] ) ) {
48
			return new GravityView_Field( $properties );
0 ignored issues
show
Unused Code introduced by
The call to GravityView_Field::__construct() has too many arguments starting with $properties.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
49
		}
50
		$class      = self::$_fields[ $type ];
51
		$class_name = get_class( $class );
52
		$field      = new $class_name( $properties );
53
54
		return $field;
55
	}
56
57
	/**
58
	 * Does the field exist (has it been registered)?
59
	 *
60
	 * @param string $field_name
61
	 *
62
	 * @return bool True: yes, it exists; False: nope
63
	 */
64
	public static function exists( $field_name ) {
65
		return isset( self::$_fields["{$field_name}"] );
66
	}
67
68
	/**
69
	 * @param string $field_name
70
	 *
71
	 * @return GravityView_Field
72
	 */
73
	public static function get_instance( $field_name ) {
74
		return isset( self::$_fields[ $field_name ] ) ? self::$_fields[ $field_name ] : false;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression isset(self::$_fields[$fi...s[$field_name] : false; of type GravityView_Field|false adds false to the return on line 74 which is incompatible with the return type documented by GravityView_Fields::get_instance of type GravityView_Field. It seems like you forgot to handle an error condition.
Loading history...
75
	}
76
77
	/**
78
	 * Alias for get_instance()
79
	 *
80
	 * @param $field_name
81
	 *
82
	 * @return GravityView_Field
83
	 */
84
	public static function get( $field_name ) {
85
		return self::get_instance( $field_name );
86
	}
87
88
	/**
89
	 * Get all fields
90
	 *
91
	 * @since 1.16 Added $group parameter
92
	 *
93
	 * @param string $group Optional. If defined, fetch all fields in a group
94
	 *
95
	 * @return GravityView_Field[]
96
	 */
97
	public static function get_all( $group = '' ) {
98
99
		if( '' !== $group ) {
100
			$return_fields = self::$_fields;
101
			foreach ( $return_fields as $key => $field ) {
102
				if( $group !== $field->group ) {
103
					unset( $return_fields[ $key ] );
104
				}
105
			}
106
			return $return_fields;
107
		} else {
108
			return self::$_fields;
109
		}
110
	}
111
112
}