Complex classes like HCSF 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 HCSF, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class HCSF |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var ServiceManager |
||
| 12 | */ |
||
| 13 | protected $serviceManager; |
||
| 14 | |||
| 15 | public function __construct() |
||
| 16 | { |
||
| 17 | define('HCSF_BASEDIR', dirname(__DIR__).DIRECTORY_SEPARATOR); |
||
| 18 | define('DB_ADDRESSFIELDS', 'cust_id, cust_no, cust_email, cust_corp, cust_name, cust_street, cust_zip, cust_town, cust_phone, cust_cellphone, cust_fax, cust_country, cust_group, cust_active, cust_emailverified, cust_tosaccepted, cust_cancellationdisclaimeraccepted'); |
||
| 19 | define('DB_ITEMFIELDS', 'itm_no, itm_name, itm_price, itm_vatid, itm_rg, itm_img, itm_group, itm_data, itm_weight, itml_name_override, itml_text1, itml_text2, itm_index'); |
||
| 20 | define('DB_ITEMGROUPFIELDS', 'itmg_no, itmg_name, itmg_img, itmgt_shorttext, itmgt_details'); |
||
| 21 | define('FILE_PAYPALLOG', 'ipnlog.txt'); |
||
| 22 | |||
| 23 | // set scale for bcmath |
||
| 24 | bcscale(6); |
||
| 25 | } |
||
| 26 | |||
| 27 | public function init() |
||
| 28 | { |
||
| 29 | $this->serviceManager = new ServiceManager(); |
||
| 30 | |||
| 31 | $this->setupRequest(); |
||
| 32 | |||
| 33 | HelperConfig::init(); |
||
| 34 | if (HelperConfig::$core['debug']) { |
||
| 35 | \HaaseIT\Toolbox\Tools::$bEnableDebug = true; |
||
| 36 | } |
||
| 37 | |||
| 38 | $this->setupSession(); |
||
| 39 | |||
| 40 | date_default_timezone_set(HelperConfig::$core['defaulttimezone']); |
||
| 41 | |||
| 42 | $this->setupHardcodedTextcats(); |
||
| 43 | |||
| 44 | $this->serviceManager->setFactory('db', function () { |
||
| 45 | return null; |
||
| 46 | }); |
||
| 47 | |||
| 48 | if (!HelperConfig::$core['maintenancemode']) { |
||
| 49 | $this->setupDB(); |
||
| 50 | $this->setupTextcats(); |
||
| 51 | HelperConfig::loadNavigation($this->serviceManager); |
||
| 52 | } |
||
| 53 | |||
| 54 | $this->setupTwig(); |
||
| 55 | |||
| 56 | if (HelperConfig::$core['enable_module_shop']) { |
||
| 57 | $this->serviceManager->setFactory('oItem', function (ServiceManager $serviceManager) { |
||
| 58 | return new \HaaseIT\HCSF\Shop\Items($serviceManager); |
||
| 59 | }); |
||
| 60 | } |
||
| 61 | |||
| 62 | $router = new \HaaseIT\HCSF\Router($this->serviceManager); |
||
| 63 | return $router->getPage(); |
||
| 64 | } |
||
| 65 | |||
| 66 | protected function setupRequest() |
||
|
|
|||
| 67 | { |
||
| 68 | // PSR-7 Stuff |
||
| 69 | // Init request object |
||
| 70 | $this->serviceManager->setFactory('request', function () { |
||
| 71 | $request = \Zend\Diactoros\ServerRequestFactory::fromGlobals(); |
||
| 72 | |||
| 73 | // cleanup request |
||
| 74 | $requesturi = urldecode($request->getRequestTarget()); |
||
| 75 | $parsedrequesturi = substr($requesturi, strlen(dirname($_SERVER['PHP_SELF']))); |
||
| 76 | if (substr($parsedrequesturi, 1, 1) !== '/') { |
||
| 77 | $parsedrequesturi = '/'.$parsedrequesturi; |
||
| 78 | } |
||
| 79 | return $request->withRequestTarget($parsedrequesturi); |
||
| 80 | }); |
||
| 81 | } |
||
| 82 | |||
| 83 | protected function setupSession() |
||
| 84 | { |
||
| 85 | if (isset($_COOKIE['acceptscookies']) && HelperConfig::$core['enable_module_customer'] && $_COOKIE['acceptscookies'] === 'yes') { |
||
| 86 | // Session handling |
||
| 87 | // session.use_trans_sid wenn nötig aktivieren |
||
| 88 | session_name('sid'); |
||
| 89 | // Session wenn nötig starten |
||
| 90 | if (session_id() == '') { |
||
| 91 | session_start(); |
||
| 92 | } |
||
| 93 | |||
| 94 | // check if the stored ip and ua equals the clients, if not, reset. if not set at all, reset |
||
| 95 | if (!empty($_SESSION['hijackprevention'])) { |
||
| 96 | if ( |
||
| 97 | $_SESSION['hijackprevention']['remote_addr'] != $_SERVER['REMOTE_ADDR'] |
||
| 98 | || |
||
| 99 | $_SESSION['hijackprevention']['user_agent'] != $_SERVER['HTTP_USER_AGENT'] |
||
| 100 | ) { |
||
| 101 | \session_regenerate_id(); |
||
| 102 | \session_unset(); |
||
| 103 | } |
||
| 104 | } else { |
||
| 105 | \session_regenerate_id(); |
||
| 106 | \session_unset(); |
||
| 107 | $_SESSION['hijackprevention']['remote_addr'] = $_SERVER['REMOTE_ADDR']; |
||
| 108 | $_SESSION['hijackprevention']['user_agent'] = $_SERVER['HTTP_USER_AGENT']; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | protected function setupHardcodedTextcats() |
||
| 127 | |||
| 128 | protected function setupDB() |
||
| 155 | |||
| 156 | protected function setupTextcats() |
||
| 172 | |||
| 173 | protected function setupTwig() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return mixed |
||
| 213 | */ |
||
| 214 | public function getServiceManager() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param Page $P |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | public function generatePage(Page $P) |
||
| 330 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: