Complex classes like CustomField 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 CustomField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class CustomField extends Model |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var int |
||
| 11 | */ |
||
| 12 | protected $id; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var int |
||
| 16 | */ |
||
| 17 | protected $projectId; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $name; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $type; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | protected $showInLeads; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var bool |
||
| 36 | */ |
||
| 37 | protected $showInDeals; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var bool |
||
| 41 | */ |
||
| 42 | protected $isMultiSelect; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | protected $isRequired; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $description; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | protected $isMultiLine; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var Category[] |
||
| 61 | */ |
||
| 62 | protected $categories = []; |
||
| 63 | |||
| 64 | public function __construct(array $data = []) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | public function toArray() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return bool |
||
| 129 | * @throws LPTrackerSDKException |
||
| 130 | */ |
||
| 131 | public function validate() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return int |
||
| 142 | */ |
||
| 143 | public function getId() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param int $id |
||
| 150 | * @return CustomField |
||
| 151 | */ |
||
| 152 | public function setId($id) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return int |
||
| 160 | */ |
||
| 161 | public function getProjectId() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param int $projectId |
||
| 168 | * @return CustomField |
||
| 169 | */ |
||
| 170 | public function setProjectId($projectId) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function getName() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param string $name |
||
| 186 | * @return CustomField |
||
| 187 | */ |
||
| 188 | public function setName($name) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public function getType() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param string $type |
||
| 204 | * @return CustomField |
||
| 205 | */ |
||
| 206 | public function setType($type) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return bool |
||
| 214 | */ |
||
| 215 | public function isShowInLeads() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param bool $showInLeads |
||
| 222 | * @return CustomField |
||
| 223 | */ |
||
| 224 | public function setShowInLeads($showInLeads) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | public function isShowInDeals() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param bool $showInDeals |
||
| 240 | * @return CustomField |
||
| 241 | */ |
||
| 242 | public function setShowInDeals($showInDeals) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | public function isMultiSelect() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param bool $isMultiSelect |
||
| 258 | * @return CustomField |
||
| 259 | */ |
||
| 260 | public function setIsMultiSelect($isMultiSelect) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | public function isRequired() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param bool $isRequired |
||
| 276 | * @return CustomField |
||
| 277 | */ |
||
| 278 | public function setIsRequired($isRequired) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function getDescription() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $description |
||
| 294 | * @return CustomField |
||
| 295 | */ |
||
| 296 | public function setDescription($description) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | public function isMultiLine() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param bool $isMultiLine |
||
| 312 | * @return CustomField |
||
| 313 | */ |
||
| 314 | public function setIsMultiLine($isMultiLine) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @return Category[] |
||
| 322 | */ |
||
| 323 | public function getCategories() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param Category[] $categories |
||
| 330 | * @return $this |
||
| 331 | */ |
||
| 332 | public function setCategories(array $categories) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param Category $category |
||
| 340 | * @return $this |
||
| 341 | */ |
||
| 342 | public function addCategory(Category $category) |
||
| 347 | } |
||
| 348 |