Complex classes like Event 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 Event, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Event |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * A machine readable name for this Event |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $apiName; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Whether or not this Event has been archived |
||
| 26 | * @var boolean |
||
| 27 | */ |
||
| 28 | private $archived; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * A category for this Event. Can be 'add_to_cart', 'save', 'search', 'share', |
||
| 32 | * 'purchase', 'convert', 'sign_up', 'subscribe' or 'other'. |
||
| 33 | * @var type |
||
| 34 | */ |
||
| 35 | private $category; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Creation date for this Event |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $created; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * A description for this Event |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $description; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * A filter object for this Event |
||
| 51 | * @var EventFilter |
||
| 52 | */ |
||
| 53 | private $eventFilter; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The type of this Event. Can be custom, click, pageview, classic_custom, |
||
| 57 | * classic_click, classic_pageview, classic_engagement, classic_revenue, |
||
| 58 | * classic_mobile_tap, classic_mobile_view, classic_mobile_session, |
||
| 59 | * classic_mobile_session_length or classic_mobile_num_session |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | private $eventType; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Unique string identifier for this event within the project. |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private $key; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * A human readable name for this Event |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | private $name; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The Page ID associated with this Event |
||
| 78 | * @var integer |
||
| 79 | */ |
||
| 80 | private $pageId; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The parent Project ID of this Event |
||
| 84 | * @var integer |
||
| 85 | */ |
||
| 86 | private $projectId; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The unique identifier of the Event |
||
| 90 | * @var integer |
||
| 91 | */ |
||
| 92 | private $id; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Whether or not this Event is a classic Event |
||
| 96 | * @var boolean |
||
| 97 | */ |
||
| 98 | private $isClassic; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Whether this Event may be edited |
||
| 102 | * @var boolean |
||
| 103 | */ |
||
| 104 | private $isEditable; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * |
||
| 108 | * @var InPageEventConfig |
||
| 109 | */ |
||
| 110 | private $config; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Constructor. |
||
| 114 | */ |
||
| 115 | 9 | public function __construct($options = array()) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Returns this object as array. |
||
| 142 | */ |
||
| 143 | 5 | public function toArray() |
|
| 172 | |||
| 173 | 5 | public function getApiName() |
|
| 177 | |||
| 178 | public function setApiName($apiName) |
||
| 182 | |||
| 183 | 7 | public function getArchived() |
|
| 187 | |||
| 188 | 9 | public function setArchived($archived) |
|
| 192 | |||
| 193 | 7 | public function getCategory() |
|
| 197 | |||
| 198 | 9 | public function setCategory($category) |
|
| 202 | |||
| 203 | 5 | public function getCreated() |
|
| 207 | |||
| 208 | 9 | public function setCreated($created) |
|
| 212 | |||
| 213 | 5 | public function getDescription() |
|
| 217 | |||
| 218 | 9 | public function setDescription($description) |
|
| 222 | |||
| 223 | 7 | public function getEventFilter() |
|
| 227 | |||
| 228 | 7 | public function setEventFilter($eventFilter) |
|
| 232 | |||
| 233 | 5 | public function getEventType() |
|
| 237 | |||
| 238 | 9 | public function setEventType($eventType) |
|
| 242 | |||
| 243 | 5 | public function getKey() |
|
| 247 | |||
| 248 | 9 | public function setKey($key) |
|
| 252 | |||
| 253 | 9 | public function getName() |
|
| 257 | |||
| 258 | 9 | public function setName($name) |
|
| 262 | |||
| 263 | 5 | public function getPageId() |
|
| 267 | |||
| 268 | 9 | public function setPageId($pageId) |
|
| 272 | |||
| 273 | 5 | public function getProjectId() |
|
| 277 | |||
| 278 | 9 | public function setProjectId($projectId) |
|
| 282 | |||
| 283 | 5 | public function getId() |
|
| 287 | |||
| 288 | 9 | public function setId($id) |
|
| 292 | |||
| 293 | 5 | public function getIsClassic() |
|
| 297 | |||
| 298 | 9 | public function setIsClassic($isClassic) |
|
| 302 | |||
| 303 | 7 | public function getIsEditable() |
|
| 307 | |||
| 308 | 9 | public function setIsEditable($isEditable) |
|
| 312 | |||
| 313 | 5 | public function getConfig() |
|
| 317 | |||
| 318 | public function setConfig($config) |
||
| 322 | } |
||
| 323 | |||
| 329 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.