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() { |
||
| 56 | |||
| 57 | |||
| 58 | public function setId($id) { |
||
| 63 | |||
| 64 | public function getId() { |
||
| 67 | |||
| 68 | |||
| 69 | public function setName($name) { |
||
| 74 | |||
| 75 | public function getName() { |
||
| 78 | |||
| 79 | |||
| 80 | public function getOwner() { |
||
| 83 | |||
| 84 | public function setOwner($owner) { |
||
| 87 | |||
| 88 | |||
| 89 | public function getUser() { |
||
| 92 | |||
| 93 | public function setUser($user) { |
||
| 96 | |||
| 97 | |||
| 98 | public function setDescription($description) { |
||
| 103 | |||
| 104 | public function getDescription() { |
||
| 107 | |||
| 108 | |||
| 109 | public function setType($type) { |
||
| 117 | |||
| 118 | public function getType() { |
||
| 121 | |||
| 122 | public function setTypeString($str) { |
||
| 127 | |||
| 128 | public function getTypeString() { |
||
| 131 | |||
| 132 | public function setTypeLongString($str) { |
||
| 135 | |||
| 136 | public function getTypeLongString() { |
||
| 139 | |||
| 140 | |||
| 141 | public function setInfo($str) { |
||
| 144 | |||
| 145 | public function getInfo() { |
||
| 148 | |||
| 149 | |||
| 150 | public function setCreation($creation) { |
||
| 155 | |||
| 156 | public function getCreation() { |
||
| 159 | |||
| 160 | |||
| 161 | public function setMembers($members) { |
||
| 166 | |||
| 167 | public function getMembers() { |
||
| 170 | |||
| 171 | |||
| 172 | public function toString() { |
||
| 175 | |||
| 176 | public function jsonSerialize() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * set all infos from an Array. |
||
| 191 | * |
||
| 192 | * @param $arr |
||
| 193 | * |
||
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | public function fromArray($arr) { |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * set Owner Infos from Array |
||
| 212 | * |
||
| 213 | * @param $array |
||
| 214 | */ |
||
| 215 | private function setOwnerMemberFromArray($array) { |
||
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * set User Infos from Array |
||
| 226 | * |
||
| 227 | * @param $array |
||
| 228 | */ |
||
| 229 | private function setUserMemberFromArray($array) { |
||
| 241 | |||
| 242 | |||
| 243 | public static function TypeString($type) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param $type |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public static function TypeLongString($type) { |
||
| 281 | |||
| 282 | } |
||
| 283 | |||
| 285 |
This check looks for method names that are not written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes
databaseConnectionSeeker.