Completed
Push — milestone/2_0/react-ui ( 75a435...d33541 )
by
unknown
03:48
created

Repository   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 195
Duplicated Lines 18.46 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 35.9%

Importance

Changes 0
Metric Value
dl 36
loc 195
rs 10
c 0
b 0
f 0
ccs 28
cts 78
cp 0.359
wmc 23
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A register_container() 0 5 1
A initialize_containers() 0 9 2
A get_containers() 0 17 4
A get_field_in_container() 19 19 4
A get_field_in_containers() 17 17 4
A get_active_containers() 0 5 1
A is_unique_container_id() 0 3 1
A get_unique_container_id() 0 16 2
A register_unique_container_id() 0 5 2
A unregister_unique_container_id() 0 5 2

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\Container;
4
5
use Carbon_Fields\Helper\Helper;
6
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
7
8
/**
9
 * Keeps track of all instantiated containers
10
 */
11
class Repository {
12
	/**
13
	 * List of registered unique container ids
14
	 *
15
	 * @see get_unique_container_id()
16
	 * @see register_unique_container_id()
17
	 * @see unregister_unique_container_id()
18
	 * @var array
19
	 */
20
	protected $registered_container_ids = array();
1 ignored issue
show
Comprehensibility Naming introduced by
The variable name $registered_container_ids 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...
21
22
	/**
23
	 * List of registered containers that should be initialized
24
	 *
25
	 * @see initialize_containers()
26
	 * @var array
27
	 */
28
	protected $pending_containers = array();
29
30
	/**
31
	 * List of all containers
32
	 *
33
	 * @see _attach()
34
	 * @var array
35
	 */
36
	protected $containers = array();
37
38
	/**
39
	 * Register a container with the repository
40
	 *
41
	 * @return array
42
	 */
43 5
	public function register_container( Container $container ) {
44 5
		$this->register_unique_container_id( $container->id );
45 5
		$this->containers[] = $container;
46 5
		$this->pending_containers[] = $container;
47 5
	}
48
49
	/**
50
	 * Initialize registered containers
51
	 *
52
	 * @return array
53
	 */
54 3
	public function initialize_containers() {
55 3
		$initialized_containers = array();
56 3
		while ( ( $container = array_shift( $this->pending_containers ) ) ) {
57 3
			$container->init();
58 3
			$initialized_containers[] = $container;
59 3
		}
60
61 3
		return $initialized_containers;
62
	}
63
64
	/**
65
	 * Return all containers
66
	 *
67
	 * @param string $type Container type to filter for
68
	 * @return array
69
	 */
70
	public function get_containers( $type = null ) {
71
		$raw_containers = $this->containers;
72
		$containers = array();
73
74
		if ( $type === null ) {
75
			$containers = $raw_containers;
76
		} else {
77
			$normalized_type = Helper::normalize_type( $type );
78
			foreach ( $raw_containers as $container ) {
79
				if ( $container->type === $normalized_type ) {
80
					$containers[] = $container;
81
				}
82
			}
83
		}
84
85
		return $containers;
86
	}
87
88
	/**
89
	 * Return field in a container with supplied id
90
	 *
91
	 * @param  string                    $field_name
92
	 * @param  string                    $container_id
93
	 * @param  bool                      $include_nested_fields
94
	 * @return Carbon_Fields\Field\Field
95
	 */
96 View Code Duplication
	public function get_field_in_container( $field_name, $container_id, $include_nested_fields = true ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
97
		$containers = $this->get_containers();
98
		$field = null;
99
100
		foreach ( $containers as $container ) {
101
			if ( $container->get_id() !== $container_id ) {
102
				continue;
103
			}
104
105
			if ( $include_nested_fields ) {
106
				$field = $container->get_field_by_name( $field_name );
107
			} else {
108
				$field = $container->get_root_field_by_name( $field_name );
109
			}
110
			break;
111
		}
112
113
		return $field;
114
	}
115
116
	/**
117
	 * Return field in containers
118
	 *
119
	 * @param  string                    $field_name
120
	 * @param  string                    $container_type
121
	 * @param  bool                      $include_nested_fields
122
	 * @return Carbon_Fields\Field\Field
123
	 */
124 View Code Duplication
	public function get_field_in_containers( $field_name, $container_type = null, $include_nested_fields = true ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
125
		$containers = $this->get_containers( $container_type );
126
		$field = null;
127
128
		foreach ( $containers as $container ) {
129
			if ( $include_nested_fields ) {
130
				$field = $container->get_field_by_name( $field_name );
131
			} else {
132
				$field = $container->get_root_field_by_name( $field_name );
133
			}
134
			if ( $field ) {
135
				break;
136
			}
137
		}
138
139
		return $field;
140
	}
141
142
	/**
143
	 * Return all currently active containers
144
	 *
145
	 * @return array
146
	 */
147
	public function get_active_containers() {
148 1
		return array_filter( $this->containers, function( $container ) {
149 1
			return $container->is_active();
150 1
		} );
151
	}
152
153
	/**
154
	 * Check if container identificator id is unique
155
	 * 
156
	 * @param string $id
157
	 */
158
	public function is_unique_container_id( $id ) {
159
		return ! in_array( $id, $this->registered_container_ids );
160
	}
161
162
	/**
163
	 * Generate a unique container identificator id based on container title
164
	 * 
165
	 * @param string $title
166
	 */
167 3
	public function get_unique_container_id( $title ) {
168 3
		$id = remove_accents( $title );
169 3
		$id = strtolower( $id );
170 3
		$id = preg_replace( '~[\-\s]+~', '_', $id );
171 3
		$id = preg_replace( '~[^\w\_]+~', '', $id );
172 3
		$id = preg_replace( '~_{2,}~', '_', $id );
173 3
		$base = $id;
174 3
		$suffix = 0;
175
176 3
		while ( ! $this->is_unique_container_id( $id ) ) {
177 2
			$suffix++;
178 2
			$id = $base . strval( $suffix );
179 2
		}
180
181 3
		return $id;
182
	}
183
184
	/**
185
	 * Add container identificator id to the list of unique container ids
186
	 *
187
	 * @param string $id
188
	 */
189
	protected function register_unique_container_id( $id ) {
190
		if ( $this->is_unique_container_id( $id ) ) {
191
			$this->registered_container_ids[] = $id;
192
		}
193
	}
194
195
	/**
196
	 * Remove container identificator id from the list of unique container ids
197
	 *
198
	 * @param string $id
199
	 */
200
	protected function unregister_unique_container_id( $id ) {
201
		if ( ! $this->is_unique_container_id( $id ) ) {
202
			unset( $this->registered_container_ids[ array_search( $id, $this->registered_container_ids ) ] );
203
		}
204
	}
205
}
206