| Conditions | 9 |
| Paths | 10 |
| Total Lines | 72 |
| Code Lines | 26 |
| 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 |
||
| 107 | public static function getLoginRedirectUrl( $redirect_to ) |
||
| 108 | { |
||
| 109 | |||
| 110 | $subway_redirect_type = get_option('subway_redirect_type'); |
||
| 111 | |||
| 112 | // Redirect the user to default behaviour. |
||
| 113 | // If there are no redirect type option saved. |
||
| 114 | if (empty($subway_redirect_type) ) { |
||
| 115 | |||
| 116 | return $redirect_to; |
||
| 117 | |||
| 118 | } |
||
| 119 | |||
| 120 | if ('default' === $subway_redirect_type ) { |
||
| 121 | return $redirect_to; |
||
| 122 | } |
||
| 123 | |||
| 124 | if ('page' === $subway_redirect_type ) { |
||
| 125 | |||
| 126 | // Get the page url of the selected page. |
||
| 127 | // If the admin selected 'Custom Page' in the redirect type settings. |
||
| 128 | $selected_redirect_page = intval(get_option('subway_redirect_page_id')); |
||
| 129 | |||
| 130 | // Redirect to default WordPress behaviour. |
||
| 131 | // If the user did not select page. |
||
| 132 | if (empty($selected_redirect_page) ) { |
||
| 133 | |||
| 134 | return $redirect_to; |
||
| 135 | } |
||
| 136 | |||
| 137 | // Otherwise, get the permalink of the saved page |
||
| 138 | // and let the user go into that page. |
||
| 139 | return get_permalink($selected_redirect_page); |
||
| 140 | |||
| 141 | } elseif ('custom_url' === $subway_redirect_type ) { |
||
| 142 | |||
| 143 | // Get the custom url saved in the redirect type settings. |
||
| 144 | $entered_custom_url = get_option('subway_redirect_custom_url'); |
||
| 145 | |||
| 146 | // Redirect to default WordPress behaviour |
||
| 147 | // if the user did enter a custom url. |
||
| 148 | if (empty($entered_custom_url) ) { |
||
| 149 | |||
| 150 | return $redirect_to; |
||
| 151 | |||
| 152 | } |
||
| 153 | |||
| 154 | // Otherwise, get the custom url saved |
||
| 155 | // and let the user go into that page. |
||
| 156 | $current_user = wp_get_current_user(); |
||
| 157 | |||
| 158 | if (! empty($current_user->ID) ) { |
||
| 159 | $entered_custom_url = str_replace( |
||
| 160 | '%user_id%', $current_user->ID, |
||
| 161 | $entered_custom_url |
||
| 162 | ); |
||
| 163 | } |
||
| 164 | |||
| 165 | if (! empty($current_user->user_login) ) { |
||
| 166 | $entered_custom_url = str_replace( |
||
| 167 | '%user_name%', $current_user->user_login, |
||
| 168 | $entered_custom_url |
||
| 169 | ); |
||
| 170 | } |
||
| 171 | |||
| 172 | return $entered_custom_url; |
||
| 173 | |||
| 174 | } |
||
| 175 | |||
| 176 | // Otherwise, quit and redirect the user back to default WordPress behaviour. |
||
| 177 | return $redirect_to; |
||
| 178 | } |
||
| 179 | |||
| 219 |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.