Completed
Push — develop ( ce465f...dc3813 )
by David
02:29 queued 11s
created

Wordlift_Autocomplete_Adapter::wl_autocomplete()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 8
nop 0
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
1
<?php
2
/**
3
 * Wordlift_Autocomplete_Adapter class.
4
 *
5
 * The {@link Wordlift_Autocomplete_Adapter} class create requests to external API's.
6
 *
7
 * @link       https://wordlift.io
8
 *
9
 * @package    Wordlift
10
 * @since      3.15.0
11
 */
12
13
use Wordlift\Autocomplete\Autocomplete_Service;
14
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
/**
20
 * Create autocomplete request to external API's and return the result if there is such.
21
 *
22
 * @since 3.15.0
23
 */
24
class Wordlift_Autocomplete_Adapter {
25
26
	/**
27
	 * The {@link Autocomplete_Service} instance.
28
	 *
29
	 * @since  3.15.0
30
	 * @access private
31
	 * @var Autocomplete_Service $configuration_service The {@link Autocomplete_Service} instance.
32
	 */
33
	private $autocomplete_service;
34
35
36
	/**
37
	 * Wordlift_Autocomplete_Adapter constructor.
38
	 *
39
	 * @param Autocomplete_Service $autocomplete_service The {@link Autocomplete_Service} instance.
40
	 *
41
	 * @since 3.14.2
42
	 *
43
	 */
44
	public function __construct( $autocomplete_service ) {
45
		$this->autocomplete_service = $autocomplete_service;
46
	}
47
48
	/**
49
	 * Handle the autocomplete ajax request.
50
	 *
51
	 * @since 3.15.0
52
	 */
53
	public function wl_autocomplete() {
54
55
		check_ajax_referer( 'wl_autocomplete' );
56
57
		// Return error if the query param is empty.
58
		if ( ! empty( $_REQUEST['query'] ) ) { // Input var okay.
59
			$query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ); // Input var okay.
60
		} else {
61
			wp_send_json_error( array(
62
				'message' => __( 'The query param is empty.', 'wordlift' ),
63
			) );
64
		}
65
66
		// Get the exclude parameter.
67
		$exclude = ! empty( $_REQUEST['exclude'] )
68
			? sanitize_text_field( wp_unslash( $_REQUEST['exclude'] ) ) : '';
69
70
		$scope = ! empty( $_REQUEST['scope'] )
71
			? sanitize_text_field( wp_unslash( $_REQUEST['scope'] ) ) : WL_AUTOCOMPLETE_SCOPE;
72
73
		// Make request.
74
		$results = $this->autocomplete_service->query( $query, $scope, $exclude );
0 ignored issues
show
Bug introduced by
The variable $query 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...
75
76
		// Clear any buffer.
77
		ob_clean();
78
79
		wp_send_json_success( $results );
80
81
	}
82
}
83