Completed
Push — milestone/2_0/container-condit... ( 3ee472...f04f2b )
by
unknown
02:49
created

Container_Condition_Installer   C

Complexity

Total Complexity 4

Size/Duplication

Total Lines 160
Duplicated Lines 10.63 %

Coupling/Cohesion

Components 1
Dependencies 25

Importance

Changes 0
Metric Value
dl 17
loc 160
rs 5
c 0
b 0
f 0
wmc 4
lcom 1
cbo 25

4 Methods

Rating   Name   Duplication   Size   Complexity  
A install() 0 9 1
B install_conditions() 0 99 1
B install_comparers() 17 36 1
A install_translators() 0 5 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\Installer;
4
5
use Carbon_Fields\Pimple\Container as PimpleContainer;
6
use Carbon_Fields\Container\Condition\Factory as ConditionFactory;
7
use Carbon_Fields\Container\Condition\Fulfillable\Fulfillable_Collection;
8
9
class Container_Condition_Installer implements Installer {
10
11
	/**
12
	 * Install dependencies in IoC container
13
	 * 
14
	 * @param  PimpleContainer $ioc
15
	 */
16
	public static function install( PimpleContainer $ioc ) {
17
		$ioc['container_condition_fulfillable_collection'] = $ioc->factory( function( $ioc ) {
18
			return new Fulfillable_Collection( $ioc['container_condition_factory'], $ioc['container_condition_translator_array'] );
19
		} );
20
21
		static::install_conditions( $ioc );
22
		static::install_comparers( $ioc );
1 ignored issue
show
Unused Code introduced by
The call to the method Carbon_Fields\Installer\...er::install_comparers() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
23
		static::install_translators( $ioc );
1 ignored issue
show
Unused Code introduced by
The call to the method Carbon_Fields\Installer\...::install_translators() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
24
	}
25
26
	protected static function install_conditions( $ioc ) {
27
		$ioc['container_condition_type_boolean'] = $ioc->factory( function( $ioc ) {
28
			return new \Carbon_Fields\Container\Condition\Boolean_Condition( array(
29
				$ioc['container_condition_comparer_type_equality'],
30
			) );
31
		} );
32
		$ioc['container_condition_type_post_id'] = $ioc->factory( function( $ioc ) {
0 ignored issues
show
Bug introduced by
The method factory cannot be called on $ioc (of type array<string,?,{"contain...ion_type_boolean":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
33
			return new \Carbon_Fields\Container\Condition\Post_ID_Condition( $ioc['container_condition_comparers_generic'] );
34
		} );
35
		$ioc['container_condition_type_post_parent_id'] = $ioc->factory( function( $ioc ) {
0 ignored issues
show
Bug introduced by
The method factory cannot be called on $ioc (of type array<string,?,{"contain...ion_type_post_id":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
36
			return new \Carbon_Fields\Container\Condition\Post_Parent_ID_Condition( $ioc['container_condition_comparers_generic'] );
37
		} );
38
		$ioc['container_condition_type_post_type'] = $ioc->factory( function( $ioc ) {
0 ignored issues
show
Bug introduced by
The method factory cannot be called on $ioc (of type array<string,?,{"contain...e_post_parent_id":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
39
			return new \Carbon_Fields\Container\Condition\Post_Type_Condition( $ioc['container_condition_comparers_wo_scalar'] );
40
		} );
41
		$ioc['container_condition_type_post_format'] = $ioc->factory( function( $ioc ) {
0 ignored issues
show
Bug introduced by
The method factory cannot be called on $ioc (of type array<string,?,{"contain...n_type_post_type":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
42
			return new \Carbon_Fields\Container\Condition\Post_Format_Condition( $ioc['container_condition_comparers_wo_scalar'] );
43
		} );
44
		$ioc['container_condition_type_post_level'] = $ioc->factory( function( $ioc ) {
0 ignored issues
show
Bug introduced by
The method factory cannot be called on $ioc (of type array<string,?,{"contain...type_post_format":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
45
			return new \Carbon_Fields\Container\Condition\Post_Level_Condition( $ioc['container_condition_comparers_generic'] );
46
		} );
47
		$ioc['container_condition_type_post_template'] = $ioc->factory( function( $ioc ) {
0 ignored issues
show
Bug introduced by
The method factory cannot be called on $ioc (of type array<string,?,{"contain..._type_post_level":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
48
			return new \Carbon_Fields\Container\Condition\Post_Template_Condition( $ioc['container_condition_comparers_wo_scalar'] );
49
		} );
50
		$ioc['container_condition_type_post_term'] = $ioc->factory( function( $ioc ) {
0 ignored issues
show
Bug introduced by
The method factory cannot be called on $ioc (of type array<string,?,{"contain...pe_post_template":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
51
			return new \Carbon_Fields\Container\Condition\Post_Term_Condition( array( 
52
				// Only support the custom comparer as this condition has it's own comparison methods
53
				$ioc['container_condition_comparer_type_custom'],
54
			) );
55
		} );
56
57
		$ioc['container_condition_type_term'] = $ioc->factory( function( $ioc ) {
0 ignored issues
show
Bug introduced by
The method factory cannot be called on $ioc (of type array<string,?,{"contain...n_type_post_term":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
58
			return new \Carbon_Fields\Container\Condition\Term_Condition(  $ioc['container_condition_comparers_wo_scalar']  );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 2 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 2 found
Loading history...
59
		} );
60
		$ioc['container_condition_type_term_taxonomy'] = $ioc->factory( function( $ioc ) {
0 ignored issues
show
Bug introduced by
The method factory cannot be called on $ioc (of type array<string,?,{"contain...dition_type_term":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
61
			return new \Carbon_Fields\Container\Condition\Term_Taxonomy_Condition(  $ioc['container_condition_comparers_wo_scalar']  );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 2 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 2 found
Loading history...
62
		} );
63
		$ioc['container_condition_type_term_level'] = $ioc->factory( function( $ioc ) {
0 ignored issues
show
Bug introduced by
The method factory cannot be called on $ioc (of type array<string,?,{"contain...pe_term_taxonomy":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
64
			return new \Carbon_Fields\Container\Condition\Term_Level_Condition( $ioc['container_condition_comparers_generic'] );
65
		} );
66
67
		$ioc['container_condition_type_user_id'] = $ioc->factory( function( $ioc ) {
0 ignored issues
show
Bug introduced by
The method factory cannot be called on $ioc (of type array<string,?,{"contain..._type_term_level":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
68
			return new \Carbon_Fields\Container\Condition\User_ID_Condition( $ioc['container_condition_comparers_generic'] );
69
		} );
70
		$ioc['container_condition_type_user_role'] = $ioc->factory( function( $ioc ) {
0 ignored issues
show
Bug introduced by
The method factory cannot be called on $ioc (of type array<string,?,{"contain...ion_type_user_id":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
71
			return new \Carbon_Fields\Container\Condition\User_Role_Condition( array(
72
				// Only support the custom comparer as this condition has it's own comparison methods
73
				$ioc['container_condition_comparer_type_custom'],
74
			) );
75
		} );
76
		$ioc['container_condition_type_user_capabiltiy'] = $ioc->factory( function( $ioc ) {
0 ignored issues
show
Bug introduced by
The method factory cannot be called on $ioc (of type array<string,?,{"contain...n_type_user_role":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
77
			return new \Carbon_Fields\Container\Condition\User_Capability_Condition( array(
78
				// Only support the custom comparer as this condition has it's own comparison methods
79
				$ioc['container_condition_comparer_type_custom'],
80
			) );
81
		} );
82
83
		$ioc['container_condition_type_current_user_id'] = $ioc->factory( function( $ioc ) {
0 ignored issues
show
Bug introduced by
The method factory cannot be called on $ioc (of type array<string,?,{"contain..._user_capabiltiy":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
84
			return new \Carbon_Fields\Container\Condition\Current_User_ID_Condition( $ioc['container_condition_comparers_generic'] );
85
		} );
86
		$ioc['container_condition_type_current_user_role'] = $ioc->factory( function( $ioc ) {
0 ignored issues
show
Bug introduced by
The method factory cannot be called on $ioc (of type array<string,?,{"contain..._current_user_id":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
87
			return new \Carbon_Fields\Container\Condition\Current_User_Role_Condition( array(
88
				// Only support the custom comparer as this condition has it's own comparison methods
89
				$ioc['container_condition_comparer_type_custom'],
90
			) );
91
		} );
92
		$ioc['container_condition_type_current_user_capability'] = $ioc->factory( function( $ioc ) {
0 ignored issues
show
Bug introduced by
The method factory cannot be called on $ioc (of type array<string,?,{"contain...urrent_user_role":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
93
			return new \Carbon_Fields\Container\Condition\Current_User_Capability_Condition( array(
94
				// Only support the custom comparer as this condition has it's own comparison methods
95
				$ioc['container_condition_comparer_type_custom'],
96
			) );
97
		} );
98
99
		$ioc['container_condition_factory'] = function() {
100
			$factory = new ConditionFactory();
101
			$factory->register( 'boolean', 'Carbon_Fields\\Container\\Condition\\Boolean_Condition' );
102
103
			$factory->register( 'post_id', 'Carbon_Fields\\Container\\Condition\\Post_ID_Condition' );
104
			$factory->register( 'post_parent_id', 'Carbon_Fields\\Container\\Condition\\Post_Parent_ID_Condition' );
105
			$factory->register( 'post_type', 'Carbon_Fields\\Container\\Condition\\Post_Type_Condition' );
106
			$factory->register( 'post_format', 'Carbon_Fields\\Container\\Condition\\Post_Format_Condition' );
107
			$factory->register( 'post_level', 'Carbon_Fields\\Container\\Condition\\Post_Level_Condition' );
108
			$factory->register( 'post_template', 'Carbon_Fields\\Container\\Condition\\Post_Template_Condition' );
109
			$factory->register( 'post_term', 'Carbon_Fields\\Container\\Condition\\Post_Term_Condition' );
110
111
			$factory->register( 'term', 'Carbon_Fields\\Container\\Condition\\Term_Condition' );
112
			$factory->register( 'term_taxonomy', 'Carbon_Fields\\Container\\Condition\\Term_Taxonomy_Condition' );
113
			$factory->register( 'term_level', 'Carbon_Fields\\Container\\Condition\\Term_Level_Condition' );
114
115
			$factory->register( 'user_id', 'Carbon_Fields\\Container\\Condition\\User_ID_Condition' );
116
			$factory->register( 'user_role', 'Carbon_Fields\\Container\\Condition\\User_Role_Condition' );
117
			$factory->register( 'user_capability', 'Carbon_Fields\\Container\\Condition\\User_Capability_Condition' );
118
119
			$factory->register( 'current_user_id', 'Carbon_Fields\\Container\\Condition\\Current_User_ID_Condition' );
120
			$factory->register( 'current_user_role', 'Carbon_Fields\\Container\\Condition\\Current_User_Role_Condition' );
121
			$factory->register( 'current_user_capability', 'Carbon_Fields\\Container\\Condition\\Current_User_Capability_Condition' );
122
			return $factory;
123
		};
124
	}
125
126
	protected static function install_comparers( $ioc ) {
127
		$ioc['container_condition_comparer_type_equality'] = function() {
128
			return new \Carbon_Fields\Container\Condition\Comparer\Equality_Comparer();
129
		};
130
		$ioc['container_condition_comparer_type_contain'] = function() {
131
			return new \Carbon_Fields\Container\Condition\Comparer\Contain_Comparer();
132
		};
133
		$ioc['container_condition_comparer_type_scalar'] = function() {
134
			return new \Carbon_Fields\Container\Condition\Comparer\Scalar_Comparer();
135
		};
136
		$ioc['container_condition_comparer_type_regex'] = function() {
137
			return new \Carbon_Fields\Container\Condition\Comparer\Regex_Comparer();
138
		};
139
		$ioc['container_condition_comparer_type_custom'] = function() {
140
			return new \Carbon_Fields\Container\Condition\Comparer\Custom_Comparer();
141
		};
142
143 View Code Duplication
		$ioc['container_condition_comparers_generic'] = function( $ioc ) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
144
			return array(
145
				$ioc['container_condition_comparer_type_equality'],
146
				$ioc['container_condition_comparer_type_contain'],
147
				$ioc['container_condition_comparer_type_scalar'],
148
				$ioc['container_condition_comparer_type_regex'],
149
				$ioc['container_condition_comparer_type_custom'],
150
			);
151
		};
152
153 View Code Duplication
		$ioc['container_condition_comparers_wo_scalar'] = function( $ioc ) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
154
			return array(
155
				$ioc['container_condition_comparer_type_equality'],
156
				$ioc['container_condition_comparer_type_contain'],
157
				$ioc['container_condition_comparer_type_regex'],
158
				$ioc['container_condition_comparer_type_custom'],
159
			);
160
		};
161
	}
162
163
	protected static function install_translators( $ioc ) {
164
		$ioc['container_condition_translator_array'] = function( $ioc ) {
165
			return new \Carbon_Fields\Container\Condition\Translator\Array_Translator( $ioc['container_condition_factory'] );
166
		};
167
	}
168
}