Passed
Push — master ( bb3ce1...337bb1 )
by Bjørn
02:35
created

ResponseInterpreter::interpretLine()   F

Complexity

Conditions 13
Paths 385

Size

Total Lines 55
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 13

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 55
ccs 38
cts 38
cp 1
rs 3.4708
cc 13
nc 385
nop 2
crap 13

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 71
    private static function parseStatusString($statusString)
26
    {
27 71
        $status = null;
28 71
        switch ($statusString) {
29 71
            case 'failure':
30 46
                $status = false;
31 46
                break;
32 68
            case 'inconclusive':
33 35
                $status = null;
34 35
                break;
35 67
            case 'success':
36 67
                $status = true;
37 67
                break;
38
        }
39 71
        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 63
    private static function evaluateConditionForString($operator, $val, $arg1)
51
    {
52 63
        switch ($operator) {
53 63
            case 'is-empty':
54
                return ($val == '');
55 63
            case 'equals':
56 59
                return ($val == $arg1);
57 36
            case 'not-equals':
58 33
                return ($val != $arg1);
59 26
            case 'begins-with':
60 3
                return (strpos($val, $arg1) === 0);
61
        }
62 23
        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 28
    private static function evaluateConditionForHash($operator, $val, $arg1, $arg2)
74
    {
75 28
        switch ($operator) {
76 28
            case 'contains-key':
77 4
                return (isset($val[$arg1]));
78 28
            case 'not-contains-key':
79 21
                return (!isset($val[$arg1]));
80 22
            case 'contains-key-value':
81 22
                return (isset($val[$arg1]) && ($val[$arg1] == $arg2));
82 15
            case 'not-contains-key-value':
83 15
                return (!isset($val[$arg1]) || ($val[$arg1] != $arg2));
84
        }
85
        return false;
86
    }
87
88
    /**
89
     * Interpret line.
90
     *
91
     * @param HttpResponse    $response
92
     * @param array           $line
93
     *
94
     * @return  TestResult|null  If the condition matches, a TestResult is returned, otherwise null
95
     */
96 71
    private static function interpretLine($response, $line)
97
    {
98
        // ie:
99
        // ['inconclusive', 'body', 'is-empty'],
100
        // ['failure', 'statusCode', 'equals', '500']
101
        // ['success', 'headers', 'contains-key-value', 'X-Response-Header-Test', 'test'],
102
103 71
        $status = self::parseStatusString($line[0]);
104
105 71
        if (!isset($line[1])) {
106 24
            return new TestResult($status, '');
107
        }
108
109 71
        $propertyToExamine = $line[1];
110 71
        $operator = $line[2];
111 71
        $arg1 = (isset($line[3]) ? $line[3] : '');
112 71
        $arg2 = (isset($line[4]) ? $line[4] : '');
113
114 71
        $valString = '';
115 71
        $valHash = [];
116 71
        $valType = '';
117 71
        switch ($propertyToExamine) {
118 71
            case 'status-code':
119 45
                $valString = $response->statusCode;
120 45
                $valType = 'string';
121 45
                break;
122 54
            case 'body':
123 43
                $valString = $response->body;
124 43
                $valType = 'string';
125 43
                break;
126 28
            case 'headers':
127 28
                $valHash = $response->getHeadersHash();
128 28
                $valType = 'hash';
129 28
                break;
130
        }
131
132 71
        $reason = $propertyToExamine . ' ' . $operator;
133 71
        if (isset($line[3])) {
134 71
            $reason .= ' "' . implode('" "', array_slice($line, 3)) . '"';
135
        }
136 71
        if (($propertyToExamine == 'status-code') && ($operator == 'not-equals')) {
137 33
            $reason .= ' - it was: ' . $valString;
138
        }
139 71
        $result = new TestResult($status, $reason);
140
141 71
        $match = false;
142 71
        if ($valType == 'string') {
143 63
            $match = self::evaluateConditionForString($operator, $valString, $arg1);
144 28
        } elseif ($valType == 'hash') {
145 28
            $match =  self::evaluateConditionForHash($operator, $valHash, $arg1, $arg2);
146
        }
147 71
        if ($match) {
148 65
            return $result;
149
        }
150 47
        return null;
151
    }
152
153
    /**
154
     * Interpret a response using an interpretation table.
155
     *
156
     * @param HttpResponse    $response
157
     * @param array           $interpretationTable
158
     *
159
     * @return TestResult   If there is no match, the test result will have status = false and
160
     *                      info = "no-match".
161
     */
162 71
    public static function interpret($response, $interpretationTable)
163
    {
164 71
        foreach ($interpretationTable as $i => $line) {
165 71
            $testResult = self::interpretLine($response, $line);
166 71
            if (!is_null($testResult)) {
167 70
                return $testResult;
168
            }
169
        }
170 29
        return new TestResult(null, 'no-match');
171
    }
172
}
173