Completed
Pull Request — master (#105)
by
unknown
03:01
created

TableNode   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 321
Duplicated Lines 13.4 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 80.38%

Importance

Changes 10
Bugs 1 Features 2
Metric Value
wmc 36
c 10
b 1
f 2
lcom 1
cbo 1
dl 43
loc 321
ccs 82
cts 102
cp 0.8038
rs 8.8

19 Methods

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