Completed
Pull Request — master (#112)
by Christophe
02:45
created

LineRangeFilter::filterFeature()   C

Complexity

Conditions 8
Paths 4

Size

Total Lines 46
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 46
ccs 37
cts 37
cp 1
rs 5.5555
cc 8
eloc 31
nc 4
nop 1
crap 8
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 range.
20
 *
21
 * @author Fabian Kiss <[email protected]>
22
 */
23
class LineRangeFilter implements FilterInterface
24
{
25
    protected $filterMinLine;
26
    protected $filterMaxLine;
27
28
    /**
29
     * Initializes filter.
30
     *
31
     * @param string $filterMinLine Minimum line of a scenario to filter on
32
     * @param string $filterMaxLine Maximum line of a scenario to filter on
33
     */
34 16
    public function __construct($filterMinLine, $filterMaxLine)
35
    {
36 16
        $this->filterMinLine = intval($filterMinLine);
37 16
        if ($filterMaxLine == '*') {
38 6
            $this->filterMaxLine = PHP_INT_MAX;
39 6
        } else {
40 10
            $this->filterMaxLine = intval($filterMaxLine);
41
        }
42 16
    }
43
44
    /**
45
     * Checks if Feature matches specified filter.
46
     *
47
     * @param FeatureNode $feature Feature instance
48
     *
49
     * @return Boolean
50
     */
51 5
    public function isFeatureMatch(FeatureNode $feature)
52
    {
53 5
        return $this->filterMinLine <= $feature->getLine()
54 5
            && $this->filterMaxLine >= $feature->getLine();
55
    }
56
57
    /**
58
     * Checks if scenario or outline matches specified filter.
59
     *
60
     * @param ScenarioInterface $scenario Scenario or Outline node instance
61
     *
62
     * @return Boolean
63
     */
64 11
    public function isScenarioMatch(ScenarioInterface $scenario)
65
    {
66 11
        if ($this->filterMinLine <= $scenario->getLine() && $this->filterMaxLine >= $scenario->getLine()) {
67 8
            return true;
68
        }
69
70 9
        if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
71 2
            foreach ($scenario->getExampleTable()->getLines() as $line) {
72 2
                if ($this->filterMinLine <= $line && $this->filterMaxLine >= $line) {
73 1
                    return true;
74
                }
75 1
            }
76 1
        }
77
78 9
        return false;
79
    }
80
81
    /**
82
     * Filters feature according to the filter.
83
     *
84
     * @param FeatureNode $feature
85
     *
86
     * @return FeatureNode
87
     */
88 2
    public function filterFeature(FeatureNode $feature)
89
    {
90 2
        $scenarios = array();
91 2
        foreach ($feature->getScenarios() as $scenario) {
92 2
            if (!$this->isScenarioMatch($scenario)) {
93 2
                continue;
94
            }
95
96 2
            if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
97 1
                $table = $scenario->getExampleTable()->getTable();
98 1
                $lines = array_keys($table);
99
100 1
                $filteredTable = array($lines[0] => $table[$lines[0]]);
101 1
                unset($table[$lines[0]]);
102
103 1
                foreach ($table as $line => $row) {
104 1
                    if ($this->filterMinLine <= $line && $this->filterMaxLine >= $line) {
105 1
                        $filteredTable[$line] = $row;
106 1
                    }
107 1
                }
108
109 1
                $scenario = new OutlineNode(
110 1
                    $scenario->getTitle(),
111 1
                    $scenario->getTags(),
112 1
                    $scenario->getSteps(),
113 1
                    new ExampleTableNode($filteredTable, $scenario->getExampleTable()->getKeyword()),
114 1
                    $scenario->getKeyword(),
115 1
                    $scenario->getLine()
116 1
                );
117 1
            }
118
119 2
            $scenarios[] = $scenario;
120 2
        }
121
122 2
        return new FeatureNode(
123 2
            $feature->getTitle(),
124 2
            $feature->getDescription(),
125 2
            $feature->getTags(),
126 2
            $feature->getBackground(),
127 2
            $scenarios,
128 2
            $feature->getKeyword(),
129 2
            $feature->getLanguage(),
130 2
            $feature->getFile(),
131 2
            $feature->getLine()
132 2
        );
133
    }
134
}
135