Completed
Pull Request — master (#7)
by
unknown
04:45
created

TableParser::parseColumns()   B

Complexity

Conditions 10
Paths 5

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 7.2765
c 0
b 0
f 0
cc 10
eloc 12
nc 5
nop 1
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This is part of the webuni/commonmark-table-extension package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Webuni\CommonMark\TableExtension;
14
15
use League\CommonMark\Block\Element\Paragraph;
16
use League\CommonMark\Block\Parser\AbstractBlockParser;
17
use League\CommonMark\ContextInterface;
18
use League\CommonMark\Cursor;
19
use League\CommonMark\Util\RegexHelper;
20
21
class TableParser extends AbstractBlockParser
22
{
23
    const REGEXP_DEFINITION = '/(?: *(:?) *-+ *(:?) *)+(?=\||$)/';
24
    const REGEXP_CELLS = '/(?:`[^`]*`|\\\\\\\\|\\\\\||[^|`\\\\]+)+(?=\||$)/';
25
    const REGEXP_CAPTION = '/^\[(.+?)\](?:\[(.+)\])?\s*$/';
26
27 14
    public function parse(ContextInterface $context, Cursor $cursor)
28
    {
29 14
        $container = $context->getContainer();
30
31 14
        if (!$container instanceof Paragraph) {
32 14
            return false;
33
        }
34
35 14
        $lines = $container->getStrings();
36 14
        if (count($lines) < 1) {
37
            return false;
38
        }
39
40 14
        $match = RegexHelper::matchAll(self::REGEXP_DEFINITION, $cursor->getLine(), $cursor->getFirstNonSpacePosition());
41 14
        if (null === $match) {
42 1
            return false;
43
        }
44
45 14
        $columns = $this->parseColumns($match);
46 14
        $head = $this->parseRow(trim(array_pop($lines)), $columns, TableCell::TYPE_HEAD);
47 14
        if (null === $head) {
48
            return false;
49
        }
50
51 14
        $table = new Table(function (Cursor $cursor) use (&$table, $columns) {
52 14
            $row = $this->parseRow($cursor->getLine(), $columns);
53 14
            if (null === $row) {
54 3
                if (null !== $table->getCaption()) {
55
                    return false;
56
                }
57
58 3
                if (null !== ($caption = $this->parseCaption($cursor->getLine()))) {
59
                    $table->setCaption($caption);
60
61
                    return true;
62
                }
63
64 3
                return false;
65
            }
66
67 14
            $table->getBody()->appendChild($row);
68
69 14
            return true;
70 14
        });
71
72 14
        $table->getHead()->appendChild($head);
73
74 14
        if (count($lines) >= 1) {
75 1
            $paragraph = new Paragraph();
76 1
            foreach ($lines as $line) {
77 1
                $paragraph->addLine($line);
78
            }
79
80 1
            $context->replaceContainerBlock($paragraph);
81 1
            $context->addBlock($table);
82
        } else {
83 14
            $context->replaceContainerBlock($table);
84
        }
85
86 14
        return true;
87
    }
88
89 14
    private function parseColumns(array $match)
90
    {
91 14
        $columns = [];
92 14
        foreach ((array) $match[0] as $i => $column) {
93 14
            if (isset($match[1][$i]) && $match[1][$i] && isset($match[2][$i]) && $match[2][$i]) {
94 3
                $columns[] = TableCell::ALIGN_CENTER;
95 14
            } elseif (isset($match[1][$i]) && $match[1][$i]) {
96 3
                $columns[] = TableCell::ALIGN_LEFT;
97 14
            } elseif (isset($match[2][$i]) && $match[2][$i]) {
98 3
                $columns[] = TableCell::ALIGN_RIGHT;
99
            } else {
100 14
                $columns[] = null;
101
            }
102
        }
103
104 14
        return $columns;
105
    }
106
107 14
    private function parseRow($line, array $columns, $type = TableCell::TYPE_BODY)
108
    {
109 14
        $cells = RegexHelper::matchAll(self::REGEXP_CELLS, $line);
110
111 14
        if (null === $cells) {
112 3
            return;
113
        }
114
115
        // $cells[0] is going to be either an array of matches or a single match
116
        // Convert single match to array of matches to allow single-column tables
117 14
        if (!is_array($cells[0])) {
118 3
            $cells = [$cells];
119
        }
120
121 14
        $row = new TableRow();
122 14
        foreach ($cells[0] as $i => $cell) {
123 14
            $row->appendChild(new TableCell(trim($cell), $type, isset($columns[$i]) ? $columns[$i] : null));
124
        }
125
126 14
        for ($j = count($columns) - 1; $j > $i; --$j) {
0 ignored issues
show
Bug introduced by
The variable $i seems to be defined by a foreach iteration on line 122. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
127 3
            $row->appendChild(new TableCell('', $type, null));
128
        }
129
130 14
        return $row;
131
    }
132
133 3
    private function parseCaption($line)
134
    {
135 3
        $caption = RegexHelper::matchAll(self::REGEXP_CAPTION, $line);
136
137 3
        if (null === $caption) {
138 3
            return;
139
        }
140
141
        return new TableCaption($caption[1], $caption[2]);
142
    }
143
}
144