Completed
Push — update/import-sync-detection ( 0bf98c...8808a0 )
by
unknown
25:48 queued 17:49
created

proxy.php ➔ AtD_http_post()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 4
dl 0
loc 53
rs 8.4032
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
/*
3
 *  This script redirects AtD AJAX requests to the AtD service
4
 */
5
6
/**
7
 * Returns array with headers in $response[0] and body in $response[1]
8
 * Based on a function from Akismet
9
 */
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
64
/*
65
 *  This function is called as an action handler to admin-ajax.php
66
 */
67
function AtD_redirect_call() {
68
	if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
69
		$postText = trim( file_get_contents( 'php://input' ) );
70
	}
71
72
	check_admin_referer( 'proxy_atd' );
73
74
	$url = $_GET['url'];
75
	/**
76
	 * Change the AtD service domain.
77
	 *
78
	 * @module after-the-deadline
79
	 *
80
	 * @since 1.2.3
81
	 *
82
	 * @param string $var The URL for AtD service domain, default is service.afterthedeadline.com.
83
	 */
84
	$service = apply_filters( 'atd_service_domain', 'service.afterthedeadline.com' );
85
86
	$user = wp_get_current_user();
87
88
	$atd_lang = get_user_locale( $user->ID );
89
90
	if ( ! empty( $atd_lang ) ) {
91
		if ( strpos( $atd_lang, 'pt' ) !== false ) {
92
			$service = 'pt.service.afterthedeadline.com';
93
		} elseif ( strpos( $atd_lang, 'de' ) !== false ) {
94
			$service = 'de.service.afterthedeadline.com';
95
		} elseif ( strpos( $atd_lang, 'es' ) !== false ) {
96
			$service = 'es.service.afterthedeadline.com';
97
		} elseif ( strpos( $atd_lang, 'fr' ) !== false ) {
98
			$service = 'fr.service.afterthedeadline.com';
99
		}
100
	}
101
102
	$guess = strcmp( AtD_get_setting( $user->ID, 'AtD_guess_lang' ), 'true' ) == 0 ? 'true' : 'false';
103
104
	$data = AtD_http_post( $postText . "&guess=$guess", defined( 'ATD_HOST' ) ? ATD_HOST : $service, $url, defined( 'ATD_PORT' ) ? ATD_PORT : 80 );
0 ignored issues
show
Bug introduced by
The variable $postText does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
105
106
	header( 'Content-Type: text/xml' );
107
108
	if ( ! empty( $data[1] ) ) {
109
		echo $data[1];
110
	}
111
112
	die();
113
}
114