Conditions | 10 |
Paths | 12 |
Total Lines | 53 |
Code Lines | 22 |
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 |
||
122 | protected function get_url_contents( $url = '' ) { |
||
123 | |||
124 | // If $url is not set, use $this->link. |
||
125 | $url = ( '' === $url ) ? $this->link : $url; |
||
126 | |||
127 | // Sanitize the URL. |
||
128 | $url = esc_url_raw( $url ); |
||
129 | |||
130 | // The transient name. |
||
131 | $transient_name = 'kirki_googlefonts_contents_' . md5( $url ); |
||
132 | |||
133 | // Get the transient value. |
||
134 | $data = get_transient( $transient_name ); |
||
135 | |||
136 | // Check for transient, if none, grab remote HTML file. |
||
137 | if ( false === $data ) { |
||
138 | |||
139 | // Get remote HTML file. |
||
140 | $response = wp_remote_get( $url ); |
||
141 | |||
142 | // Check for error. |
||
143 | if ( is_wp_error( $response ) ) { |
||
144 | set_transient( 'kirki_googlefonts_fallback_to_link', 'yes', HOUR_IN_SECONDS ); |
||
145 | return false; |
||
146 | } |
||
147 | |||
148 | if ( ! isset( $response['response'] ) || ! is_array( $response['response'] ) || ! isset( $response['response']['code'] ) || 200 !== $response['response']['code'] ) { |
||
149 | return false; |
||
150 | } |
||
151 | |||
152 | // Parse remote HTML file. |
||
153 | $data = wp_remote_retrieve_body( $response ); |
||
154 | |||
155 | // Check for error. |
||
156 | if ( is_wp_error( $data ) ) { |
||
157 | set_transient( 'kirki_googlefonts_fallback_to_link', 'yes', HOUR_IN_SECONDS ); |
||
158 | return false; |
||
159 | } |
||
160 | |||
161 | // Return false if the data is empty. |
||
162 | if ( ! $data ) { |
||
163 | set_transient( 'kirki_googlefonts_fallback_to_link', 'yes', HOUR_IN_SECONDS ); |
||
164 | return false; |
||
165 | } |
||
166 | |||
167 | // Store remote HTML file in transient, expire after 24 hours. |
||
168 | set_transient( $transient_name, $data, DAY_IN_SECONDS ); |
||
169 | set_transient( 'kirki_googlefonts_fallback_to_link', 'no', DAY_IN_SECONDS ); |
||
170 | } |
||
171 | |||
172 | return $data; |
||
173 | |||
174 | } |
||
175 | } |
||
176 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.