Completed
Push — milestone/2_0/react-ui ( d404a3...73b2ee )
by
unknown
21:44
created

Factory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 40
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
A make() 0 21 4
1
<?php
2
3
namespace Carbon_Fields\Container\Condition;
4
5
use Carbon_Fields\App;
6
use Carbon_Fields\Helper\Helper;
7
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
8
9
class Factory {
10
11
	/**
12
	 * Get the type for the specified class
13
	 * 
14
	 * @param  string $class
15
	 * @return string
16
	 */
17
	public function get_type( $class ) {
18
		return Helper::class_to_type( $class, '_Condition' );
19
	}
20
21
	/**
22
	 * Get an instance of the specified type
23
	 * 
24
	 * @param  string $type
25
	 * @return mixed
26
	 */
27
	public function make( $type ) {
28
		$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...
29
		$normalized_type = Helper::normalize_type( $type );
30
		
31
		$identifier = 'container_condition_type_' . $normalized_type;
32
		if ( App::has( $identifier ) ) {
33
			return App::resolve( $identifier );
34
		}
35
36
		if ( class_exists( $type ) ) {
37
			$reflection = new \ReflectionClass( $type );
38
			if ( $reflection->isSubclassOf( $condition_type_superclass ) ) {
39
				return new $type();
40
			} else {
41
				Incorrect_Syntax_Exception::raise( 'Field must be of type Carbon_Fields\\Field\\Field' );
42
			}
43
		}
44
45
		Incorrect_Syntax_Exception::raise( 'Unknown condition type "' . $type . '".' );
46
		return null;
47
	}
48
}