Passed
Push — markdown ( d911a6...8b10f6 )
by Greg
06:43
created

CensusTableContinueParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 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\Block\Paragraph;
28
use League\CommonMark\Node\Inline\Text;
29
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
30
use League\CommonMark\Parser\Block\BlockContinue;
31
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
32
use League\CommonMark\Parser\Cursor;
33
34
use function explode;
35
use function str_starts_with;
36
use function strlen;
37
use function substr;
38
39
/**
40
 * Convert webtrees 1.x census-assistant markup into tables.
41
 * Note that webtrees 2.0 generates markdown tables directly.
42
 */
43
class CensusTableContinueParser extends AbstractBlockContinueParser
44
{
45
    /** @var array<int,string> */
46
    private array $head = [];
47
48
    /** @var array<int,array<int,string>> */
49
    private array $body = [];
50
51
    /**
52
     * @param Cursor                       $cursor
53
     * @param BlockContinueParserInterface $activeBlockParser
54
     *
55
     * @return BlockContinue|null
56
     */
57
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
58
    {
59
        $line = $cursor->getLine();
60
61
        if ($line === CensusTableExtension::CA_SUFFIX) {
62
            return BlockContinue::finished();
63
        }
64
65
        // Blank line before the suffix is an error.
66
        if ($line === '') {
67
            return BlockContinue::none();
1 ignored issue
show
Bug introduced by
Are you sure the usage of League\CommonMark\Parser...k\BlockContinue::none() targeting League\CommonMark\Parser...k\BlockContinue::none() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
68
        }
69
70
        $cells = explode('|', $line);
71
72
        $callback = static function (string $text): string {
73
            if (str_starts_with($text, CensusTableExtension::TH_PREFIX)) {
74
                return substr($text, strlen(CensusTableExtension::TH_PREFIX));
75
            }
76
77
            return $text;
78
        };
79
80
        if ($this->head === []) {
81
            $this->head = array_map($callback, $cells);
82
        } else {
83
            $this->body[] = $cells;
84
        }
85
86
        return BlockContinue::at($cursor);
87
    }
88
89
    /**
90
     * @return Table
91
     */
92
    public function getBlock(): AbstractBlock
93
    {
94
        //$para = new Paragraph(); $para->appendChild(new Text('foo!')); return $para;
95
96
        $table = new Table();
97
        $head  = new TableSection(TableSection::TYPE_HEAD);
98
        $body  = new TableSection(TableSection::TYPE_BODY);
99
100
        $tr = new TableRow();
101
102
        foreach ($this->head as $th) {
103
            $table_cell = new TableCell(TableCell::TYPE_HEADER);
104
            $table_cell->appendChild(new Text($th));
105
            $tr->appendChild($table_cell);
106
        }
107
108
        $head->appendChild($tr);
109
110
        foreach ($this->body as $cells) {
111
            $tr = new TableRow();
112
113
            foreach ($cells as $td) {
114
                $table_cell = new TableCell(TableCell::TYPE_HEADER);
115
                $table_cell->appendChild(new Text($td));
116
                $tr->appendChild($table_cell);
117
            }
118
119
            $body->appendChild($tr);
120
        }
121
122
        $table->appendChild($head);
123
        $table->appendChild($body);
124
125
        return $table;
126
    }
127
}
128