Completed
Pull Request — master (#119)
by
unknown
07:15
created

OutlineNode::getExampleTables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\Node;
12
13
/**
14
 * Represents Gherkin Outline.
15
 *
16
 * @author Konstantin Kudryashov <[email protected]>
17
 */
18
class OutlineNode implements ScenarioInterface
19
{
20
    /**
21
     * @var string
22
     */
23
    private $title;
24
    /**
25
     * @var string[]
26
     */
27
    private $tags;
28
    /**
29
     * @var StepNode[]
30
     */
31
    private $steps;
32
    /**
33
     * @var ExampleTableNode[]
34
     */
35
    private $tables;
36
    /**
37
     * @var string
38
     */
39
    private $keyword;
40
    /**
41
     * @var integer
42
     */
43
    private $line;
44
    /**
45
     * @var null|ExampleNode[]
46
     */
47
    private $examples;
48
49
    /**
50
     * Initializes outline.
51
     *
52
     * @param null|string      $title
53
     * @param string[]         $tags
54
     * @param StepNode[]       $steps
55
     * @param ExampleTableNode[] $tables
56
     * @param string           $keyword
57
     * @param integer          $line
58
     */
59 38 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
        $title,
61
        array $tags,
62
        array $steps,
63
        array $tables,
64
        $keyword,
65
        $line
66
    ) {
67 38
        $this->title = $title;
68 38
        $this->tags = $tags;
69 38
        $this->steps = $steps;
70 38
        $this->tables = $tables;
71 38
        $this->keyword = $keyword;
72 38
        $this->line = $line;
73 38
    }
74
75
    /**
76
     * Returns node type string
77
     *
78
     * @return string
79
     */
80
    public function getNodeType()
81
    {
82
        return 'Outline';
83
    }
84
85
    /**
86
     * Returns outline title.
87
     *
88
     * @return null|string
89
     */
90 5
    public function getTitle()
91
    {
92 5
        return $this->title;
93
    }
94
95
    /**
96
     * Checks if outline is tagged with tag.
97
     *
98
     * @param string $tag
99
     *
100
     * @return Boolean
101
     */
102
    public function hasTag($tag)
103
    {
104
        return in_array($tag, $this->getTags());
105
    }
106
107
    /**
108
     * Checks if outline has tags (both inherited from feature and own).
109
     *
110
     * @return Boolean
111
     */
112 1
    public function hasTags()
113
    {
114 1
        return 0 < count($this->getTags());
115
    }
116
117
    /**
118
     * Returns outline tags (including inherited from feature).
119
     *
120
     * @return string[]
121
     */
122 5
    public function getTags()
123
    {
124 5
        return $this->tags;
125
    }
126
127
    /**
128
     * Checks if outline has steps.
129
     *
130
     * @return Boolean
131
     */
132 1
    public function hasSteps()
133
    {
134 1
        return 0 < count($this->steps);
135
    }
136
137
    /**
138
     * Returns outline steps.
139
     *
140
     * @return StepNode[]
141
     */
142 8
    public function getSteps()
143
    {
144 8
        return $this->steps;
145
    }
146
147
    /**
148
     * Checks if outline has examples.
149
     *
150
     * @return Boolean
151
     */
152 12
    public function hasExamples()
153
    {
154 12
        return 0 < count($this->tables);
155
    }
156
157
    /**
158
     * Builds and returns examples table for the outline.
159
     *
160
     * WARNING: it returns a merged table with tags lost.
161
     *
162
     * @deprecated use getExampleTables instead
163
     * @return ExampleTableNode
164
     */
165 11
    public function getExampleTable()
166
    {
167 11
        $table = array();
168
169 11
        foreach ($this->tables[0]->getTable() as $k => $v) {
170 5
            $table[$k] = $v;
171 11
        }
172
173
        /** @var ExampleTableNode $exampleTableNode */
174 11
        $exampleTableNode = new ExampleTableNode($table, $this->tables[0]->getKeyword());
175 11
        for ($i = 1; $i < count($this->tables); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
176 4
            $exampleTableNode->mergeRowsFromTable($this->tables[$i]);
0 ignored issues
show
Deprecated Code introduced by
The method Behat\Gherkin\Node\TableNode::mergeRowsFromTable() has been deprecated with message: remove together with OutlineNode::getExampleTable

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...
177 4
        }
178
179 11
        return $exampleTableNode;
180
    }
181
182
    /**
183
     * Returns list of examples for the outline.
184
     * @return ExampleNode[]
185
     */
186 6
    public function getExamples()
187
    {
188 6
        return $this->examples = $this->examples ?: $this->createExamples();
189
    }
190
191
    /**
192
     * Returns examples tables array for the outline.
193
     * @return ExampleTableNode[]
194
     */
195 10
    public function getExampleTables()
196
    {
197 10
        return $this->tables;
198
    }
199
200
    /**
201
     * Returns outline keyword.
202
     *
203
     * @return string
204
     */
205 3
    public function getKeyword()
206
    {
207 3
        return $this->keyword;
208
    }
209
210
    /**
211
     * Returns outline declaration line number.
212
     *
213
     * @return integer
214
     */
215 16
    public function getLine()
216
    {
217 16
        return $this->line;
218
    }
219
220
    /**
221
     * Creates examples for this outline using examples table.
222
     *
223
     * @return ExampleNode[]
224
     */
225 6
    protected function createExamples()
226
    {
227 6
        $examples = array();
228
229 6
        foreach ($this->getExampleTables() as $exampleTable) {
230 6
            foreach ($exampleTable->getColumnsHash() as $rowNum => $row) {
231 4
                $examples[] = new ExampleNode(
232 4
                    $exampleTable->getRowAsString($rowNum + 1),
233 4
                    array_merge($this->tags, $exampleTable->getTags()),
234 4
                    $this->getSteps(),
235 4
                    $row,
236 4
                    $exampleTable->getRowLine($rowNum + 1)
237 4
                );
238 6
            }
239 6
        }
240
241 6
        return $examples;
242
    }
243
}
244