Passed
Push — markdown ( d911a6 )
by Greg
10:14
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 Fisharebest\Webtrees\Census\Census;
23
use League\CommonMark\Extension\Table\Table;
24
use League\CommonMark\Extension\Table\TableCell;
25
use League\CommonMark\Extension\Table\TableRow;
26
use League\CommonMark\Extension\Table\TableSection;
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 explode;
34
use function str_starts_with;
35
use function strlen;
36
use function substr;
37
38
/**
39
 * Convert webtrees 1.x census-assistant markup into tables.
40
 * Note that webtrees 2.0 generates markdown tables directly.
41
 */
42
class CensusTableContinueParser extends AbstractBlockContinueParser
43
{
44
    private Table $table;
45
46
    private TableSection $head;
47
48
    private TableSection $body;
49
50
    public function __construct()
51
    {
52
        $this->table = new Table();
53
        $this->head  = new TableSection(TableSection::TYPE_HEAD);
54
        $this->body  = new TableSection(TableSection::TYPE_BODY);
55
56
        $this->table->appendChild($this->head);
57
        $this->table->appendChild($this->body);
58
    }
59
60
    /**
61
     * @param Cursor                       $cursor
62
     * @param BlockContinueParserInterface $activeBlockParser
63
     *
64
     * @return BlockContinue|null
65
     */
66
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
67
    {
68
        $line = $cursor->getLine();
69
70
        if ($line === CensusTableExtension::CA_SUFFIX) {
71
            return BlockContinue::finished();
72
        }
73
74
        // Blank line before the suffix is an error.
75
        if ($line === '') {
76
            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...
77
        }
78
79
        $cells = explode('|', $line);
80
81
        $table_row = new TableRow();
82
83
        if (empty($this->head->children())) {
84
            foreach ($cells as $text) {
85
                if (str_starts_with($text, CensusTableExtension::TH_PREFIX)) {
86
                    $text = substr($text, strlen(CensusTableExtension::TH_PREFIX));
87
                }
88
                $table_cell = new TableCell(TableCell::TYPE_HEADER);
89
                $table_cell->appendChild(new Text($text));
90
            }
91
92
            $this->head->appendChild($table_row);
93
        } else {
94
            foreach ($cells as $text) {
95
                $table_cell = new TableCell(TableCell::TYPE_DATA);
96
                $table_cell->appendChild(new Text($text));
97
            }
98
            $this->body->appendChild($table_row);
99
        }
100
101
        return BlockContinue::at($cursor);
102
    }
103
104
    /**
105
     * @return Table
106
     */
107
    public function getBlock(): Table
108
    {
109
        $table = new Table();
110
        $table->appendChild($this->head);
111
        $table->appendChild($this->body);
112
113
        return $table;
114
    }
115
}
116