Completed
Pull Request — master (#119)
by
unknown
02:43
created

OutlineNode::getExampleTables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 4
cts 4
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 34 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 34
        $this->title = $title;
68 34
        $this->tags = $tags;
69 34
        $this->steps = $steps;
70 34
        $this->tables = $tables;
71 34
        $this->keyword = $keyword;
72 34
        $this->line = $line;
73 34
    }
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 4
    public function getTitle()
91
    {
92 4
        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 1
    public function getTags()
113
    {
114 1
        return $this->tags;
115
    }
116
117
    /**
118
     * Checks if outline has tags (both inherited from feature and own).
119
     *
120
     * @return Boolean
121
     */
122 3
    public function hasTags()
123
    {
124 3
        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 6
    public function hasExamples()
143
    {
144 6
        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 10
     * @deprecated use getExampleTables instead
153
     * @return ExampleTableNode
154 10
     */
155
    public function getExampleTable()
156
    {
157
        $table = array();
158
159
        foreach ($this->tables[0]->getTable() as $k => $v) {
160
            $table[$k] = $v;
161
        }
162 5
163
        /** @var ExampleTableNode $exampleTableNode */
164 5
        $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 5
    /**
173
     * Returns list of examples for the outline.
174 5
     * @return ExampleNode[]
175
     */
176
    public function getExamples()
177
    {
178
        return $this->examples = $this->examples ?: $this->createExamples();
179
    }
180
181
    /**
182 2
     * Creates examples for this outline using examples table.
183
     *
184 2
     * @return ExampleNode[]
185
     */
186
    protected function createExamples()
187
    {
188
        $examples = array();
189
190
        foreach ($this->getExampleTables() as $exampleTable) {
191
            foreach ($exampleTable->getColumnsHash() as $rowNum => $row) {
192 15
                $examples[] = new ExampleNode(
193
                    $exampleTable->getRowAsString($rowNum + 1),
194 15
                    array_merge($this->tags, $exampleTable->getTags()),
195
                    $this->getSteps(),
196
                    $row,
197
                    $exampleTable->getRowLine($rowNum + 1)
198
                );
199
            }
200
        }
201
202 5
        return $examples;
203
    }
204 5
205 5
    /**
206 3
     * Returns examples tables array for the outline.
207 3
     * @return ExampleTableNode[]
208 3
     */
209 3
    public function getExampleTables()
210 3
    {
211 3
        return $this->tables;
212 3
    }
213 5
214
    /**
215 5
     * Returns outline steps.
216
     *
217
     * @return StepNode[]
218
     */
219
    public function getSteps()
220
    {
221
        return $this->steps;
222
    }
223
224
    /**
225
     * Returns outline keyword.
226
     *
227
     * @return string
228
     */
229
    public function getKeyword()
230
    {
231
        return $this->keyword;
232
    }
233
234
    /**
235
     * Returns outline declaration line number.
236
     *
237
     * @return integer
238
     */
239
    public function getLine()
240
    {
241
        return $this->line;
242
    }
243
}
244