LineRangeFilter::filterFeature()   B
last analyzed

Complexity

Conditions 10
Paths 4

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 35
cts 35
cp 1
rs 7.1151
c 0
b 0
f 0
cc 10
nc 4
nop 1
crap 10

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        } 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 7
            foreach ($scenario->getExampleTable()->getLines() as $line) {
0 ignored issues
show
Deprecated Code introduced by
The method Behat\Gherkin\Node\OutlineNode::getExampleTable() has been deprecated with message: use getExampleTables instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
72 2
                if ($this->filterMinLine <= $line && $this->filterMaxLine >= $line) {
73 1
                    return true;
74
                }
75
            }
76
        }
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
                // first accumulate examples and then create scenario
98 1
                $exampleTableNodes = array();
99
100 1
                foreach ($scenario->getExampleTables() as $exampleTable) {
101 1
                    $table = $exampleTable->getTable();
102 1
                    $lines = array_keys($table);
103
104 1
                    $filteredTable = array($lines[0] => $table[$lines[0]]);
105 1
                    unset($table[$lines[0]]);
106
107 1
                    foreach ($table as $line => $row) {
108 1
                        if ($this->filterMinLine <= $line && $this->filterMaxLine >= $line) {
109 1
                            $filteredTable[$line] = $row;
110
                        }
111
                    }
112
113 1
                    if (count($filteredTable) > 1) {
114 1
                        $exampleTableNodes[] = new ExampleTableNode($filteredTable, $exampleTable->getKeyword(), $exampleTable->getTags());
115
                    }
116
                }
117
118 1
                $scenario = new OutlineNode(
119 1
                    $scenario->getTitle(),
120 1
                    $scenario->getTags(),
121 1
                    $scenario->getSteps(),
122 1
                    $exampleTableNodes,
123 1
                    $scenario->getKeyword(),
124 1
                    $scenario->getLine()
125
                );
126
            }
127
128 2
            $scenarios[] = $scenario;
129
        }
130
131 2
        return new FeatureNode(
132 2
            $feature->getTitle(),
133 2
            $feature->getDescription(),
134 2
            $feature->getTags(),
135 2
            $feature->getBackground(),
136 2
            $scenarios,
137 2
            $feature->getKeyword(),
138 2
            $feature->getLanguage(),
139 2
            $feature->getFile(),
140 2
            $feature->getLine()
141
        );
142
    }
143
}
144