Completed
Push — master ( f89a6d...41603f )
by Bjørn
02:37
created

ResponseInterpreter::interpret()   F

Complexity

Conditions 14
Paths 386

Size

Total Lines 57
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 14

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 38
c 3
b 0
f 0
dl 0
loc 57
ccs 39
cts 39
cp 1
rs 3.1083
cc 14
nc 386
nop 2
crap 14

How to fix   Long Method    Complexity   

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
namespace HtaccessCapabilityTester\Testers\Helpers;
4
5
use \HtaccessCapabilityTester\HttpResponse;
6
use \HtaccessCapabilityTester\TestResult;
7
use \HtaccessCapabilityTester\Testers\AbstractTester;
8
9
/**
10
 * Class for interpreting responses using a defined interpretation table.
11
 *
12
 * @package    HtaccessCapabilityTester
13
 * @author     Bjørn Rosell <[email protected]>
14
 * @since      Class available since 0.7
15
 */
16
class ResponseInterpreter
17
{
18
19
    /**
20
     * Parse status string (failure | success | inconclusive) to bool|null.
21
     *
22
     * @param  string  $statusString  (failure | success | inconclusive)
23
     * @return bool|null
24
     */
25 52
    private static function parseStatusString($statusString)
26
    {
27 52
        $status = null;
28 52
        switch ($statusString) {
29 52
            case 'failure':
30 31
                $status = false;
31 31
                break;
32 49
            case 'inconclusive':
33 21
                $status = null;
34 21
                break;
35 48
            case 'success':
36 48
                $status = true;
37 48
                break;
38
        }
39 52
        return $status;
40
    }
41
42
    /**
43
     * Evaluate condition (string examination)
44
     *
45
     * @param  string  $val
46
     * @param  string  $operator  (is-empty | equals | not-equals | begins-with)
47
     * @param  string  $arg1  (only required for some operators)
48
     * @return bool
49
     */
50 45
    private static function evaluateConditionForString($operator, $val, $arg1)
51
    {
52 45
        switch ($operator) {
53 45
            case 'is-empty':
54
                return ($val == '');
55 45
            case 'equals':
56 41
                return ($val == $arg1);
57 22
            case 'not-equals':
58 19
                return ($val != $arg1);
59 12
            case 'begins-with':
60 3
                return (strpos($val, $arg1) === 0);
61
        }
62 9
        return false;
63
    }
64
65
    /**
66
     * Evaluate condition  (hash examination)
67
     *
68
     * @param  array  $val
69
     * @param  string $operator  (is-empty | equals | not-equals | begins-with)
70
     * @param  string $arg1  (only required for some operators)
71
     * @return bool
72
     */
73 15
    private static function evaluateConditionForHash($operator, $val, $arg1, $arg2)
74
    {
75 15
        switch ($operator) {
76 15
            case 'contains-key':
77 2
                return (isset($val[$arg1]));
78 15
            case 'not-contains-key':
79 9
                return (!isset($val[$arg1]));
80 11
            case 'contains-key-value':
81 11
                return (isset($val[$arg1]) && ($val[$arg1] == $arg2));
82 7
            case 'not-contains-key-value':
83 7
                return (!isset($val[$arg1]) || ($val[$arg1] != $arg2));
84
        }
85
        return false;
86
    }
87
88
    /**
89
     * Interpret a response using an interpretation table.
90
     *
91
     * @param HttpResponse    $response
92
     * @param array           $interpretationTable
93
     *
94
     * @return TestResult   If there is no match, the test result will have status = false and
95
     *                      info = "no-match".
96
     */
97 52
    public static function interpret($response, $interpretationTable)
98
    {
99 52
        foreach ($interpretationTable as $i => $entry) {
100
            // ie:
101
            // ['inconclusive', 'body', 'is-empty'],
102
            // ['failure', 'statusCode', 'equals', '500']
103
            // ['success', 'headers', 'contains-key-value', 'X-Response-Header-Test', 'test'],
104
105 52
            $status = self::parseStatusString($entry[0]);
106
107 52
            if (!isset($entry[1])) {
108 12
                return new TestResult($status, '');
109
            }
110
111 52
            $propertyToExamine = $entry[1];
112 52
            $operator = $entry[2];
113 52
            $arg1 = (isset($entry[3]) ? $entry[3] : '');
114 52
            $arg2 = (isset($entry[4]) ? $entry[4] : '');
115
116 52
            $valString = '';
117 52
            $valHash = [];
118 52
            $valType = '';
119 52
            switch ($propertyToExamine) {
120 52
                case 'status-code':
121 31
                    $valString = $response->statusCode;
122 31
                    $valType = 'string';
123 31
                    break;
124 35
                case 'body':
125 25
                    $valString = $response->body;
126 25
                    $valType = 'string';
127 25
                    break;
128 15
                case 'headers':
129 15
                    $valHash = $response->getHeadersHash();
130 15
                    $valType = 'hash';
131 15
                    break;
132
            }
133
134 52
            $reason = $propertyToExamine . ' ' . $operator;
135 52
            if (isset($entry[3])) {
136 52
                $reason .= ' "' . implode('" "', array_slice($entry, 3)) . '"';
137
            }
138 52
            if (($propertyToExamine == 'status-code') && ($operator == 'not-equals')) {
139 19
                $reason .= ' - it was: ' . $valString;
140
            }
141 52
            $result = new TestResult($status, $reason);
142
143 52
            $match = false;
144 52
            if ($valType == 'string') {
145 45
                $match = self::evaluateConditionForString($operator, $valString, $arg1);
146 15
            } elseif ($valType == 'hash') {
147 15
                $match =  self::evaluateConditionForHash($operator, $valHash, $arg1, $arg2);
148
            }
149 52
            if ($match) {
150 46
                return $result;
151
            }
152
        }
153 15
        return new TestResult(null, 'no-match');
154
    }
155
}
156