Issues (2502)

app/CommonMark/CensusTableContinueParser.php (2 issues)

Labels
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\CommonMark;
21
22
use League\CommonMark\Extension\Table\Table;
23
use League\CommonMark\Extension\Table\TableCell;
24
use League\CommonMark\Extension\Table\TableRow;
25
use League\CommonMark\Extension\Table\TableSection;
26
use League\CommonMark\Node\Block\AbstractBlock;
27
use League\CommonMark\Node\Inline\Text;
28
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
29
use League\CommonMark\Parser\Block\BlockContinue;
30
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
31
use League\CommonMark\Parser\Cursor;
32
33
use function array_map;
34
use function explode;
35
use function iterator_to_array;
36
use function str_starts_with;
37
use function strlen;
38
use function substr;
39
40
/**
41
 * Convert webtrees 1.x census-assistant markup into tables.
42
 * Note that webtrees 2.0 generates markdown tables directly.
43
 */
44
class CensusTableContinueParser extends AbstractBlockContinueParser
45
{
46
    private Table $table;
47
48
    private TableSection $thead;
49
50
    private TableSection $tbody;
51
52
    /**
53
     * Constructor
54
     */
55
    public function __construct()
56
    {
57
        $this->table = new Table();
58
        $this->thead = new TableSection(TableSection::TYPE_HEAD);
59
        $this->tbody = new TableSection(TableSection::TYPE_BODY);
60
        $this->table->appendChild($this->thead);
61
        $this->table->appendChild($this->tbody);
62
    }
63
64
    /**
65
     * @param Cursor                       $cursor
66
     * @param BlockContinueParserInterface $activeBlockParser
67
     *
68
     * @return BlockContinue|null
69
     */
70
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): BlockContinue|null
71
    {
72
        $line = $cursor->getLine();
73
74
        if ($line === CensusTableExtension::CA_SUFFIX) {
0 ignored issues
show
The type Fisharebest\Webtrees\Com...rk\CensusTableExtension was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
75
            return BlockContinue::finished();
76
        }
77
78
        // Blank line before the suffix is an error.
79
        if ($line === '') {
80
            return BlockContinue::none();
81
        }
82
83
        $cells = explode('|', $line);
84
85
        $callback = static function (string $text): string {
86
            if (str_starts_with($text, CensusTableExtension::TH_PREFIX)) {
87
                return substr($text, strlen(CensusTableExtension::TH_PREFIX));
88
            }
89
90
            return $text;
91
        };
92
93
        $tr = new TableRow();
94
95
        if (iterator_to_array($this->thead->children()) === []) {
0 ignored issues
show
$this->thead->children() of type League\CommonMark\Node\Node[]|array is incompatible with the type Traversable expected by parameter $iterator of iterator_to_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
        if (iterator_to_array(/** @scrutinizer ignore-type */ $this->thead->children()) === []) {
Loading history...
96
            $cells = array_map($callback, $cells);
97
98
            foreach ($cells as $cell) {
99
                $table_cell = new TableCell(TableCell::TYPE_HEADER);
100
                $table_cell->appendChild(new Text($cell));
101
                $tr->appendChild($table_cell);
102
            }
103
104
            $this->thead->appendChild($tr);
105
        } else {
106
            foreach ($cells as $cell) {
107
                $table_cell = new TableCell(TableCell::TYPE_DATA);
108
                $table_cell->appendChild(new Text($cell));
109
                $tr->appendChild($table_cell);
110
            }
111
112
            $this->tbody->appendChild($tr);
113
        }
114
115
        return BlockContinue::at($cursor);
116
    }
117
118
    /**
119
     * @return Table
120
     */
121
    public function getBlock(): AbstractBlock
122
    {
123
        return $this->table;
124
    }
125
}
126