Passed
Push — render-xml ( 61b7f8 )
by Colin
02:12
created

TableStartParser::parseSeparator()   C

Complexity

Conditions 14
Paths 32

Size

Total Lines 63
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 14.2288

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 63
ccs 34
cts 38
cp 0.8947
rs 6.2666
cc 14
nc 32
nop 1
crap 14.2288

How to fix   Long Method    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
declare(strict_types=1);
4
5
/*
6
 * This is part of the league/commonmark package.
7
 *
8
 * (c) Martin Hasoň <[email protected]>
9
 * (c) Webuni s.r.o. <[email protected]>
10
 * (c) Colin O'Dell <[email protected]>
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 */
15
16
namespace League\CommonMark\Extension\Table;
17
18
use League\CommonMark\Parser\Block\BlockStart;
19
use League\CommonMark\Parser\Block\BlockStartParserInterface;
20
use League\CommonMark\Parser\Block\ParagraphParser;
21
use League\CommonMark\Parser\Cursor;
22
use League\CommonMark\Parser\MarkdownParserStateInterface;
23
24
final class TableStartParser implements BlockStartParserInterface
25
{
26 63
    public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
27
    {
28 63
        $paragraph = $parserState->getParagraphContent();
29 63
        if ($paragraph === null || \strpos($paragraph, '|') === false) {
30 60
            return BlockStart::none();
31
        }
32
33 33
        $columns = self::parseSeparator($cursor);
34 33
        if (\count($columns) === 0) {
35 3
            return BlockStart::none();
36
        }
37
38 33
        $lines    = \explode("\n", $paragraph);
39 33
        $lastLine = \array_pop($lines);
40
41 33
        $headerCells = TableParser::split($lastLine);
42 33
        if (\count($headerCells) > \count($columns)) {
43 3
            return BlockStart::none();
44
        }
45
46 30
        $cursor->advanceToEnd();
47
48 30
        $parsers = [];
49
50 30
        if (\count($lines) > 0) {
51
            $p = new ParagraphParser();
52
            $p->addLine(\implode("\n", $lines));
53
            $parsers[] = $p;
54
        }
55
56 30
        $parsers[] = new TableParser($columns, $headerCells);
57
58 30
        return BlockStart::of(...$parsers)
59 30
            ->at($cursor)
60 30
            ->replaceActiveBlockParser();
61
    }
62
63
    /**
64
     * @return array<int, string|null>
65
     *
66
     * @psalm-return array<int, TableCell::ALIGN_*|null>
67
     *
68
     * @phpstan-return array<int, TableCell::ALIGN_*|null>
69
     */
70 33
    private static function parseSeparator(Cursor $cursor): array
71
    {
72 33
        $columns = [];
73 33
        $pipes   = 0;
74 33
        $valid   = false;
75
76 33
        while (! $cursor->isAtEnd()) {
77 33
            switch ($c = $cursor->getCharacter()) {
78 33
                case '|':
79 33
                    $cursor->advanceBy(1);
80 33
                    $pipes++;
81 33
                    if ($pipes > 1) {
82
                        // More than one adjacent pipe not allowed
83
                        return [];
84
                    }
85
86
                    // Need at least one pipe, even for a one-column table
87 33
                    $valid = true;
88 33
                    break;
89 33
                case '-':
90 30
                case ':':
91 33
                    if ($pipes === 0 && \count($columns) > 0) {
92
                        // Need a pipe after the first column (first column doesn't need to start with one)
93
                        return [];
94
                    }
95
96 33
                    $left  = false;
97 33
                    $right = false;
98 33
                    if ($c === ':') {
99 6
                        $left = true;
100 6
                        $cursor->advanceBy(1);
101
                    }
102
103 33
                    if ($cursor->match('/^-+/') === null) {
104
                        // Need at least one dash
105
                        return [];
106
                    }
107
108 33
                    if ($cursor->getCharacter() === ':') {
109 6
                        $right = true;
110 6
                        $cursor->advanceBy(1);
111
                    }
112
113 33
                    $columns[] = self::getAlignment($left, $right);
114
                    // Next, need another pipe
115 33
                    $pipes = 0;
116 33
                    break;
117 30
                case ' ':
118 3
                case "\t":
119
                    // White space is allowed between pipes and columns
120 30
                    $cursor->advanceToNextNonSpaceOrTab();
121 30
                    break;
122
                default:
123
                    // Any other character is invalid
124 3
                    return [];
125
            }
126
        }
127
128 33
        if (! $valid) {
129
            return [];
130
        }
131
132 33
        return $columns;
133
    }
134
135
    /**
136
     * @psalm-return TableCell::ALIGN_*|null
137
     *
138
     * @phpstan-return TableCell::ALIGN_*|null
139
     *
140
     * @psalm-pure
141
     */
142 33
    private static function getAlignment(bool $left, bool $right): ?string
143
    {
144 33
        if ($left && $right) {
145 6
            return TableCell::ALIGN_CENTER;
146
        }
147
148 33
        if ($left) {
149 3
            return TableCell::ALIGN_LEFT;
150
        }
151
152 33
        if ($right) {
153 6
            return TableCell::ALIGN_RIGHT;
154
        }
155
156 30
        return null;
157
    }
158
}
159