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 | private int $maxAutocompletedCells; |
||
27 | |||
28 | public function __construct(int $maxAutocompletedCells = TableParser::DEFAULT_MAX_AUTOCOMPLETED_CELLS) |
||
0 ignored issues
–
show
|
|||
29 | { |
||
30 | $this->maxAutocompletedCells = $maxAutocompletedCells; |
||
31 | } |
||
32 | |||
33 | public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart |
||
34 | { |
||
35 | $paragraph = $parserState->getParagraphContent(); |
||
36 | if ($paragraph === null || \strpos($paragraph, '|') === false) { |
||
37 | return BlockStart::none(); |
||
38 | } |
||
39 | |||
40 | $columns = self::parseSeparator($cursor); |
||
41 | if (\count($columns) === 0) { |
||
42 | return BlockStart::none(); |
||
43 | } |
||
44 | |||
45 | $lastLineBreak = \strrpos($paragraph, "\n"); |
||
46 | $lastLine = $lastLineBreak === false ? $paragraph : \substr($paragraph, $lastLineBreak + 1); |
||
47 | |||
48 | $headerCells = TableParser::split($lastLine); |
||
49 | if (\count($headerCells) > \count($columns)) { |
||
50 | return BlockStart::none(); |
||
51 | } |
||
52 | |||
53 | $cursor->advanceToEnd(); |
||
54 | |||
55 | $parsers = []; |
||
56 | |||
57 | if ($lastLineBreak !== false) { |
||
58 | $p = new ParagraphParser(); |
||
59 | $p->addLine(\substr($paragraph, 0, $lastLineBreak)); |
||
60 | $parsers[] = $p; |
||
61 | } |
||
62 | |||
63 | $parsers[] = new TableParser($columns, $headerCells, $this->maxAutocompletedCells); |
||
64 | |||
65 | return BlockStart::of(...$parsers) |
||
66 | ->at($cursor) |
||
67 | ->replaceActiveBlockParser(); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @return array<int, string|null> |
||
72 | * |
||
73 | * @psalm-return array<int, TableCell::ALIGN_*|null> |
||
74 | * |
||
75 | * @phpstan-return array<int, TableCell::ALIGN_*|null> |
||
76 | */ |
||
77 | private static function parseSeparator(Cursor $cursor): array |
||
78 | { |
||
79 | $columns = []; |
||
80 | $pipes = 0; |
||
81 | $valid = false; |
||
82 | |||
83 | while (! $cursor->isAtEnd()) { |
||
84 | switch ($c = $cursor->getCurrentCharacter()) { |
||
85 | case '|': |
||
86 | $cursor->advanceBy(1); |
||
87 | $pipes++; |
||
88 | if ($pipes > 1) { |
||
89 | // More than one adjacent pipe not allowed |
||
90 | return []; |
||
91 | } |
||
92 | |||
93 | // Need at least one pipe, even for a one-column table |
||
94 | $valid = true; |
||
95 | break; |
||
96 | case '-': |
||
97 | case ':': |
||
98 | if ($pipes === 0 && \count($columns) > 0) { |
||
99 | // Need a pipe after the first column (first column doesn't need to start with one) |
||
100 | return []; |
||
101 | } |
||
102 | |||
103 | $left = false; |
||
104 | $right = false; |
||
105 | if ($c === ':') { |
||
106 | $left = true; |
||
107 | $cursor->advanceBy(1); |
||
108 | } |
||
109 | |||
110 | if ($cursor->match('/^-+/') === null) { |
||
111 | // Need at least one dash |
||
112 | return []; |
||
113 | } |
||
114 | |||
115 | if ($cursor->getCurrentCharacter() === ':') { |
||
116 | $right = true; |
||
117 | $cursor->advanceBy(1); |
||
118 | } |
||
119 | |||
120 | $columns[] = self::getAlignment($left, $right); |
||
121 | // Next, need another pipe |
||
122 | $pipes = 0; |
||
123 | break; |
||
124 | case ' ': |
||
125 | case "\t": |
||
126 | // White space is allowed between pipes and columns |
||
127 | $cursor->advanceToNextNonSpaceOrTab(); |
||
128 | break; |
||
129 | default: |
||
130 | // Any other character is invalid |
||
131 | return []; |
||
132 | } |
||
133 | } |
||
134 | |||
135 | if (! $valid) { |
||
136 | return []; |
||
137 | } |
||
138 | |||
139 | return $columns; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @psalm-return TableCell::ALIGN_*|null |
||
144 | * |
||
145 | * @phpstan-return TableCell::ALIGN_*|null |
||
146 | * |
||
147 | * @psalm-pure |
||
148 | */ |
||
149 | private static function getAlignment(bool $left, bool $right): ?string |
||
150 | { |
||
151 | if ($left && $right) { |
||
152 | return TableCell::ALIGN_CENTER; |
||
153 | } |
||
154 | |||
155 | if ($left) { |
||
156 | return TableCell::ALIGN_LEFT; |
||
157 | } |
||
158 | |||
159 | if ($right) { |
||
160 | return TableCell::ALIGN_RIGHT; |
||
161 | } |
||
162 | |||
163 | return null; |
||
164 | } |
||
165 | } |
||
166 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths