Conditions | 8 |
Paths | 26 |
Total Lines | 93 |
Code Lines | 61 |
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 |
||
115 | public function update_status( Payment $payment ) { |
||
116 | $approval_code = filter_input( INPUT_POST, 'approval_code', FILTER_SANITIZE_STRING ); |
||
117 | |||
118 | $input_hash = filter_input( INPUT_POST, 'response_hash' ); |
||
119 | |||
120 | $hash_values = array( |
||
121 | $this->client->get_secret(), |
||
122 | $approval_code, |
||
123 | filter_input( INPUT_POST, 'chargetotal', FILTER_SANITIZE_STRING ), |
||
124 | filter_input( INPUT_POST, 'currency', FILTER_SANITIZE_STRING ), |
||
125 | filter_input( INPUT_POST, 'txndatetime', FILTER_SANITIZE_STRING ), |
||
126 | $this->client->get_storename(), |
||
127 | ); |
||
128 | |||
129 | if ( filter_has_var( INPUT_POST, 'notification_hash' ) ) { |
||
130 | $input_hash = filter_input( INPUT_POST, 'notification_hash' ); |
||
131 | |||
132 | $hash_values = array( |
||
133 | filter_input( INPUT_POST, 'chargetotal', FILTER_SANITIZE_STRING ), |
||
134 | $this->client->get_secret(), |
||
135 | filter_input( INPUT_POST, 'currency', FILTER_SANITIZE_STRING ), |
||
136 | filter_input( INPUT_POST, 'txndatetime', FILTER_SANITIZE_STRING ), |
||
137 | $this->client->get_storename(), |
||
138 | $approval_code, |
||
139 | ); |
||
140 | } |
||
141 | |||
142 | $hash = Client::compute_hash( $hash_values ); |
||
143 | |||
144 | // Check if the posted hash is equal to the calculated response or notification hash. |
||
145 | if ( 0 === strcasecmp( $input_hash, $hash ) ) { |
||
146 | $response_code = substr( $approval_code, 0, 1 ); |
||
147 | |||
148 | switch ( $response_code ) { |
||
149 | case 'Y': |
||
150 | $status = Statuses::SUCCESS; |
||
151 | |||
152 | break; |
||
153 | case 'N': |
||
154 | $status = Statuses::FAILURE; |
||
155 | |||
156 | $fail_code = filter_input( INPUT_POST, 'fail_rc', FILTER_SANITIZE_NUMBER_INT ); |
||
157 | |||
158 | if ( '5993' === $fail_code ) { |
||
159 | $status = Statuses::CANCELLED; |
||
160 | } |
||
161 | |||
162 | break; |
||
163 | |||
164 | default: |
||
165 | $status = Statuses::OPEN; |
||
166 | |||
167 | break; |
||
168 | } |
||
169 | |||
170 | // Set the status of the payment. |
||
171 | $payment->set_status( $status ); |
||
172 | |||
173 | $labels = array( |
||
174 | 'approval_code' => __( 'Approval code', 'pronamic_ideal' ), |
||
175 | 'oid' => __( 'Order ID', 'pronamic_ideal' ), |
||
176 | 'refnumber' => _x( 'Reference number', 'creditcard', 'pronamic_ideal' ), |
||
177 | 'status' => __( 'Status', 'pronamic_ideal' ), |
||
178 | 'txndate_processed' => __( 'Time of transaction processing', 'pronamic_ideal' ), |
||
179 | 'tdate' => __( 'Identification for transaction', 'pronamic_ideal' ), |
||
180 | 'fail_reason' => __( 'Fail reason', 'pronamic_ideal' ), |
||
181 | 'response_hash' => __( 'Response hash', 'pronamic_ideal' ), |
||
182 | 'processor_response_code' => __( 'Processor response code', 'pronamic_ideal' ), |
||
183 | 'fail_rc' => __( 'Fail code', 'pronamic_ideal' ), |
||
184 | 'terminal_id' => __( 'Terminal ID', 'pronamic_ideal' ), |
||
185 | 'ccbin' => __( 'Creditcard issuing bank', 'pronamic_ideal' ), |
||
186 | 'cccountry' => __( 'Creditcard country', 'pronamic_ideal' ), |
||
187 | 'ccbrand' => __( 'Creditcard brand', 'pronamic_ideal' ), |
||
188 | ); |
||
189 | |||
190 | $note = ''; |
||
191 | |||
192 | $note .= '<p>'; |
||
193 | $note .= __( 'EMS e-Commerce transaction data in response message:', 'pronamic_ideal' ); |
||
194 | $note .= '</p>'; |
||
195 | |||
196 | $note .= '<dl>'; |
||
197 | |||
198 | foreach ( $labels as $key => $label ) { |
||
199 | if ( filter_has_var( INPUT_POST, $key ) ) { |
||
200 | $note .= sprintf( '<dt>%s</dt>', esc_html( $label ) ); |
||
201 | $note .= sprintf( '<dd>%s</dd>', esc_html( filter_input( INPUT_POST, $key, FILTER_SANITIZE_STRING ) ) ); |
||
202 | } |
||
203 | } |
||
204 | |||
205 | $note .= '</dl>'; |
||
206 | |||
207 | $payment->add_note( $note ); |
||
208 | } |
||
211 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: