Complex classes like Fulfillable_Collection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Fulfillable_Collection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Fulfillable_Collection implements Fulfillable { |
||
10 | |||
11 | /** |
||
12 | * Condition factory used to translated condition types |
||
13 | * |
||
14 | * @var Factory |
||
15 | */ |
||
16 | protected $condition_factory; |
||
17 | |||
18 | /** |
||
19 | * Array translator used to support array representations of fulfillables |
||
20 | * |
||
21 | * @var Array_Translator |
||
22 | */ |
||
23 | protected $array_translator; |
||
24 | |||
25 | /** |
||
26 | * Array of fulfillables in this collection |
||
27 | * |
||
28 | * @var array<array> |
||
29 | */ |
||
30 | protected $fulfillables = array(); |
||
31 | |||
32 | /** |
||
33 | * Array of supported fulfillable comparisons |
||
34 | * |
||
35 | * @var array<string> |
||
36 | */ |
||
37 | protected $supported_fulfillable_comparisons = array( 'AND', 'OR' ); |
||
38 | |||
39 | /** |
||
40 | * Array of allowed condition types which propagate to child collections |
||
41 | * |
||
42 | * @var array<string> |
||
43 | */ |
||
44 | protected $condition_type_list = array(); |
||
45 | |||
46 | /** |
||
47 | * Whether the condition type list is a whitelist or a blacklist |
||
48 | * |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected $condition_type_list_whitelist = false; |
||
52 | |||
53 | /** |
||
54 | * Constructor |
||
55 | * |
||
56 | * @param Factory $condition_factory |
||
57 | * @param Array_Translator $array_translator |
||
58 | */ |
||
59 | public function __construct( Factory $condition_factory, Array_Translator $array_translator ) { |
||
63 | |||
64 | /** |
||
65 | * Get an array of the fulfillables in this collection |
||
66 | * |
||
67 | * @return array<Fulfillable> |
||
68 | */ |
||
69 | 1 | public function get_fulfillables() { |
|
72 | |||
73 | /** |
||
74 | * Get array of allowed condition types |
||
75 | * |
||
76 | * @return array<string> |
||
77 | */ |
||
78 | public function get_condition_type_list() { |
||
81 | |||
82 | /** |
||
83 | * Set array of allowed condition types |
||
84 | * WARNING: this will NOT remove already added conditions which are no longer allowed |
||
85 | * |
||
86 | * @param array<string> $condition_type_list |
||
87 | * @param bool $whitelist |
||
88 | * @return Fulfillable_Collection $this |
||
89 | */ |
||
90 | public function set_condition_type_list( $condition_type_list, $whitelist ) { |
||
96 | |||
97 | /** |
||
98 | * Check if conditions types list is a whitelist |
||
99 | * |
||
100 | * @return bool |
||
101 | */ |
||
102 | public function is_condition_type_list_whitelist() { |
||
105 | |||
106 | /** |
||
107 | * Check if condition type is allowed |
||
108 | * |
||
109 | * @param string $condition_type |
||
110 | * @return bool |
||
111 | */ |
||
112 | public function is_condition_type_allowed( $condition_type ) { |
||
119 | |||
120 | /** |
||
121 | * Propagate allowed condition types to child collections |
||
122 | */ |
||
123 | protected function propagate_condition_type_list() { |
||
132 | |||
133 | /** |
||
134 | * Shorthand for where with OR comparison |
||
135 | * |
||
136 | * @param string|array|callable $condition_type |
||
137 | * @param string $comparison_operator Can be skipped. Defaults to "=" |
||
138 | * @param mixed $value |
||
139 | * @return Fulfillable_Collection $this |
||
140 | */ |
||
141 | public function or_where( $condition_type, $comparison_operator = '=', $value = null ) { |
||
142 | $this->where( $condition_type, $comparison_operator, $value, 'OR' ); |
||
143 | return $this; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Add fulfillable with optional comparison_operator |
||
148 | * This method assumes there is no fulfillable that can be compared with literal NULL |
||
149 | * |
||
150 | * @param string|array|callable $condition_type |
||
151 | * @param string $comparison_operator Can be skipped. Defaults to "=" |
||
152 | * @param mixed $value |
||
153 | * @param string $fulfillable_comparison |
||
154 | * @return Fulfillable_Collection $this |
||
155 | */ |
||
156 | 9 | public function where( $condition_type, $comparison_operator = '=', $value = null, $fulfillable_comparison = 'AND' ) { |
|
157 | 9 | if ( is_array( $condition_type ) ) { |
|
158 | 2 | return $this->where_array( $condition_type, $fulfillable_comparison ); |
|
159 | } |
||
160 | |||
161 | 7 | if ( is_callable( $condition_type ) ) { |
|
162 | 2 | return $this->where_collection( $condition_type, $fulfillable_comparison ); |
|
163 | } |
||
164 | |||
165 | 7 | if ( ! $this->is_condition_type_allowed( $condition_type ) ) { |
|
166 | Incorrect_Syntax_Exception::raise( 'Unsupported container condition used: ' . $condition_type ); |
||
167 | return $this; |
||
168 | } |
||
169 | |||
170 | 7 | if ( $value === null ) { |
|
171 | // We do not have a supplied comparison_operator so we default to "=" |
||
172 | 5 | $value = $comparison_operator; |
|
173 | 5 | $comparison_operator = '='; |
|
174 | 5 | } |
|
175 | |||
176 | 7 | $condition = $this->condition_factory->make( $condition_type ); |
|
177 | 6 | $condition->set_comparison_operator( $comparison_operator ); |
|
178 | 6 | $condition->set_value( $value ); |
|
179 | 6 | $this->add_fulfillable( $condition, $fulfillable_comparison ); |
|
180 | 6 | return $this; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Add a Fulfillable through array representation |
||
185 | * |
||
186 | * @param array $fulfillable_as_array |
||
187 | * @param string $fulfillable_comparison |
||
188 | * @return Fulfillable_Collection $this |
||
189 | */ |
||
190 | protected function where_array( $fulfillable_as_array, $fulfillable_comparison) { |
||
191 | $fulfillable = $this->array_translator->foreign_to_fulfillable( $fulfillable_as_array ); |
||
192 | $this->add_fulfillable( $fulfillable, $fulfillable_comparison ); |
||
193 | return $this; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Add a Fulfillable_Collection for nested logic |
||
198 | * |
||
199 | * @param callable $collection_callable |
||
200 | * @param string $fulfillable_comparison |
||
201 | * @return Fulfillable_Collection $this |
||
202 | */ |
||
203 | protected function where_collection( $collection_callable, $fulfillable_comparison) { |
||
204 | $collection = \Carbon_Fields\Carbon_Fields::resolve( 'container_condition_fulfillable_collection' ); |
||
205 | $collection->set_condition_type_list( $this->get_condition_type_list(), $this->is_condition_type_list_whitelist() ); |
||
206 | $collection_callable( $collection ); |
||
207 | $this->add_fulfillable( $collection, $fulfillable_comparison ); |
||
208 | return $this; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Add fulfillable to collection |
||
213 | * |
||
214 | * @param Fulfillable $fulfillable |
||
215 | * @param string $fulfillable_comparison See static::$supported_fulfillable_comparisons |
||
216 | */ |
||
217 | 2 | public function add_fulfillable( Fulfillable $fulfillable, $fulfillable_comparison ) { |
|
228 | |||
229 | /** |
||
230 | * Remove fulfillable from collection |
||
231 | * |
||
232 | * @param Fulfillable $fulfillable |
||
233 | * @return bool Fulfillable found and removed |
||
234 | */ |
||
235 | public function remove_fulfillable( Fulfillable $fulfillable ) { |
||
247 | |||
248 | /** |
||
249 | * Get a copy of the collection with conditions not in the whitelist filtered out |
||
250 | * |
||
251 | * @param array<string> $condition_whitelist |
||
252 | * @return Fulfillable_Collection |
||
253 | */ |
||
254 | 2 | public function filter( $condition_whitelist ) { |
|
281 | |||
282 | /** |
||
283 | * Get a copy of the collection with passed conditions evaluated into boolean conditions |
||
284 | * Useful when evaluating only certain condition types but preserving the rest |
||
285 | * or when passing dynamic conditions to the front-end |
||
286 | * |
||
287 | * @param array<string> $condition_types |
||
288 | * @param array|boolean $environment Environment array or a boolean value to force on conditions |
||
289 | * @param array $comparison_operators Array of comparison operators to evaluate regardless of condition type |
||
290 | * @return Fulfillable_Collection |
||
291 | */ |
||
292 | 3 | public function evaluate( $condition_types, $environment, $comparison_operators = array() ) { |
|
320 | |||
321 | /** |
||
322 | * Check if all fulfillables are fulfilled taking into account their fulfillable comparison |
||
323 | * |
||
324 | * @param array $environment |
||
325 | * @return bool |
||
326 | */ |
||
327 | 14 | public function is_fulfilled( $environment ) { |
|
356 | } |