Conditions | 6 |
Paths | 6 |
Total Lines | 53 |
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 |
||
10 | function AtD_http_post( $request, $host, $path, $port = 80 ) { |
||
11 | $http_args = array( |
||
12 | 'body' => $request, |
||
13 | 'headers' => array( |
||
14 | 'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' ), |
||
15 | 'Host' => $host, |
||
16 | 'User-Agent' => 'AtD/0.1', |
||
17 | ), |
||
18 | 'httpversion' => '1.0', |
||
19 | /** |
||
20 | * Change the timeout time for AtD post. |
||
21 | * |
||
22 | * @module after-the-deadline |
||
23 | * |
||
24 | * @since 1.2.3 |
||
25 | * |
||
26 | * @param int $var Timeout time in seconds, default 15. |
||
27 | */ |
||
28 | 'timeout' => apply_filters( 'atd_http_post_timeout', 15 ), |
||
29 | ); |
||
30 | |||
31 | // Handle non-standard ports being passed in. |
||
32 | if ( ( 80 !== $port ) && is_numeric( $port ) && ( intval( $port ) > 0 ) ) { |
||
33 | $host .= ':' . intval( $port ); |
||
34 | } |
||
35 | // Strip any / off the begining so we can add it back and protect against SSRF |
||
36 | $path = ltrim( $path, '/' ); |
||
37 | $AtD_url = set_url_scheme( "http://{$host}/{$path}" ); |
||
38 | $response = wp_remote_post( $AtD_url, $http_args ); |
||
39 | $code = (int) wp_remote_retrieve_response_code( $response ); |
||
40 | |||
41 | if ( is_wp_error( $response ) ) { |
||
42 | /** |
||
43 | * Fires when there is a post error to AtD. |
||
44 | * |
||
45 | * @module after-the-deadline |
||
46 | * |
||
47 | * @since 1.2.3 |
||
48 | * |
||
49 | * @param int|string http-error The error that AtD runs into. |
||
50 | */ |
||
51 | do_action( 'atd_http_post_error', 'http-error' ); |
||
52 | return array(); |
||
53 | } elseif ( 200 != $code ) { |
||
54 | /** This action is documented in modules/after-the-deadline/proxy.php */ |
||
55 | do_action( 'atd_http_post_error', $code ); |
||
56 | } |
||
57 | |||
58 | return array( |
||
59 | wp_remote_retrieve_headers( $response ), |
||
60 | wp_remote_retrieve_body( $response ), |
||
61 | ); |
||
62 | } |
||
63 | |||
114 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: