Completed
Push — milestone/2_0/react-ui ( 1c376a...57d10c )
by
unknown
02:51
created

Factory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get_type() 0 3 1
B make() 0 22 4
1
<?php
2
3
namespace Carbon_Fields\Container\Condition;
4
5
use Carbon_Fields\Helper\Helper;
6
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
7
8
class Factory {
9
10
	/**
11
	 * Get the type for the specified class
12
	 * 
13
	 * @param  string $class
14
	 * @return string
15
	 */
16
	public function get_type( $class ) {
17
		return Helper::class_to_type( $class, '_Condition' );
18
	}
19
20
	/**
21
	 * Get an instance of the specified type
22
	 * 
23
	 * @param  string $type
24
	 * @return mixed
25
	 */
26
	public function make( $type ) {
27
		$condition_type_superclass = 'Carbon_Fields\\Container\\Condition\\Condition';
1 ignored issue
show
Comprehensibility Naming introduced by
The variable name $condition_type_superclass exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
28
		$normalized_type = Helper::normalize_type( $type );
29
		
30
		$identifier = 'container_condition_type_' . $normalized_type;
31
		if ( \Carbon_Fields\Carbon_Fields::has( $identifier ) ) {
32
			return \Carbon_Fields\Carbon_Fields::resolve( $identifier );
33
		}
34
35
		if ( class_exists( $type ) ) {
36
			$reflection = new \ReflectionClass( $type );
37
			if ( $reflection->isSubclassOf( $condition_type_superclass ) ) {
38
				return new $type();
39
			} else {
40
				Incorrect_Syntax_Exception::raise( 'Condition must be of type ' . $condition_type_superclass );
41
				return null;
42
			}
43
		}
44
45
		Incorrect_Syntax_Exception::raise( 'Unknown condition type "' . $type . '".' );
46
		return null;
47
	}
48
}