Completed
Pull Request — master (#161)
by Phil
13:29
created

OutlineNode::getExampleTables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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