Completed
Push — master ( e1e275...3d6629 )
by Martin
20:32
created

TableParser   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 28
c 5
b 1
f 1
lcom 1
cbo 9
dl 0
loc 116
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
C parse() 0 61 10
B parseColumns() 0 17 10
B parseRow() 0 18 6
A parseCaption() 0 10 2
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
    public function parse(ContextInterface $context, Cursor $cursor)
28
    {
29
        $container = $context->getContainer();
30
31
        if (!$container instanceof Paragraph) {
32
            return false;
33
        }
34
35
        $lines = $container->getStrings();
36
        if (count($lines) < 1) {
37
            return false;
38
        }
39
40
        $match = RegexHelper::matchAll(self::REGEXP_DEFINITION, $cursor->getLine(), $cursor->getFirstNonSpacePosition());
41
        if (null === $match) {
42
            return false;
43
        }
44
45
        $columns = $this->parseColumns($match);
46
        $head = $this->parseRow(trim(array_pop($lines)), $columns, TableCell::TYPE_HEAD);
47
        if (null === $head) {
48
            return false;
49
        }
50
51
        $table = new Table(function (Cursor $cursor) use (&$table, $columns) {
52
            $row = $this->parseRow($cursor->getLine(), $columns);
53
            if (null === $row) {
54
                if (null !== $table->getCaption()) {
55
                    return false;
56
                }
57
58
                if (null !== ($caption = $this->parseCaption($cursor->getLine()))) {
59
                    $table->setCaption($caption);
60
61
                    return true;
62
                }
63
64
                return false;
65
            }
66
67
            $table->getBody()->appendChild($row);
68
69
            return true;
70
        });
71
72
        $table->getHead()->appendChild($head);
73
74
        if (count($lines) >= 1) {
75
            $paragraph = new Paragraph();
76
            foreach ($lines as $line) {
77
                $paragraph->addLine($line);
78
            }
79
80
            $context->replaceContainerBlock($paragraph);
81
            $context->addBlock($table);
82
        } else {
83
            $context->replaceContainerBlock($table);
84
        }
85
86
        return true;
87
    }
88
89
    private function parseColumns(array $match)
90
    {
91
        $columns = [];
92
        foreach ((array) $match[0] as $i => $column) {
93
            if (isset($match[1][$i]) && $match[1][$i] && isset($match[2][$i]) && $match[2][$i]) {
94
                $columns[] = TableCell::ALIGN_CENTER;
95
            } elseif (isset($match[1][$i]) && $match[1][$i]) {
96
                $columns[] = TableCell::ALIGN_LEFT;
97
            } elseif (isset($match[2][$i]) && $match[2][$i]) {
98
                $columns[] = TableCell::ALIGN_RIGHT;
99
            } else {
100
                $columns[] = null;
101
            }
102
        }
103
104
        return $columns;
105
    }
106
107
    private function parseRow($line, array $columns, $type = TableCell::TYPE_BODY)
108
    {
109
        $cells = RegexHelper::matchAll(self::REGEXP_CELLS, $line);
110
        if (null === $cells || !is_array($cells[0])) {
111
            return;
112
        }
113
114
        $row = new TableRow();
115
        foreach ($cells[0] as $i => $cell) {
116
            $row->appendChild(new TableCell(trim($cell), $type, isset($columns[$i]) ? $columns[$i] : null));
117
        }
118
119
        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 115. 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...
120
            $row->appendChild(new TableCell('', $type, null));
121
        }
122
123
        return $row;
124
    }
125
126
    private function parseCaption($line)
127
    {
128
        $caption = RegexHelper::matchAll(self::REGEXP_CAPTION, $line);
129
130
        if (null === $caption) {
131
            return null;
132
        }
133
134
        return new TableCaption($caption[1], $caption[2]);
135
    }
136
}
137