Completed
Pull Request — master (#119)
by
unknown
02:35
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
     * Returns outline tags (including inherited from feature).
109
     *
110
     * @return string[]
111
     */
112 5
    public function getTags()
113
    {
114 5
        return $this->tags;
115
    }
116
117
    /**
118
     * Checks if outline has tags (both inherited from feature and own).
119
     *
120
     * @return Boolean
121
     */
122 1
    public function hasTags()
123
    {
124 1
        return 0 < count($this->getTags());
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
     * Checks if outline has examples.
139
     *
140
     * @return Boolean
141
     */
142 12
    public function hasExamples()
143
    {
144 12
        return 0 < count($this->tables);
145
    }
146
147
    /**
148
     * Builds and returns examples table for the outline.
149
     *
150
     * WARNING: it returns a merged table with tags lost.
151
     *
152
     * @deprecated use getExampleTables instead
153
     * @return ExampleTableNode
154
     */
155
    public function getExampleTable()
156
    {
157
        $table = array();
158
159
        foreach ($this->tables[0]->getTable() as $k => $v) {
160
            $table[$k] = $v;
161
        }
162
163
        /** @var ExampleTableNode $exampleTableNode */
164
        $exampleTableNode = new ExampleTableNode($table, $this->tables[0]->getKeyword());
165
        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...
166
            $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...
167
        }
168
169
        return $exampleTableNode;
170
    }
171
172
    /**
173
     * Returns list of examples for the outline.
174
     * @return ExampleNode[]
175
     */
176 6
    public function getExamples()
177
    {
178 6
        return $this->examples = $this->examples ?: $this->createExamples();
179
    }
180
181
    /**
182
     * Creates examples for this outline using examples table.
183
     *
184
     * @return ExampleNode[]
185
     */
186 6
    protected function createExamples()
187
    {
188 6
        $examples = array();
189
190 6
        foreach ($this->getExampleTables() as $exampleTable) {
191 6
            foreach ($exampleTable->getColumnsHash() as $rowNum => $row) {
192 4
                $examples[] = new ExampleNode(
193 4
                    $exampleTable->getRowAsString($rowNum + 1),
194 4
                    array_merge($this->tags, $exampleTable->getTags()),
195 4
                    $this->getSteps(),
196 4
                    $row,
197 4
                    $exampleTable->getRowLine($rowNum + 1)
198 4
                );
199 6
            }
200 6
        }
201
202 6
        return $examples;
203
    }
204
205
    /**
206
     * Returns examples tables array for the outline.
207
     * @return ExampleTableNode[]
208
     */
209 10
    public function getExampleTables()
210
    {
211 10
        return $this->tables;
212
    }
213
214
    /**
215
     * Returns outline steps.
216
     *
217
     * @return StepNode[]
218
     */
219 8
    public function getSteps()
220
    {
221 8
        return $this->steps;
222
    }
223
224
    /**
225
     * Returns outline keyword.
226
     *
227
     * @return string
228
     */
229 3
    public function getKeyword()
230
    {
231 3
        return $this->keyword;
232
    }
233
234
    /**
235
     * Returns outline declaration line number.
236
     *
237
     * @return integer
238
     */
239 16
    public function getLine()
240
    {
241 16
        return $this->line;
242
    }
243
}
244