Completed
Push — master ( faf631...885177 )
by Konstantin
11s
created

LineFilter::filterFeature()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 45
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 45
ccs 36
cts 36
cp 1
rs 6.7272
cc 7
eloc 30
nc 6
nop 1
crap 7
1
<?php
2
3
/*
4
 * This file is part of the Behat Gherkin.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Behat\Gherkin\Filter;
12
13
use Behat\Gherkin\Node\ExampleTableNode;
14
use Behat\Gherkin\Node\FeatureNode;
15
use Behat\Gherkin\Node\OutlineNode;
16
use Behat\Gherkin\Node\ScenarioInterface;
17
18
/**
19
 * Filters scenarios by definition line number.
20
 *
21
 * @author Konstantin Kudryashov <[email protected]>
22
 */
23
class LineFilter implements FilterInterface
24
{
25
    protected $filterLine;
26
27
    /**
28
     * Initializes filter.
29
     *
30
     * @param string $filterLine Line of the scenario to filter on
31
     */
32 4
    public function __construct($filterLine)
33
    {
34 4
        $this->filterLine = intval($filterLine);
35 4
    }
36
37
    /**
38
     * Checks if Feature matches specified filter.
39
     *
40
     * @param FeatureNode $feature Feature instance
41
     *
42
     * @return Boolean
43
     */
44 1
    public function isFeatureMatch(FeatureNode $feature)
45
    {
46 1
        return $this->filterLine === $feature->getLine();
47
    }
48
49
    /**
50
     * Checks if scenario or outline matches specified filter.
51
     *
52
     * @param ScenarioInterface $scenario Scenario or Outline node instance
53
     *
54
     * @return Boolean
55
     */
56 3
    public function isScenarioMatch(ScenarioInterface $scenario)
57
    {
58 3
        if ($this->filterLine === $scenario->getLine()) {
59 3
            return true;
60
        }
61
62 3
        if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
63 2
            return $this->filterLine === $scenario->getLine()
64 2
                || in_array($this->filterLine, $scenario->getExampleTable()->getLines());
65
        }
66
67 3
        return false;
68
    }
69
70
    /**
71
     * Filters feature according to the filter and returns new one.
72
     *
73
     * @param FeatureNode $feature
74
     *
75
     * @return FeatureNode
76
     */
77 2
    public function filterFeature(FeatureNode $feature)
78
    {
79 2
        $scenarios = array();
80 2
        foreach ($feature->getScenarios() as $scenario) {
81 2
            if (!$this->isScenarioMatch($scenario)) {
82 2
                continue;
83
            }
84
85 2
            if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
86 1
                $table = $scenario->getExampleTable()->getTable();
87 1
                $lines = array_keys($table);
88
89 1
                if (in_array($this->filterLine, $lines)) {
90 1
                    $filteredTable = array($lines[0] => $table[$lines[0]]);
91
92 1
                    if ($lines[0] !== $this->filterLine) {
93 1
                        $filteredTable[$this->filterLine] = $table[$this->filterLine];
94 1
                    }
95
96 1
                    $scenario = new OutlineNode(
97 1
                        $scenario->getTitle(),
98 1
                        $scenario->getTags(),
99 1
                        $scenario->getSteps(),
100 1
                        new ExampleTableNode($filteredTable, $scenario->getExampleTable()->getKeyword()),
101 1
                        $scenario->getKeyword(),
102 1
                        $scenario->getLine()
103 1
                    );
104 1
                }
105 1
            }
106
107 2
            $scenarios[] = $scenario;
108 2
        }
109
110 2
        return new FeatureNode(
111 2
            $feature->getTitle(),
112 2
            $feature->getDescription(),
113 2
            $feature->getTags(),
114 2
            $feature->getBackground(),
115 2
            $scenarios,
116 2
            $feature->getKeyword(),
117 2
            $feature->getLanguage(),
118 2
            $feature->getFile(),
119 2
            $feature->getLine()
120 2
        );
121
    }
122
}
123