Complex classes like Info 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 Info, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Info implements Model |
||
20 | { |
||
21 | protected $totalAccepted; |
||
22 | protected $totalAnswers; |
||
23 | protected $totalBadges; |
||
24 | protected $totalComments; |
||
25 | protected $totalQuestions; |
||
26 | protected $totalUnanswered; |
||
27 | protected $totalUsers; |
||
28 | protected $totalVotes; |
||
29 | protected $answersPerMinute; |
||
30 | protected $apiRevision; |
||
31 | protected $badgesPerMinute; |
||
32 | protected $newActiveUsers; |
||
33 | protected $questionsPerMinute; |
||
34 | protected $site; |
||
35 | |||
36 | public static function fromJson(array $data) |
||
73 | |||
74 | public static function fromProperties( |
||
109 | |||
110 | public function setTotalAccepted($totalAccepted) |
||
116 | |||
117 | public function getTotalAccepted() |
||
121 | |||
122 | public function setTotalAnswers($totalAnswers) |
||
128 | |||
129 | public function getTotalAnswers() |
||
133 | |||
134 | public function setTotalBadges($totalBadges) |
||
140 | |||
141 | public function getTotalBadges() |
||
145 | |||
146 | public function setTotalComments($totalComments) |
||
152 | |||
153 | public function getTotalComments() |
||
157 | |||
158 | public function setTotalQuestions($totalQuestions) |
||
164 | |||
165 | public function getTotalQuestions() |
||
169 | |||
170 | public function setTotalUnanswered($totalUnanswered) |
||
176 | |||
177 | public function getTotalUnanswered() |
||
181 | |||
182 | public function setTotalUsers($totalUsers) |
||
188 | |||
189 | public function getTotalUsers() |
||
193 | |||
194 | public function setTotalVotes($totalVotes) |
||
200 | |||
201 | public function getTotalVotes() |
||
205 | |||
206 | public function setAnswersPerMinute($answersPerMinute) |
||
212 | |||
213 | public function getAnswersPerMinute() |
||
217 | |||
218 | public function setApiRevision($apiRevision) |
||
224 | |||
225 | public function getApiRevision() |
||
229 | |||
230 | public function setBadgesPerMinute($badgesPerMinute) |
||
236 | |||
237 | public function getBadgesPerMinute() |
||
241 | |||
242 | public function setNewActiveUsers($newActiveUsers) |
||
248 | |||
249 | public function getNewActiveUsers() |
||
253 | |||
254 | public function setQuestionsPerMinute($questionsPerMinute) |
||
260 | |||
261 | public function getQuestionsPerMinute() |
||
265 | |||
266 | public function setSite(Site $site = null) |
||
272 | |||
273 | public function getSite() |
||
277 | } |
||
278 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.