Completed
Push — master ( 3f293a...dfbdc5 )
by Konstantin
02:30
created

TableNode   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 312
Duplicated Lines 6.41 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 83.51%

Importance

Changes 0
Metric Value
wmc 36
lcom 1
cbo 1
dl 20
loc 312
ccs 81
cts 97
cp 0.8351
rs 8.8
c 0
b 0
f 0

19 Methods

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