Completed
Push — output_parsers_refactor ( 9ffc6c...755930 )
by Alessandro
09:57
created

AbstractParser::parsingFoundResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Paraunit\Parser;
4
5
use Paraunit\Printer\OutputContainer;
6
use Paraunit\Process\ProcessResultInterface;
7
8
/**
9
 * Class AbstractParser
10
 * @package Paraunit\Parser
11
 */
12
abstract class AbstractParser implements JSONParserChainElementInterface, OutputContainerBearerInterface
13
{
14
    /** @var  OutputContainer */
15
    protected $outputContainer;
16
17
    /** @var  string */
18
    protected $singleResultMarker;
19
20 37
    /** @var  string */
21
    protected $title;
22 37
23 37
    /** @var  string */
24 37
    protected $status;
25
26 37
    /** @var  string */
27 37
    protected $messageStartsWith;
28
29
    /**
30
     * AbstractParser constructor.
31
     *
32 26
     * @param OutputContainer $outputContainer
33
     * @param string $singleResultMarker The output of the single test result (.FERW etc)
34 26
     * @param string $status The status that the parser should catch
35
     * @param string | null $messageStartsWith The start of the message that the parser should look for
36
     */
37
    public function __construct(OutputContainer $outputContainer, $singleResultMarker, $status, $messageStartsWith = null)
38
    {
39
        $this->outputContainer = $outputContainer;
40
        $this->singleResultMarker = $singleResultMarker;
41 24
        $this->status = $status;
42
        $this->messageStartsWith = $messageStartsWith;
43 24
    }
44
45 24
    /**
46 10
     * {@inheritdoc}
47
     */
48 10 View Code Duplication
    public function parsingFoundResult(ProcessResultInterface $process, \stdClass $log)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50 10
        if ($log->status == $this->status) {
51 10
            $process->addTestResult($this->singleResultMarker);
52
            $this->outputContainer->addToOutputBuffer($process, $log->message);
53 10
54
            return false;
55
        }
56 18
57
        return true;
58
    }
59
60
    /**
61
     * @return OutputContainer
62
     */
63 18
    public function getOutputContainer()
64
    {
65 18
        return $this->outputContainer;
66 4
    }
67
68 4
    /**
69
     * @param ProcessResultInterface $process
70
     * @return bool
71 15
     */
72
    protected function storeParsedBlocks(ProcessResultInterface $process)
73
    {
74 37
        $parsedBlob = $this->parse($process);
75
76 37
        if (isset($parsedBlob[1])) {
77 37
            $parsedBlocks = preg_split('/^\d+\) /m', $parsedBlob[1]);
78
            // il primo è sempre vuoto a causa dello split
79 37
            unset($parsedBlocks[0]);
80
81
            $this->outputContainer->addToOutputBuffer($parsedBlocks);
0 ignored issues
show
Bug introduced by
The call to addToOutputBuffer() misses a required argument $message.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$parsedBlocks is of type array, but the function expects a object<Paraunit\Process\ProcessResultInterface>.

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...
82
            $this->outputContainer->addFileName($process->getFilename());
0 ignored issues
show
Bug introduced by
The method addFileName() does not seem to exist on object<Paraunit\Printer\OutputContainer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
84
            return true;
85
        }
86 37
87
        return false;
88 37
    }
89
90
    /**
91 37
     * @param ProcessResultInterface $process
92
     * @return bool
93
     */
94
    protected function parsingFoundSomething(ProcessResultInterface $process)
95
    {
96
        if (count($this->parse($process)) > 0) {
97
            $this->outputContainer->addFileName($process->getFilename());
0 ignored issues
show
Bug introduced by
The method addFileName() does not seem to exist on object<Paraunit\Printer\OutputContainer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
99
            return true;
100
        }
101
102
        return false;
103
    }
104
105
    private function parse(ProcessResultInterface $process)
106
    {
107
        $parsedBlob = array();
108
        preg_match(static::PARSING_REGEX, $process->getOutput(), $parsedBlob);
109
110
        return $parsedBlob;
111
    }
112
}
113