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 | 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 ) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Check if conditions types list is a whitelist |
||
| 100 | * |
||
| 101 | * @return bool |
||
| 102 | */ |
||
| 103 | public function is_condition_type_list_whitelist() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Check if condition type is allowed |
||
| 109 | * |
||
| 110 | * @param string $condition_type |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | public function is_condition_type_allowed( $condition_type ) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Propagate allowed condition types to child collections |
||
| 123 | */ |
||
| 124 | protected function propagate_condition_type_list() { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Shorthand for when with AND comparison |
||
| 136 | * |
||
| 137 | * @param string|array|callable $condition_type |
||
| 138 | * @param string $comparison_operator Can be skipped. Defaults to "=" |
||
| 139 | * @param mixed $value |
||
| 140 | * @return Fulfillable_Collection $this |
||
| 141 | */ |
||
| 142 | public function and_when( $condition_type, $comparison_operator = '=', $value = null ) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Shorthand for when with OR comparison |
||
| 149 | * |
||
| 150 | * @param string|array|callable $condition_type |
||
| 151 | * @param string $comparison_operator Can be skipped. Defaults to "=" |
||
| 152 | * @param mixed $value |
||
| 153 | * @return Fulfillable_Collection $this |
||
| 154 | */ |
||
| 155 | public function or_when( $condition_type, $comparison_operator = '=', $value = null ) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Add fulfillable with optional comparison_operator |
||
| 162 | * This method assumes there is no fulfillable that can be compared with literal NULL |
||
| 163 | * |
||
| 164 | * @param string|array|callable $condition_type |
||
| 165 | * @param string $comparison_operator Can be skipped. Defaults to "=" |
||
| 166 | * @param mixed $value |
||
| 167 | * @param string $fulfillable_comparison |
||
| 168 | * @return Fulfillable_Collection $this |
||
| 169 | */ |
||
| 170 | public function when( $condition_type, $comparison_operator = '=', $value = null, $fulfillable_comparison = 'AND' ) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Add a Fulfillable through array representation |
||
| 198 | * |
||
| 199 | * @param array $fulfillable_as_array |
||
| 200 | * @param string $fulfillable_comparison |
||
| 201 | * @return Fulfillable_Collection $this |
||
| 202 | */ |
||
| 203 | protected function when_array( $fulfillable_as_array, $fulfillable_comparison) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Add a Fulfillable_Collection for nested logic |
||
| 211 | * |
||
| 212 | * @param callable $collection_callable |
||
| 213 | * @param string $fulfillable_comparison |
||
| 214 | * @return Fulfillable_Collection $this |
||
| 215 | */ |
||
| 216 | protected function when_collection( $collection_callable, $fulfillable_comparison) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Add fulfillable to collection |
||
| 226 | * |
||
| 227 | * @param Fulfillable $fulfillable |
||
| 228 | * @param string $fulfillable_comparison See static::$supported_fulfillable_comparisons |
||
| 229 | */ |
||
| 230 | public function add_fulfillable( Fulfillable $fulfillable, $fulfillable_comparison ) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Remove fulfillable from collection |
||
| 243 | * |
||
| 244 | * @param Fulfillable $fulfillable |
||
| 245 | * @return bool Fulfillable found and removed |
||
| 246 | */ |
||
| 247 | public function remove_fulfillable( Fulfillable $fulfillable ) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get a copy of the collection with conditions not in the whitelist filtered out |
||
| 262 | * |
||
| 263 | * @param array<string> $condition_whitelist |
||
| 264 | * @return Fulfillable_Collection |
||
| 265 | */ |
||
| 266 | public function filter( $condition_whitelist ) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get a copy of the collection with passed conditions evaluated into boolean conditions |
||
| 296 | * Useful when evaluating only certain condition types but preserving the rest |
||
| 297 | * or when passing dynamic conditions to the front-end |
||
| 298 | * |
||
| 299 | * @param array<string> $condition_types |
||
| 300 | * @param array|boolean $environment Environment array or a boolean value to force on conditions |
||
| 301 | * @return Fulfillable_Collection |
||
| 302 | */ |
||
| 303 | public function evaluate( $condition_types, $environment ) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Check if all fulfillables are fulfilled taking into account their fulfillable comparison |
||
| 333 | * |
||
| 334 | * @param array $environment |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | public function is_fulfilled( $environment ) { |
||
| 366 | } |