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 |
||
10 | class Fulfillable_Collection implements Fulfillable { |
||
11 | |||
12 | /** |
||
13 | * Condition factory used to translated condition types |
||
14 | * |
||
15 | * @var Factory |
||
16 | */ |
||
17 | protected $condition_factory; |
||
18 | |||
19 | /** |
||
20 | * Array translator used to support array representations of fulfillables |
||
21 | * |
||
22 | * @var Array_Translator |
||
23 | */ |
||
24 | protected $array_translator; |
||
25 | |||
26 | /** |
||
27 | * Array of fulfillables in this collection |
||
28 | * |
||
29 | * @var array<array> |
||
30 | */ |
||
31 | protected $fulfillables = array(); |
||
32 | |||
33 | /** |
||
34 | * Array of supported fulfillable comparisons |
||
35 | * |
||
36 | * @var array<string> |
||
37 | */ |
||
38 | protected $supported_fulfillable_comparisons = array( 'AND', 'OR' ); |
||
39 | |||
40 | /** |
||
41 | * Array of allowed condition types which propagate to child collections |
||
42 | * |
||
43 | * @var array<string> |
||
44 | */ |
||
45 | protected $condition_type_list = array(); |
||
46 | |||
47 | /** |
||
48 | * Whether the condition type list is a whitelist or a blacklist |
||
49 | * |
||
50 | * @var bool |
||
51 | */ |
||
52 | protected $condition_type_list_whitelist = false; |
||
53 | |||
54 | /** |
||
55 | * Constructor |
||
56 | * |
||
57 | * @param Factory $condition_factory |
||
58 | * @param Array_Translator $array_translator |
||
59 | */ |
||
60 | public function __construct( Factory $condition_factory, Array_Translator $array_translator ) { |
||
64 | |||
65 | /** |
||
66 | * Get an array of the fulfillables in this collection |
||
67 | * |
||
68 | * @return array<Fulfillable> |
||
69 | */ |
||
70 | 1 | public function get_fulfillables() { |
|
73 | |||
74 | /** |
||
75 | * Get array of allowed condition types |
||
76 | * |
||
77 | * @return array<string> |
||
78 | */ |
||
79 | public function get_condition_type_list() { |
||
82 | |||
83 | /** |
||
84 | * Set array of allowed condition types |
||
85 | * WARNING: this will NOT remove already added conditions which are no longer allowed |
||
86 | * |
||
87 | * @param array<string> $condition_type_list |
||
88 | * @param bool $whitelist |
||
89 | * @return Fulfillable_Collection $this |
||
90 | */ |
||
91 | public function set_condition_type_list( $condition_type_list, $whitelist ) { |
||
104 | |||
105 | /** |
||
106 | * Check if conditions types list is a whitelist |
||
107 | * |
||
108 | * @return bool |
||
109 | */ |
||
110 | public function is_condition_type_list_whitelist() { |
||
113 | |||
114 | /** |
||
115 | * Check if condition type is allowed |
||
116 | * |
||
117 | * @param string $condition_type |
||
118 | * @return bool |
||
119 | */ |
||
120 | public function is_condition_type_allowed( $condition_type ) { |
||
127 | |||
128 | /** |
||
129 | * Propagate allowed condition types to child collections |
||
130 | */ |
||
131 | protected function propagate_condition_type_list() { |
||
140 | |||
141 | /** |
||
142 | * Shorthand for when with AND comparison |
||
143 | * |
||
144 | * @param string|array|callable $condition_type |
||
145 | * @param string $comparison_operator Can be skipped. Defaults to "=" |
||
146 | * @param mixed $value |
||
147 | * @return Fulfillable_Collection $this |
||
148 | */ |
||
149 | public function and_when( $condition_type, $comparison_operator = '=', $value = null ) { |
||
153 | |||
154 | /** |
||
155 | * Shorthand for when with OR comparison |
||
156 | * |
||
157 | * @param string|array|callable $condition_type |
||
158 | * @param string $comparison_operator Can be skipped. Defaults to "=" |
||
159 | * @param mixed $value |
||
160 | * @return Fulfillable_Collection $this |
||
161 | */ |
||
162 | public function or_when( $condition_type, $comparison_operator = '=', $value = null ) { |
||
166 | |||
167 | /** |
||
168 | * Add fulfillable with optional comparison_operator |
||
169 | * This method assumes there is no fulfillable that can be compared with literal NULL |
||
170 | * |
||
171 | * @param string|array|callable $condition_type |
||
172 | * @param string $comparison_operator Can be skipped. Defaults to "=" |
||
173 | * @param mixed $value |
||
174 | * @param string $fulfillable_comparison |
||
175 | * @return Fulfillable_Collection $this |
||
176 | */ |
||
177 | 9 | public function when( $condition_type, $comparison_operator = '=', $value = null, $fulfillable_comparison = 'AND' ) { |
|
202 | |||
203 | /** |
||
204 | * Add a Fulfillable through array representation |
||
205 | * |
||
206 | * @param array $fulfillable_as_array |
||
207 | * @param string $fulfillable_comparison |
||
208 | * @return Fulfillable_Collection $this |
||
209 | */ |
||
210 | protected function when_array( $fulfillable_as_array, $fulfillable_comparison) { |
||
215 | |||
216 | /** |
||
217 | * Add a Fulfillable_Collection for nested logic |
||
218 | * |
||
219 | * @param callable $collection_callable |
||
220 | * @param string $fulfillable_comparison |
||
221 | * @return Fulfillable_Collection $this |
||
222 | */ |
||
223 | protected function when_collection( $collection_callable, $fulfillable_comparison) { |
||
230 | |||
231 | /** |
||
232 | * Add fulfillable to collection |
||
233 | * |
||
234 | * @param Fulfillable $fulfillable |
||
235 | * @param string $fulfillable_comparison See static::$supported_fulfillable_comparisons |
||
236 | */ |
||
237 | 2 | public function add_fulfillable( Fulfillable $fulfillable, $fulfillable_comparison ) { |
|
247 | |||
248 | /** |
||
249 | * Remove fulfillable from collection |
||
250 | * |
||
251 | * @param Fulfillable $fulfillable |
||
252 | * @return bool Fulfillable found and removed |
||
253 | */ |
||
254 | public function remove_fulfillable( Fulfillable $fulfillable ) { |
||
266 | |||
267 | /** |
||
268 | * Get a copy of the collection with conditions not in the whitelist filtered out |
||
269 | * |
||
270 | * @param array<string> $condition_whitelist |
||
271 | * @return Fulfillable_Collection |
||
272 | */ |
||
273 | 2 | public function filter( $condition_whitelist ) { |
|
300 | |||
301 | /** |
||
302 | * Get a copy of the collection with passed conditions evaluated into boolean conditions |
||
303 | * Useful when evaluating only certain condition types but preserving the rest |
||
304 | * or when passing dynamic conditions to the front-end |
||
305 | * |
||
306 | * @param array<string> $condition_types |
||
307 | * @param array|boolean $environment Environment array or a boolean value to force on conditions |
||
308 | * @return Fulfillable_Collection |
||
309 | */ |
||
310 | public function evaluate( $condition_types, $environment ) { |
||
337 | |||
338 | /** |
||
339 | * Check if all fulfillables are fulfilled taking into account their fulfillable comparison |
||
340 | * |
||
341 | * @param array $environment |
||
342 | * @return bool |
||
343 | */ |
||
344 | 14 | public function is_fulfilled( $environment ) { |
|
373 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..