Completed
Push — output_parsers_refactor ( 710907...9ffc6c )
by Alessandro
02:42
created

JSONLogParser::handleLogItem()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6.0359

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 18
cts 20
cp 0.9
rs 8.7624
cc 6
eloc 17
nc 6
nop 2
crap 6.0359
1
<?php
2
3
namespace Paraunit\Parser;
4
5
use Paraunit\Exception\JSONLogNotFoundException;
6
use Paraunit\Process\ProcessResultInterface;
7
8
/**
9
 * Class JSONLogParser
10
 * @package Paraunit\Parser
11
 */
12
class JSONLogParser implements ProcessOutputParserChainElementInterface
13
{
14
    /** @var  JSONLogFetcher */
15
    private $logLocator;
16
17
    /**
18
     * JSONLogParser constructor.
19
     * @param JSONLogFetcher $logLocator
20
     */
21 10
    public function __construct(JSONLogFetcher $logLocator)
22
    {
23 10
        $this->logLocator = $logLocator;
24 10
    }
25
26 10
    public function parseAndContinue(ProcessResultInterface $process)
27
    {
28
        try {
29 10
            $logs = json_decode($this->logLocator->fetch($process));
0 ignored issues
show
Documentation introduced by
$process is of type object<Paraunit\Process\ProcessResultInterface>, but the function expects a object<Paraunit\Process\ParaunitProcessInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
30 10
        } catch (JSONLogNotFoundException $exception) {
31 1
            return true;
32
        }
33
34 9
        foreach ($logs as $singleLog) {
35 9
            $this->handleLogItem($process, $singleLog);
36 9
        }
37
38 9
        return false;
39
    }
40
41
    /**
42
     * @param ProcessResultInterface $process
43
     * @param \stdClass $logItem
44
     */
45 9
    private function handleLogItem(ProcessResultInterface $process, \stdClass $logItem)
46
    {
47 9
        if ($logItem->event == 'test') {
48 9
            switch ($logItem->status) {
49 9
                case 'pass':
50 9
                    $process->addTestResult('.');
51 9
                    break;
52 6
                case 'fail':
53 1
                    $process->addTestResult('F');
54 1
                    break;
55 6
                case 'error':
56 5
                    $process->addTestResult($this->checkErrorMessage($logItem));
57 5
                    break;
58 1
                case 'warning':
59 1
                    $process->addTestResult('W');
60 1
                    break;
61
                default:
62
                    var_dump($logItem->status);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($logItem->status); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
63 9
            }
64 9
        }
65 9
    }
66
67 5
    private function checkErrorMessage(\stdClass $logItem)
68
    {
69 5
        switch (true) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match('/^Incomplete... /', $logItem->message) of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match('/^Skipped Te... /', $logItem->message) of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match('/^Risky Test: /', $logItem->message) of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
70 5
            case preg_match('/^Incomplete Test: /', $logItem->message):
71 1
                return 'I';
72 4
            case preg_match('/^Skipped Test: /', $logItem->message):
73 1
                return 'S';
74 3
            case preg_match('/^Risky Test: /', $logItem->message):
75 1
                return 'R';
76 2
            default:
77 2
                return 'E';
78 2
        }
79
    }
80
}
81