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