Conditions | 15 |
Paths | 3073 |
Total Lines | 82 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 1 |
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 |
||
215 | public function init() { |
||
216 | |||
217 | // Get PHP Console extension password. |
||
218 | $password = $this->options['password']; |
||
219 | |||
220 | if ( ! $password ) { |
||
221 | // Display admin notice and abort if no password has been set. |
||
222 | add_action( 'admin_notices', array( $this, 'password_notice' ) ); |
||
223 | return; |
||
224 | } |
||
225 | |||
226 | // Selectively remove slashes added by WordPress as expected by PhpConsole. |
||
227 | if ( array_key_exists( PhpConsole\Connector::POST_VAR_NAME, $_POST ) ) { |
||
228 | $_POST[ PhpConsole\Connector::POST_VAR_NAME ] = stripslashes_deep( $_POST[ PhpConsole\Connector::POST_VAR_NAME ] ); |
||
229 | } |
||
230 | |||
231 | // Get PHP Console instance if wasn't set yet. |
||
232 | if ( ! $this->connector instanceof PhpConsole\Connector ) { |
||
233 | $this->connector = PhpConsole\Connector::getInstance(); |
||
234 | } |
||
235 | |||
236 | // Set PHP Console password. |
||
237 | try { |
||
238 | $this->connector->setPassword( $password ); |
||
239 | } catch ( \Exception $e ) { |
||
240 | $this->print_notice_exception( $e ); |
||
241 | } |
||
242 | |||
243 | // Get PHP Console handler instance. |
||
244 | $handler = PhpConsole\Handler::getInstance(); |
||
245 | |||
246 | if ( true !== PhpConsole\Handler::getInstance()->isStarted() ) { |
||
247 | try { |
||
248 | $handler->start(); |
||
249 | } catch( \Exception $e ) { |
||
250 | $this->print_notice_exception( $e ); |
||
251 | } |
||
252 | } |
||
253 | |||
254 | // Enable SSL-only mode. |
||
255 | if ( true === $this->options['ssl'] ) { |
||
256 | $this->connector->enableSslOnlyMode(); |
||
257 | } |
||
258 | |||
259 | // Restrict IP addresses. |
||
260 | $allowedIpMasks = ! empty( $this->options['ip'] ) ? explode( ',', $this->options['ip'] ) : ''; |
||
261 | |||
262 | if ( is_array( $allowedIpMasks ) && count( $allowedIpMasks ) > 0 ) { |
||
263 | $this->connector->setAllowedIpMasks( $allowedIpMasks ); |
||
264 | } |
||
265 | |||
266 | $evalProvider = $this->connector->getEvalDispatcher()->getEvalProvider(); |
||
267 | |||
268 | try { |
||
269 | $evalProvider->addSharedVar( 'uri', $_SERVER['REQUEST_URI'] ); |
||
270 | } catch ( \Exception $e ) { |
||
271 | $this->print_notice_exception( $e ); |
||
272 | } |
||
273 | |||
274 | try { |
||
275 | $evalProvider->addSharedVarReference( 'post', $_POST ); |
||
276 | } catch ( \Exception $e ) { |
||
277 | $this->print_notice_exception( $e ); |
||
278 | } |
||
279 | |||
280 | $openBaseDirs = array( ABSPATH, get_template_directory() ); |
||
281 | |||
282 | try { |
||
283 | $evalProvider->addSharedVarReference( 'dirs', $openBaseDirs ); |
||
284 | } catch ( \Exception $e ) { |
||
285 | $this->print_notice_exception( $e ); |
||
286 | } |
||
287 | |||
288 | $evalProvider->setOpenBaseDirs( $openBaseDirs ); |
||
289 | |||
290 | try { |
||
291 | $this->connector->startEvalRequestsListener(); |
||
292 | } catch ( \Exception $e ) { |
||
293 | $this->print_notice_exception( $e ); |
||
294 | } |
||
295 | |||
296 | } |
||
297 | |||
345 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.