Completed
Pull Request — master (#119)
by
unknown
01:47
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
use DeepCopy\DeepCopy;
14
15
/**
16
 * Represents Gherkin Outline.
17
 *
18
 * @author Konstantin Kudryashov <[email protected]>
19
 */
20
class OutlineNode implements ScenarioInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    private $title;
26
    /**
27
     * @var string[]
28
     */
29
    private $tags;
30
    /**
31
     * @var StepNode[]
32
     */
33
    private $steps;
34
    /**
35
     * @var ExampleTableNode[]
36
     */
37
    private $tables;
38
    /**
39
     * @var string
40
     */
41
    private $keyword;
42
    /**
43
     * @var integer
44
     */
45
    private $line;
46
    /**
47
     * @var null|ExampleNode[]
48
     */
49
    private $examples;
50
51
    /**
52
     * Initializes outline.
53
     *
54
     * @param null|string      $title
55
     * @param string[]         $tags
56
     * @param StepNode[]       $steps
57
     * @param ExampleTableNode[] $tables
58
     * @param string           $keyword
59 34
     * @param integer          $line
60
     */
61 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...
62
        $title,
63
        array $tags,
64
        array $steps,
65
        array $tables,
66
        $keyword,
67 34
        $line
68 34
    ) {
69 34
        $this->title = $title;
70 34
        $this->tags = $tags;
71 34
        $this->steps = $steps;
72 34
        $this->tables = $tables;
73 34
        $this->keyword = $keyword;
74
        $this->line = $line;
75
    }
76
77
    /**
78
     * Returns node type string
79
     *
80
     * @return string
81
     */
82
    public function getNodeType()
83
    {
84
        return 'Outline';
85
    }
86
87
    /**
88
     * Returns outline title.
89
     *
90 4
     * @return null|string
91
     */
92 4
    public function getTitle()
93
    {
94
        return $this->title;
95
    }
96
97
    /**
98
     * Checks if outline is tagged with tag.
99
     *
100
     * @param string $tag
101
     *
102
     * @return Boolean
103
     */
104
    public function hasTag($tag)
105
    {
106
        return in_array($tag, $this->getTags());
107
    }
108
109
    /**
110
     * Returns outline tags (including inherited from feature).
111
     *
112 1
     * @return string[]
113
     */
114 1
    public function getTags()
115
    {
116
        return $this->tags;
117
    }
118
119
    /**
120
     * Checks if outline has tags (both inherited from feature and own).
121
     *
122 3
     * @return Boolean
123
     */
124 3
    public function hasTags()
125
    {
126
        return 0 < count($this->getTags());
127
    }
128
129
    /**
130
     * Checks if outline has steps.
131
     *
132 1
     * @return Boolean
133
     */
134 1
    public function hasSteps()
135
    {
136
        return 0 < count($this->steps);
137
    }
138
139
    /**
140
     * Checks if outline has examples.
141
     *
142 6
     * @return Boolean
143
     */
144 6
    public function hasExamples()
145
    {
146
        return 0 < count($this->tables);
147
    }
148
149
    /**
150
     * Builds and returns examples table for the outline.
151
     *
152 10
     * WARNING: it returns a merged table with tags lost.
153
     *
154 10
     * @deprecated use getExampleTables instead
155
     * @return ExampleTableNode
156
     */
157
    public function getExampleTable()
158
    {
159
        $cp = new DeepCopy();
160
        $table = $cp->copy($this->tables[0]->getTable());
161
        /** @var ExampleTableNode $exampleTableNode */
162 5
        $exampleTableNode = new ExampleTableNode($table, $this->tables[0]->getKeyword());
163
        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...
164 5
            $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...
165
        }
166
167
        return $exampleTableNode;
168
    }
169
170
    /**
171
     * Returns list of examples for the outline.
172 5
     * @return ExampleNode[]
173
     */
174 5
    public function getExamples()
175
    {
176
        return $this->examples = $this->examples ?: $this->createExamples();
177
    }
178
179
    /**
180
     * Creates examples for this outline using examples table.
181
     *
182 2
     * @return ExampleNode[]
183
     */
184 2
    protected function createExamples()
185
    {
186
        $examples = [];
187
188
        foreach ($this->getExampleTables() as $exampleTable) {
189
            foreach ($exampleTable->getColumnsHash() as $rowNum => $row) {
190
                $examples[] = new ExampleNode(
191
                    $exampleTable->getRowAsString($rowNum + 1),
192 15
                    array_merge($this->tags, $exampleTable->getTags()),
193
                    $this->getSteps(),
194 15
                    $row,
195
                    $exampleTable->getRowLine($rowNum + 1)
196
                );
197
            }
198
        }
199
200
        return $examples;
201
    }
202 5
203
    /**
204 5
     * Returns examples tables array for the outline.
205 5
     * @return ExampleTableNode[]
206 3
     */
207 3
    public function getExampleTables()
208 3
    {
209 3
        return $this->tables;
210 3
    }
211 3
212 3
    /**
213 5
     * Returns outline steps.
214
     *
215 5
     * @return StepNode[]
216
     */
217
    public function getSteps()
218
    {
219
        return $this->steps;
220
    }
221
222
    /**
223
     * Returns outline keyword.
224
     *
225
     * @return string
226
     */
227
    public function getKeyword()
228
    {
229
        return $this->keyword;
230
    }
231
232
    /**
233
     * Returns outline declaration line number.
234
     *
235
     * @return integer
236
     */
237
    public function getLine()
238
    {
239
        return $this->line;
240
    }
241
}
242