Parser   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
c 2
b 0
f 0
dl 0
loc 44
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 30 6
A __construct() 0 4 1
1
<?php
2
3
namespace Bex\Behat\BehatRSTSpecificationLocatorExtension\RST;
4
5
use Behat\Gherkin\Node\FeatureNode;
6
use Behat\Gherkin\Parser as GherkinParser;
7
use Doctrine\RST\Nodes\CodeNode;
8
use Doctrine\RST\Nodes\QuoteNode;
9
use Doctrine\RST\Parser as DoctrineRSTParser;
10
11
class Parser
12
{
13
    /** @var DoctrineRSTParser */
14
    private $parser;
15
16
    /** @var GherkinParser */
17
    private $gherkinParser;
18
19
    public function __construct(DoctrineRSTParser $parser, GherkinParser $gherkinParser)
20
    {
21
        $this->parser = $parser;
22
        $this->gherkinParser = $gherkinParser;
23
    }
24
25
    public function parse(string $input, string $file = null): ?FeatureNode
26
    {
27
        $mainDocument = $this->parser->parse($input);
28
29
        // collect documents (content of quote notes counts as an embedded document)
30
        $documents = [$mainDocument];
31
        foreach ($mainDocument->getNodes() as $node) {
32
            if ($node instanceof QuoteNode) {
33
                $documents[] = $node->getValue();
34
            }
35
        }
36
37
        // collect all code node
38
        $gherkinNodes = [];
39
        foreach ($documents as $document) {
40
            $documentGherkinNodes = $document->getNodes(function ($node) {
41
                return ($node instanceof CodeNode) && ($node->getLanguage() === 'gherkin');
42
            });
43
44
            $gherkinNodes = array_merge($gherkinNodes, $documentGherkinNodes);
45
        }
46
47
        $feature = $mainDocument->getTitle();
48
49
        $text = '';
50
        foreach ($gherkinNodes as $node) {
51
            $text .= $node->getValue() . PHP_EOL;
52
        }
53
54
        return $this->gherkinParser->parse('Feature: ' . $feature . PHP_EOL . $text, $file);
55
    }
56
}