LineFilter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 3
            return $this->filterLine === $scenario->getLine()
64 3
                || in_array($this->filterLine, $scenario->getExampleTable()->getLines());
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...
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
                foreach ($scenario->getExampleTables() as $exampleTable) {
87 1
                    $table = $exampleTable->getTable();
88 1
                    $lines = array_keys($table);
89
90 1
                    if (in_array($this->filterLine, $lines)) {
91 1
                        $filteredTable = array($lines[0] => $table[$lines[0]]);
92
93 1
                        if ($lines[0] !== $this->filterLine) {
94 1
                            $filteredTable[$this->filterLine] = $table[$this->filterLine];
95
                        }
96
97 1
                        $scenario = new OutlineNode(
98 1
                            $scenario->getTitle(),
99 1
                            $scenario->getTags(),
100 1
                            $scenario->getSteps(),
101 1
                            array(new ExampleTableNode($filteredTable, $exampleTable->getKeyword(), $exampleTable->getTags())),
102 1
                            $scenario->getKeyword(),
103 1
                            $scenario->getLine()
104
                        );
105
                    }
106
                }
107
            }
108
109 2
            $scenarios[] = $scenario;
110
        }
111
112 2
        return new FeatureNode(
113 2
            $feature->getTitle(),
114 2
            $feature->getDescription(),
115 2
            $feature->getTags(),
116 2
            $feature->getBackground(),
117 2
            $scenarios,
118 2
            $feature->getKeyword(),
119 2
            $feature->getLanguage(),
120 2
            $feature->getFile(),
121 2
            $feature->getLine()
122
        );
123
    }
124
}
125