Complex classes like Rollout 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 Rollout, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Rollout |
||
14 | { |
||
15 | /** |
||
16 | * @var StorageInterface |
||
17 | */ |
||
18 | private $storage; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private $groups; |
||
24 | |||
25 | /** |
||
26 | * @param StorageInterface $storage |
||
27 | */ |
||
28 | public function __construct(StorageInterface $storage) |
||
35 | |||
36 | /** |
||
37 | * @param string $feature |
||
38 | */ |
||
39 | public function activate($feature) |
||
47 | |||
48 | /** |
||
49 | * @param string $feature |
||
50 | */ |
||
51 | public function deactivate($feature) |
||
59 | |||
60 | /** |
||
61 | * @param string $feature |
||
62 | * @param string $group |
||
63 | */ |
||
64 | public function activateGroup($feature, $group) |
||
72 | |||
73 | /** |
||
74 | * @param string $feature |
||
75 | * @param string $group |
||
76 | */ |
||
77 | public function deactivateGroup($feature, $group) |
||
85 | |||
86 | /** |
||
87 | * @param string $feature |
||
88 | * @param RolloutUserInterface $user |
||
89 | */ |
||
90 | public function activateUser($feature, RolloutUserInterface $user) |
||
98 | |||
99 | /** |
||
100 | * @param string $feature |
||
101 | * @param RolloutUserInterface $user |
||
102 | */ |
||
103 | public function deactivateUser($feature, RolloutUserInterface $user) |
||
111 | |||
112 | /** |
||
113 | * @param string $group |
||
114 | * @param \Closure $closure |
||
115 | */ |
||
116 | public function defineGroup($group, \Closure $closure) |
||
120 | |||
121 | /** |
||
122 | * @param string $feature |
||
123 | * @param RolloutUserInterface|null $user |
||
124 | * @param array $requestParameters |
||
125 | * @return bool |
||
126 | */ |
||
127 | public function isActive($feature, RolloutUserInterface $user = null, array $requestParameters = array()) |
||
133 | |||
134 | /** |
||
135 | * @param string $feature |
||
136 | * @param integer $percentage |
||
137 | */ |
||
138 | public function activatePercentage($feature, $percentage) |
||
146 | |||
147 | /** |
||
148 | * @param string $feature |
||
149 | */ |
||
150 | public function deactivatePercentage($feature) |
||
158 | |||
159 | /** |
||
160 | * @param string $feature |
||
161 | * @param string $requestParam |
||
162 | */ |
||
163 | public function activateRequestParam($feature, $requestParam) |
||
171 | |||
172 | /** |
||
173 | * @param string $feature |
||
174 | */ |
||
175 | public function deactivateRequestParam($feature) |
||
183 | |||
184 | /** |
||
185 | * @param string $group |
||
186 | * @param RolloutUserInterface|null $user |
||
187 | * @return bool |
||
188 | */ |
||
189 | public function isActiveInGroup($group, RolloutUserInterface $user = null) |
||
199 | |||
200 | /** |
||
201 | * @param string $feature |
||
202 | * @return Feature |
||
203 | */ |
||
204 | public function get($feature) |
||
218 | |||
219 | /** |
||
220 | * Remove a feature definition from rollout |
||
221 | * |
||
222 | * @param string $feature |
||
223 | */ |
||
224 | public function remove($feature) |
||
234 | |||
235 | /** |
||
236 | * Update feature specific data |
||
237 | * |
||
238 | * @example $rollout->setFeatureData('chat', array( |
||
239 | * 'description' => 'foo', |
||
240 | * 'release_date' => 'bar', |
||
241 | * 'whatever' => 'baz' |
||
242 | * )); |
||
243 | * |
||
244 | * @param string $feature |
||
245 | * @param array $data |
||
246 | */ |
||
247 | public function setFeatureData($feature, array $data) |
||
255 | |||
256 | /** |
||
257 | * Clear all feature data |
||
258 | * |
||
259 | * @param string $feature |
||
260 | */ |
||
261 | public function clearFeatureData($feature) |
||
269 | |||
270 | /** |
||
271 | * @return array |
||
272 | */ |
||
273 | public function features() |
||
283 | |||
284 | /** |
||
285 | * @param string $name |
||
286 | * @return string |
||
287 | */ |
||
288 | private function key($name) |
||
292 | |||
293 | /** |
||
294 | * @return string |
||
295 | */ |
||
296 | private function featuresKey() |
||
300 | |||
301 | /** |
||
302 | * @param Feature $feature |
||
303 | */ |
||
304 | protected function save(Feature $feature) |
||
315 | } |
||
316 |