Complex classes like Circle 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 Circle, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Circle implements \JsonSerializable { |
||
30 | |||
31 | const CIRCLES_PERSONAL = 1; |
||
32 | const CIRCLES_HIDDEN = 2; |
||
33 | const CIRCLES_PRIVATE = 4; |
||
34 | const CIRCLES_PUBLIC = 8; |
||
35 | |||
36 | const CIRCLES_ALL = 15; |
||
37 | |||
38 | private $id; |
||
39 | private $name; |
||
40 | |||
41 | /** @var Member */ |
||
42 | private $owner; |
||
43 | |||
44 | /** @var Member */ |
||
45 | private $user; |
||
46 | private $description; |
||
47 | private $type; |
||
48 | private $typeString; |
||
49 | private $typeLongString; |
||
50 | private $creation; |
||
51 | private $members; |
||
52 | private $info; |
||
53 | |||
54 | public function __construct() { |
||
57 | |||
58 | |||
59 | public function setId($id) { |
||
64 | |||
65 | public function getId() { |
||
68 | |||
69 | |||
70 | public function setName($name) { |
||
75 | |||
76 | public function getName() { |
||
79 | |||
80 | |||
81 | public function getOwner() { |
||
84 | |||
85 | public function setOwner($owner) { |
||
88 | |||
89 | |||
90 | public function getUser() { |
||
93 | |||
94 | public function setUser($user) { |
||
97 | |||
98 | |||
99 | public function setDescription($description) { |
||
104 | |||
105 | public function getDescription() { |
||
108 | |||
109 | |||
110 | public function setType($type) { |
||
118 | |||
119 | public function getType() { |
||
122 | |||
123 | public function setTypeString($str) { |
||
128 | |||
129 | public function getTypeString() { |
||
132 | |||
133 | public function setTypeLongString($str) { |
||
136 | |||
137 | public function getTypeLongString() { |
||
140 | |||
141 | |||
142 | public function setInfo($str) { |
||
145 | |||
146 | public function getInfo() { |
||
149 | |||
150 | |||
151 | public function setCreation($creation) { |
||
156 | |||
157 | public function getCreation() { |
||
160 | |||
161 | |||
162 | public function setMembers($members) { |
||
167 | |||
168 | public function getMembers() { |
||
171 | |||
172 | |||
173 | public function toString() { |
||
176 | |||
177 | public function jsonSerialize() { |
||
189 | |||
190 | public static function fromArray($arr) { |
||
207 | |||
208 | |||
209 | /** |
||
210 | * return Owner Infos from Array |
||
211 | * |
||
212 | * @param $array |
||
213 | * |
||
214 | * @return null|Member |
||
215 | */ |
||
216 | private static function getOwnerMemberFromArray($array) { |
||
226 | |||
227 | /** |
||
228 | * return User Infos from Array |
||
229 | * |
||
230 | * @param $array |
||
231 | * |
||
232 | * @return null|Member |
||
233 | */ |
||
234 | private static function getUserMemberFromArray($array) { |
||
250 | |||
251 | |||
252 | public static function TypeString($type) { |
||
268 | |||
269 | /** |
||
270 | * @param $type |
||
271 | * |
||
272 | * @return string |
||
273 | */ |
||
274 | public static function TypeLongString($type) { |
||
290 | |||
291 | } |
||
292 | |||
294 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.