Completed
Push — master ( 938ad9...a54f1f )
by Andrii
04:46
created

QueryBuilder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 83.33%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 11
lcom 2
cbo 2
dl 0
loc 65
ccs 20
cts 24
cp 0.8333
rs 10
c 3
b 1
f 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A buildAuth() 0 10 3
A buildMethod() 0 15 2
A buildUri() 0 4 1
A buildHeaders() 0 4 1
A buildProtocolVersion() 0 4 1
A buildQueryParams() 0 4 1
A buildFormParams() 0 4 1
A buildBody() 0 4 1
1
<?php
2
/**
3
 * Tools to use API as ActiveRecord for Yii2
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart
6
 * @package   yii2-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart\rest;
12
13
use hiqdev\hiart\Query;
14
15
class QueryBuilder extends \hiqdev\hiart\AbstractQueryBuilder
16
{
17
    protected $authHeaders = [];
18
19
    /**
20
     * This function is for you to provide your authentication.
21
     * @param Query $query
22
     */
23 2
    public function buildAuth(Query $query)
24
    {
25 2
        $auth = $this->db->getAuth();
26 2
        if (isset($auth['headerToken'])) {
27
            $authHeaders['Authorization'] = 'token ' . $auth['headerToken'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$authHeaders was never initialized. Although not strictly required by PHP, it is generally a good practice to add $authHeaders = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key 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.

Loading history...
28
        }
29 2
        if (isset($auth['headerBearer'])) {
30
            $authHeaders['Authorization'] = 'Bearer ' . $auth['headerBearer'];
0 ignored issues
show
Bug introduced by
The variable $authHeaders 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...
31
        }
32 2
    }
33
34 2
    public function buildMethod(Query $query)
35
    {
36
        static $defaultMethods = [
37
            'get'       => 'GET',
38
            'put'       => 'PUT',
39
            'head'      => 'HEAD',
40
            'post'      => 'GET',
41
            'search'    => 'GET',
42
            'insert'    => 'POST',
43
            'update'    => 'PUT',
44
            'delete'    => 'DELETE',
45 2
        ];
46
47 2
        return isset($defaultMethods[$query->action]) ? $defaultMethods[$query->action] : 'POST';
48
    }
49
50 2
    public function buildUri(Query $query)
51
    {
52 2
        return $query->from;
53
    }
54
55 2
    public function buildHeaders(Query $query)
56
    {
57 2
        return $this->authHeaders;
58
    }
59
60 2
    public function buildProtocolVersion(Query $query)
61
    {
62 2
        return null;
63
    }
64
65 2
    public function buildQueryParams(Query $query)
66
    {
67 2
        return [];
68
    }
69
70 2
    public function buildFormParams(Query $query)
71
    {
72 2
        return [];
73
    }
74
75 2
    public function buildBody(Query $query)
76
    {
77 2
        return null;
78
    }
79
}
80