Complex classes like Feature 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 Feature, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Feature |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | private $name; |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | private $groups = array(); |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $users = array(); |
||
27 | |||
28 | /** |
||
29 | * @var integer |
||
30 | */ |
||
31 | private $percentage = 0; |
||
32 | |||
33 | /** |
||
34 | * @var string|null |
||
35 | */ |
||
36 | private $requestParam; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | private $data = array(); |
||
42 | |||
43 | /** |
||
44 | * @param string $name |
||
45 | * @param string|null $settings |
||
46 | */ |
||
47 | public function __construct($name, $settings = null) |
||
72 | |||
73 | /** |
||
74 | * @return string |
||
75 | */ |
||
76 | public function getName() |
||
80 | |||
81 | /** |
||
82 | * @param integer $percentage |
||
83 | */ |
||
84 | public function setPercentage($percentage) |
||
88 | |||
89 | /** |
||
90 | * @return integer |
||
91 | */ |
||
92 | public function getPercentage() |
||
96 | |||
97 | /** |
||
98 | * @return string |
||
99 | */ |
||
100 | public function serialize() |
||
110 | |||
111 | /** |
||
112 | * @param RolloutUserInterface $user |
||
113 | */ |
||
114 | public function addUser(RolloutUserInterface $user) |
||
120 | |||
121 | /** |
||
122 | * @param RolloutUserInterface $user |
||
123 | */ |
||
124 | public function removeUser(RolloutUserInterface $user) |
||
130 | |||
131 | /** |
||
132 | * @return array |
||
133 | */ |
||
134 | public function getUsers() |
||
138 | |||
139 | /** |
||
140 | * @param string $group |
||
141 | */ |
||
142 | public function addGroup($group) |
||
148 | |||
149 | /** |
||
150 | * @param string $group |
||
151 | */ |
||
152 | public function removeGroup($group) |
||
158 | |||
159 | /** |
||
160 | * @return array |
||
161 | */ |
||
162 | public function getGroups() |
||
166 | |||
167 | /** |
||
168 | * @return string|null |
||
169 | */ |
||
170 | public function getRequestParam() |
||
174 | |||
175 | /** |
||
176 | * @param string|null $requestParam |
||
177 | */ |
||
178 | public function setRequestParam($requestParam) |
||
182 | |||
183 | /** |
||
184 | * @return array |
||
185 | */ |
||
186 | public function getData() |
||
190 | |||
191 | /** |
||
192 | * @param array $data |
||
193 | */ |
||
194 | public function setData(array $data) |
||
198 | |||
199 | /** |
||
200 | * Clear the feature of all configuration |
||
201 | */ |
||
202 | public function clear() |
||
210 | |||
211 | /** |
||
212 | * Is the feature active? |
||
213 | * |
||
214 | * @param Rollout $rollout |
||
215 | * @param RolloutUserInterface|null $user |
||
216 | * @param array $requestParameters |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function isActive(Rollout $rollout, RolloutUserInterface $user = null, array $requestParameters = array()) |
||
232 | |||
233 | /** |
||
234 | * @return array |
||
235 | */ |
||
236 | public function toArray() |
||
246 | |||
247 | /** |
||
248 | * @param array $requestParameters |
||
249 | * @return bool |
||
250 | */ |
||
251 | private function isParamInRequestParams(array $requestParameters) |
||
260 | |||
261 | /** |
||
262 | * @param RolloutUserInterface $user |
||
263 | * @return bool |
||
264 | */ |
||
265 | private function isUserInPercentage(RolloutUserInterface $user) |
||
269 | |||
270 | /** |
||
271 | * @param RolloutUserInterface $user |
||
272 | * @return boolean |
||
273 | */ |
||
274 | private function isUserInActiveUsers(RolloutUserInterface $user) |
||
278 | |||
279 | /** |
||
280 | * @param Rollout $rollout |
||
281 | * @param RolloutUserInterface|null $user |
||
282 | * @return bool |
||
283 | */ |
||
284 | private function isInActiveGroup(Rollout $rollout, RolloutUserInterface $user = null) |
||
294 | } |
||
295 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: