Completed
Pull Request — master (#7)
by
unknown
02:32 queued 36s
created

TableParser::parseRow()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 14
cts 14
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 14
nc 10
nop 3
crap 7
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 17
    public function parse(ContextInterface $context, Cursor $cursor)
28
    {
29 17
        $container = $context->getContainer();
30
31 17
        if (!$container instanceof Paragraph) {
32 17
            return false;
33
        }
34
35 17
        $lines = $container->getStrings();
36 17
        if (count($lines) < 1) {
37
            return false;
38
        }
39
40 17
        $match = RegexHelper::matchAll(self::REGEXP_DEFINITION, $cursor->getLine(), $cursor->getFirstNonSpacePosition());
41 17
        if (null === $match) {
42 1
            return false;
43
        }
44
45 17
        $columns = $this->parseColumns($match);
46 17
        $head = $this->parseRow(trim(array_pop($lines)), $columns, TableCell::TYPE_HEAD);
47 17
        if (null === $head) {
48
            return false;
49
        }
50
51 17
        $table = new Table(function (Cursor $cursor) use (&$table, $columns) {
52 17
            $row = $this->parseRow($cursor->getLine(), $columns);
53 17
            if (null === $row) {
54 5
                if (null !== $table->getCaption()) {
55 2
                    return false;
56
                }
57
58 5
                if (null !== ($caption = $this->parseCaption($cursor->getLine()))) {
59 3
                    $table->setCaption($caption);
60
61 3
                    return true;
62
                }
63
64 2
                return false;
65
            }
66
67 17
            $table->getBody()->appendChild($row);
68
69 17
            return true;
70 17
        });
71
72 17
        $table->getHead()->appendChild($head);
73
74 17
        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 17
            $context->replaceContainerBlock($table);
84
        }
85
86 17
        return true;
87
    }
88
89 17
    private function parseColumns(array $match)
90
    {
91 17
        $columns = [];
92 17
        foreach ((array) $match[0] as $i => $column) {
93 17
            if (isset($match[1][$i]) && $match[1][$i] && isset($match[2][$i]) && $match[2][$i]) {
94 3
                $columns[] = TableCell::ALIGN_CENTER;
95 17
            } elseif (isset($match[1][$i]) && $match[1][$i]) {
96 3
                $columns[] = TableCell::ALIGN_LEFT;
97 17
            } elseif (isset($match[2][$i]) && $match[2][$i]) {
98 3
                $columns[] = TableCell::ALIGN_RIGHT;
99
            } else {
100 17
                $columns[] = null;
101
            }
102
        }
103
104 17
        return $columns;
105
    }
106
107 17
    private function parseRow($line, array $columns, $type = TableCell::TYPE_BODY)
108
    {
109 17
        $cells = RegexHelper::matchAll(self::REGEXP_CELLS, $line);
110
111 17
        if (null === $cells) {
112 4
            return;
113
        }
114
115
        // If we have a single match we might be using a single-column table
116 17
        if (!is_array($cells[0])) {
117
            // Single-column table cells that have [body] content must be prefixed with | to not be treated as captions
118 6
            if (null !== $this->parseCaption($line)) {
119 3
                return;
120
            }
121
122
            // Fake single match as array of matches for the code below
123 5
            $cells = [$cells];
124
        }
125
126 17
        $row = new TableRow();
127 17
        foreach ($cells[0] as $i => $cell) {
128 17
            $row->appendChild(new TableCell(trim($cell), $type, isset($columns[$i]) ? $columns[$i] : null));
129
        }
130
131 17
        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 127. 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...
132 4
            $row->appendChild(new TableCell('', $type, null));
133
        }
134
135 17
        return $row;
136
    }
137
138 7
    private function parseCaption($line)
139
    {
140 7
        $caption = RegexHelper::matchAll(self::REGEXP_CAPTION, $line);
141
142 7
        if (null === $caption) {
143 6
            return;
144
        }
145
146 3
        return new TableCaption($caption[1], $caption[2]);
147
    }
148
}
149