Completed
Push — master ( df4d09...dfc47a )
by Ciaran
08:37
created

OutlineNode::getExampleTables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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
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|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|ExampleTableNode[]  $tables
56
     * @param string           $keyword
57
     * @param integer          $line
58
     */
59 226
    public function __construct(
60
        $title,
61
        array $tags,
62
        array $steps,
63
        $tables,
64
        $keyword,
65
        $line
66
    ) {
67 226
        $this->title = $title;
68 226
        $this->tags = $tags;
69 226
        $this->steps = $steps;
70 226
        $this->keyword = $keyword;
71 226
        $this->line = $line;
72 226
        if (!is_array($tables)) {
73 5
           $this->tables = array($tables);
74
        } else {
75 221
            $this->tables = $tables;
76
        }
77 226
    }
78
79
    /**
80
     * Returns node type string
81
     *
82
     * @return string
83
     */
84
    public function getNodeType()
85
    {
86
        return 'Outline';
87
    }
88
89
    /**
90
     * Returns outline title.
91
     *
92
     * @return null|string
93
     */
94 5
    public function getTitle()
95
    {
96 5
        return $this->title;
97
    }
98
99
    /**
100
     * Checks if outline is tagged with tag.
101
     *
102
     * @param string $tag
103
     *
104
     * @return Boolean
105
     */
106
    public function hasTag($tag)
107
    {
108
        return in_array($tag, $this->getTags());
109
    }
110
111
    /**
112
     * Checks if outline has tags (both inherited from feature and own).
113
     *
114
     * @return Boolean
115
     */
116 1
    public function hasTags()
117
    {
118 1
        return 0 < count($this->getTags());
119
    }
120
121
    /**
122
     * Returns outline tags (including inherited from feature).
123
     *
124
     * @return string[]
125
     */
126 5
    public function getTags()
127
    {
128 5
        return $this->tags;
129
    }
130
131
    /**
132
     * Checks if outline has steps.
133
     *
134
     * @return Boolean
135
     */
136 1
    public function hasSteps()
137
    {
138 1
        return 0 < count($this->steps);
139
    }
140
141
    /**
142
     * Returns outline steps.
143
     *
144
     * @return StepNode[]
145
     */
146 8
    public function getSteps()
147
    {
148 8
        return $this->steps;
149
    }
150
151
    /**
152
     * Checks if outline has examples.
153
     *
154
     * @return Boolean
155
     */
156 12
    public function hasExamples()
157
    {
158 12
        return 0 < count($this->tables);
159
    }
160
161
    /**
162
     * Builds and returns examples table for the outline.
163
     *
164
     * WARNING: it returns a merged table with tags lost.
165
     *
166
     * @deprecated use getExampleTables instead
167
     * @return ExampleTableNode
168
     */
169 11
    public function getExampleTable()
170
    {
171 11
        $table = array();
172 11
        foreach ($this->tables[0]->getTable() as $k => $v) {
173 5
            $table[$k] = $v;
174
        }
175
176
        /** @var ExampleTableNode $exampleTableNode */
177 11
        $exampleTableNode = new ExampleTableNode($table, $this->tables[0]->getKeyword());
178 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...
179 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...
180
        }
181 11
        return $exampleTableNode;
182
    }
183
184
    /**
185
     * Returns list of examples for the outline.
186
     * @return ExampleNode[]
187
     */
188 6
    public function getExamples()
189
    {
190 6
        return $this->examples = $this->examples ?: $this->createExamples();
191
    }
192
193
    /**
194
     * Returns examples tables array for the outline.
195
     * @return ExampleTableNode[]
196
     */
197 10
    public function getExampleTables()
198
    {
199 10
        return $this->tables;
200
    }
201
202
    /**
203
     * Returns outline keyword.
204
     *
205
     * @return string
206
     */
207 3
    public function getKeyword()
208
    {
209 3
        return $this->keyword;
210
    }
211
212
    /**
213
     * Returns outline declaration line number.
214
     *
215
     * @return integer
216
     */
217 16
    public function getLine()
218
    {
219 16
        return $this->line;
220
    }
221
222
    /**
223
     * Creates examples for this outline using examples table.
224
     *
225
     * @return ExampleNode[]
226
     */
227 6
    protected function createExamples()
228
    {
229 6
        $examples = array();
230
231 6
        foreach ($this->getExampleTables() as $exampleTable) {
232 6
            foreach ($exampleTable->getColumnsHash() as $rowNum => $row) {
233 4
                $examples[] = new ExampleNode(
234 4
                    $exampleTable->getRowAsString($rowNum + 1),
235 4
                    array_merge($this->tags, $exampleTable->getTags()),
236 4
                    $this->getSteps(),
237 4
                    $row,
238 4
                    $exampleTable->getRowLine($rowNum + 1)
239
                );
240
            }
241
        }
242
243 6
        return $examples;
244
    }
245
}
246