Conditions | 14 |
Paths | 161 |
Total Lines | 88 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
56 | public function preparePage() |
||
57 | { |
||
58 | $this->P = new \HaaseIT\HCSF\CorePage($this->serviceManager); |
||
59 | $this->P->cb_pagetype = 'content'; |
||
60 | |||
61 | if (!$this->helperCustomer->getUserData()) { |
||
62 | $this->P->oPayload->cl_html = $this->textcats->T('denied_notloggedin'); |
||
63 | } else { |
||
64 | $this->P->cb_customcontenttemplate = 'customer/customerhome'; |
||
65 | |||
66 | $aPData['display_logingreeting'] = false; |
||
|
|||
67 | if (filter_input(INPUT_GET, 'login') !== null) { |
||
68 | $aPData['display_logingreeting'] = true; |
||
69 | } |
||
70 | if (filter_input(INPUT_GET, 'editprofile') !== null) { |
||
71 | $aErr = []; |
||
72 | |||
73 | if (filter_input(INPUT_POST, 'doEdit') === 'yes') { |
||
74 | $sql = 'SELECT '.DB_ADDRESSFIELDS.' FROM customer WHERE cust_id != :id AND cust_email = :email'; |
||
75 | |||
76 | $sEmail = filter_var(trim(Tools::getFormfield('email')), FILTER_SANITIZE_EMAIL); |
||
77 | |||
78 | $hResult = $this->db->prepare($sql); |
||
79 | $hResult->bindValue(':id', $_SESSION['user']['cust_id'], \PDO::PARAM_INT); |
||
80 | $hResult->bindValue(':email', $sEmail, \PDO::PARAM_STR); |
||
81 | $hResult->execute(); |
||
82 | $iRows = $hResult->rowCount(); |
||
83 | if ($iRows == 1) { |
||
84 | $aErr['adrform_error_emailalreadytaken'] = true; |
||
85 | } |
||
86 | $aErr = $this->helperCustomer->validateCustomerForm($this->config->getLang(), $aErr, true); |
||
87 | |||
88 | if (empty($aErr)) { |
||
89 | if ($this->config->getCustomer('allow_edituserprofile')) { |
||
90 | $aData = [ |
||
91 | //'cust_email' => $sEmail, // disabled until renwewd email verification implemented |
||
92 | 'cust_corp' => filter_var(trim(Tools::getFormfield('corpname')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW), |
||
93 | 'cust_name' => filter_var(trim(Tools::getFormfield('name')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW), |
||
94 | 'cust_street' => filter_var(trim(Tools::getFormfield('street')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW), |
||
95 | 'cust_zip' => filter_var(trim(Tools::getFormfield('zip')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW), |
||
96 | 'cust_town' => filter_var(trim(Tools::getFormfield('town')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW), |
||
97 | 'cust_phone' => filter_var(trim(Tools::getFormfield('phone')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW), |
||
98 | 'cust_cellphone' => filter_var(trim(Tools::getFormfield('cellphone')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW), |
||
99 | 'cust_fax' => filter_var(trim(Tools::getFormfield('fax')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW), |
||
100 | 'cust_country' => filter_var(trim(Tools::getFormfield('country')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW), |
||
101 | ]; |
||
102 | } |
||
103 | $postpwd = filter_input(INPUT_POST, 'pwd'); |
||
104 | if (!empty($postpwd)) { |
||
105 | $aData['cust_password'] = password_hash($postpwd, PASSWORD_DEFAULT); |
||
106 | $aPData['infopasswordchanged'] = true; |
||
107 | } |
||
108 | $aData['cust_id'] = $_SESSION['user']['cust_id']; |
||
109 | |||
110 | if (count($aData) > 1) { |
||
111 | $sql = \HaaseIT\Toolbox\DBTools::buildPSUpdateQuery($aData, 'customer', 'cust_id'); |
||
112 | $hResult = $this->db->prepare($sql); |
||
113 | foreach ($aData as $sKey => $sValue) { |
||
114 | $hResult->bindValue(':'.$sKey, $sValue); |
||
115 | } |
||
116 | $hResult->execute(); |
||
117 | $aPData['infochangessaved'] = true; |
||
118 | } else { |
||
119 | $aPData['infonothingchanged'] = true; |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | $this->P->cb_customdata['customerform'] = $this->helperCustomer->buildCustomerForm( |
||
124 | $this->config->getLang(), |
||
125 | 'editprofile', |
||
126 | $aErr |
||
127 | ); |
||
128 | //if (HelperConfig::$customer["allow_edituserprofile"]) $P["lang"]["cl_html"] .= '<br>'.$this->textcats->T("userprofile_infoeditemail"); // Future implementation |
||
129 | } else { |
||
130 | $this->P->cb_customdata['customerform'] = $this->helperCustomer->buildCustomerForm( |
||
131 | $this->config->getLang(), |
||
132 | 'userhome' |
||
133 | ); |
||
134 | } |
||
135 | $aPData['showprofilelinks'] = false; |
||
136 | if (filter_input(INPUT_GET, 'editprofile') === null) { |
||
137 | $aPData['showprofilelinks'] = true; |
||
138 | } |
||
139 | if (isset($aPData) && count($aPData)) { |
||
140 | $this->P->cb_customdata['userhome'] = $aPData; |
||
141 | } |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.