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