Complex classes like AbstractEntryPoint 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 AbstractEntryPoint, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | abstract class AbstractEntryPoint implements EPInterface { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Whether or not Authentication is Required |
||
| 15 | * @var bool |
||
| 16 | */ |
||
| 17 | protected $_AUTH_REQUIRED = true; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The default Module for the EntryPoint |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $_MODULE; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The URL for the EntryPoint |
||
| 27 | * - When configuring URL you define URL Parameters with $variables |
||
| 28 | * Examples: |
||
| 29 | * - Forecasts/$record_id |
||
| 30 | * - $module Variable is a keyword to place the Module property into the URL |
||
| 31 | * Examples: |
||
| 32 | * - $module/$record |
||
| 33 | * - Options property is used to replace variables in the order in which they are passed |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $_URL; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * An array of Required Data properties that should be passed in the Request |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $_REQUIRED_DATA; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The configured URL for the EntryPoint |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $url; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The configured Module for the EntryPoint |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $Module; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The passed in Options for the EntryPoint |
||
| 59 | * - If $module variable is used in $_URL static property, then 1st option will be used as Module |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $Options = array(); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The data being passed to the API EntryPoint |
||
| 66 | * @var mixed - array or Std Object |
||
| 67 | */ |
||
| 68 | protected $Data; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The Request Object, used by the EntryPoint to submit the data |
||
| 72 | * @var Object |
||
| 73 | */ |
||
| 74 | protected $Request; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The Response Object, returned by the Request Object |
||
| 78 | * @var Object |
||
| 79 | */ |
||
| 80 | protected $Response; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Access Token for authentication |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $accessToken; |
||
| 87 | |||
| 88 | public function __construct($url,$options = array()){ |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @inheritdoc |
||
| 106 | */ |
||
| 107 | public function module($module){ |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @inheritdoc |
||
| 114 | */ |
||
| 115 | public function options(array $options){ |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @inheritdoc |
||
| 122 | */ |
||
| 123 | public function data($data){ |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Override function for configuring Default Values on some EntryPoints to allow for short hand |
||
| 132 | * @param mixed $data |
||
| 133 | * @return mixed |
||
| 134 | */ |
||
| 135 | protected function configureData($data){ |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @inheritdoc |
||
| 141 | */ |
||
| 142 | public function execute(){ |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @inheritdoc |
||
| 156 | */ |
||
| 157 | public function authRequired() { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @inheritdoc |
||
| 163 | */ |
||
| 164 | public function configureAuth($accessToken) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @inheritdoc |
||
| 174 | */ |
||
| 175 | public function getModule() { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @inheritdoc |
||
| 181 | */ |
||
| 182 | public function getData(){ |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @inheritdoc |
||
| 188 | */ |
||
| 189 | public function getURL(){ |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @inheritdoc |
||
| 195 | */ |
||
| 196 | public function getResponse(){ |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @inheritdoc |
||
| 202 | */ |
||
| 203 | public function getRequest(){ |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Configures the URL, by updating any variable placeholders in the URL property on the EntryPoint |
||
| 209 | * - Replaces $module with $this->Module |
||
| 210 | * - Replaces all other variables starting with $, with options in the order they were given |
||
| 211 | */ |
||
| 212 | protected function configureURL(){ |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Setup the Request Object property, setup on initial Construct of EntryPoint |
||
| 245 | */ |
||
| 246 | protected function setupRequest(){ |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Setup the Response Object Property, not called until after Request Execution |
||
| 252 | */ |
||
| 253 | protected function setupResponse(){ |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Verify URL variables have been removed, and that valid number of options were passed. |
||
| 259 | * @return bool |
||
| 260 | * @throws EntryPointException |
||
| 261 | */ |
||
| 262 | protected function verifyURL(){ |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Validate Required Data for the Request |
||
| 282 | * @return bool |
||
| 283 | * @throws EntryPointException |
||
| 284 | */ |
||
| 285 | protected function validateData(){ |
||
| 304 | |||
| 305 | } |