Completed
Pull Request — master (#121)
by
unknown
02:47
created

TableNode   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 299
Duplicated Lines 6.69 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 82.98%

Importance

Changes 0
Metric Value
wmc 36
lcom 1
cbo 1
dl 20
loc 299
ccs 78
cts 94
cp 0.8298
rs 8.8
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 31 8
A getNodeType() 0 4 1
A getHash() 0 4 1
A getColumnsHash() 0 12 2
A getRowsHash() 0 10 3
A getTable() 0 4 1
A getRows() 0 4 1
A getLines() 0 4 1
A getRow() 10 10 2
A getColumn() 0 15 3
A getRowLine() 10 10 2
A getRowAsString() 0 9 2
A getRowAsStringWithWrappedValues() 0 11 2
A getTableAsString() 0 9 2
A getLine() 0 4 1
A __toString() 0 4 1
A getIterator() 0 4 1
A padRight() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 ArrayIterator;
14
use Behat\Gherkin\Exception\NodeException;
15
use Iterator;
16
use IteratorAggregate;
17
18
/**
19
 * Represents Gherkin Table argument.
20
 *
21
 * @author Konstantin Kudryashov <[email protected]>
22
 */
23
class TableNode implements ArgumentInterface, IteratorAggregate
24
{
25
    /**
26
     * @var array
27
     */
28
    private $table;
29
    /**
30
     * @var integer
31
     */
32
    private $maxLineLength = array();
33
34
    /**
35
     * Initializes table.
36
     *
37
     * @param array $table Table in form of [$rowLineNumber => [$val1, $val2, $val3]]
38
     *
39
     * @throws NodeException If the given table is invalid
40
     */
41 53
    public function __construct(array $table)
42
    {
43 53
        $this->table = $table;
44 53
        $columnCount = null;
45
46 53
        foreach ($this->getRows() as $row) {
47 40
            if ($columnCount === null) {
48 40
                $columnCount = count($row);
49 40
            }
50
51 40
            if (count($row) !== $columnCount) {
52 1
                throw new NodeException('Table does not have same number of columns in every row.');
53
            }
54
55 40
            if (!is_array($row)) {
56 1
                throw new NodeException('Table is not two-dimensional.');
57
            }
58
59 39
            foreach ($row as $column => $string) {
60 39
                if (!isset($this->maxLineLength[$column])) {
61 39
                    $this->maxLineLength[$column] = 0;
62 39
                }
63
64 39
                if (is_array($string)) {
65 1
                    throw new NodeException('Table is not two-dimensional.');
66
                }
67
68 38
                $this->maxLineLength[$column] = max($this->maxLineLength[$column], mb_strlen($string, 'utf8'));
69 38
            }
70 51
        }
71 50
    }
72
73
    /**
74
     * Returns node type.
75
     *
76
     * @return string
77
     */
78
    public function getNodeType()
79
    {
80
        return 'Table';
81
    }
82
83
    /**
84
     * Returns table hash, formed by columns (ColumnsHash).
85
     *
86
     * @return array
87
     */
88 4
    public function getHash()
89
    {
90 4
        return $this->getColumnsHash();
91
    }
92
93
    /**
94
     * Returns table hash, formed by columns.
95
     *
96
     * @return array
97
     */
98 19
    public function getColumnsHash()
99
    {
100 19
        $rows = $this->getRows();
101 19
        $keys = array_shift($rows);
102
103 19
        $hash = array();
104 19
        foreach ($rows as $row) {
105 11
            $hash[] = array_combine($keys, $row);
106 19
        }
107
108 19
        return $hash;
109
    }
110
111
    /**
112
     * Returns table hash, formed by rows.
113
     *
114
     * @return array
115
     */
116 2
    public function getRowsHash()
117
    {
118 2
        $hash = array();
119
120 2
        foreach ($this->getRows() as $row) {
121 2
            $hash[array_shift($row)] = (1 == count($row)) ? $row[0] : $row;
122 2
        }
123
124 2
        return $hash;
125
    }
126
127
    /**
128
     * Returns numerated table lines.
129
     * Line numbers are keys, lines are values.
130
     *
131
     * @return array
132
     */
133 4
    public function getTable()
134
    {
135 4
        return $this->table;
136
    }
137
138
    /**
139
     * Returns table rows.
140
     *
141
     * @return array
142
     */
143 53
    public function getRows()
144
    {
145 53
        return array_values($this->table);
146
    }
147
148
    /**
149
     * Returns table definition lines.
150
     *
151
     * @return array
152
     */
153 5
    public function getLines()
154
    {
155 5
        return array_keys($this->table);
156
    }
157
158
    /**
159
     * Returns specific row in a table.
160
     *
161
     * @param integer $index Row number
162
     *
163
     * @return array
164
     *
165
     * @throws NodeException If row with specified index does not exist
166
     */
167 8 View Code Duplication
    public function getRow($index)
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...
168
    {
169 8
        $rows = $this->getRows();
170
171 8
        if (!isset($rows[$index])) {
172
            throw new NodeException(sprintf('Rows #%d does not exist in table.', $index));
173
        }
174
175 8
        return $rows[$index];
176
    }
177
178
    /**
179
     * Returns specific column in a table.
180
     *
181
     * @param integer $index Column number
182
     *
183
     * @return array
184
     *
185
     * @throws NodeException If column with specified index does not exist
186
     */
187 1
    public function getColumn($index)
188
    {
189 1
        if ($index >= count($this->getRow(0))) {
190
            throw new NodeException(sprintf('Column #%d does not exist in table.', $index));
191
        }
192
193 1
        $rows = $this->getRows();
194 1
        $column = array();
195
196 1
        foreach ($rows as $row) {
197 1
            $column[] = $row[$index];
198 1
        }
199
200 1
        return $column;
201
    }
202
203
    /**
204
     * Returns line number at which specific row was defined.
205
     *
206
     * @param integer $index
207
     *
208
     * @return integer
209
     *
210
     * @throws NodeException If row with specified index does not exist
211
     */
212 4 View Code Duplication
    public function getRowLine($index)
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...
213
    {
214 4
        $lines = array_keys($this->table);
215
216 4
        if (!isset($lines[$index])) {
217
            throw new NodeException(sprintf('Rows #%d does not exist in table.', $index));
218
        }
219
220 4
        return $lines[$index];
221
    }
222
223
    /**
224
     * Converts row into delimited string.
225
     *
226
     * @param integer $rowNum Row number
227
     *
228
     * @return string
229
     */
230 5
    public function getRowAsString($rowNum)
231
    {
232 5
        $values = array();
233 5
        foreach ($this->getRow($rowNum) as $column => $value) {
234 5
            $values[] = $this->padRight(' ' . $value . ' ', $this->maxLineLength[$column] + 2);
235 5
        }
236
237 5
        return sprintf('|%s|', implode('|', $values));
238
    }
239
240
    /**
241
     * Converts row into delimited string.
242
     *
243
     * @param integer  $rowNum  Row number
244
     * @param callable $wrapper Wrapper function
245
     *
246
     * @return string
247
     */
248
    public function getRowAsStringWithWrappedValues($rowNum, $wrapper)
249
    {
250
        $values = array();
251
        foreach ($this->getRow($rowNum) as $column => $value) {
252
            $value = $this->padRight(' ' . $value . ' ', $this->maxLineLength[$column] + 2);
253
254
            $values[] = call_user_func($wrapper, $value, $column);
255
        }
256
257
        return sprintf('|%s|', implode('|', $values));
258
    }
259
260
    /**
261
     * Converts entire table into string
262
     *
263
     * @return string
264
     */
265 2
    public function getTableAsString()
266
    {
267 2
        $lines = array();
268 2
        for ($i = 0; $i < count($this->getRows()); $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...
269 2
            $lines[] = $this->getRowAsString($i);
270 2
        }
271
272 2
        return implode("\n", $lines);
273
    }
274
275
    /**
276
     * Returns line number at which table was started.
277
     *
278
     * @return integer
279
     */
280
    public function getLine()
281
    {
282
        return $this->getRowLine(0);
283
    }
284
285
    /**
286
     * Converts table into string
287
     *
288
     * @return string
289
     */
290
    public function __toString()
291
    {
292
        return $this->getTableAsString();
293
    }
294
295
    /**
296
     * Retrieves a hash iterator.
297
     *
298
     * @return Iterator
299
     */
300 1
    public function getIterator()
301
    {
302 1
        return new ArrayIterator($this->getHash());
303
    }
304
305
    /**
306
     * Pads string right.
307
     *
308
     * @param string  $text   Text to pad
309
     * @param integer $length Length
310
     *
311
     * @return string
312
     */
313 5
    protected function padRight($text, $length)
314
    {
315 5
        while ($length > mb_strlen($text, 'utf8')) {
316 5
            $text = $text . ' ';
317 5
        }
318
319 5
        return $text;
320
    }
321
}
322