RstBuilder   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 18

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getContent() 0 2 1
A escape() 0 4 1
A indent() 0 3 1
A addFieldList() 0 4 1
A unindent() 0 4 1
A addLine() 0 3 1
A addH3() 0 4 1
A add() 0 3 1
A addH1() 0 4 1
B addMultiline() 0 13 6
A addH2() 0 4 1
A addMultilineWithoutRendering() 0 7 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace JuliusHaertl\PHPDocToRst\Builder;
25
26
27
use phpDocumentor\Reflection\Fqsen;
28
29
/**
30
 * Helper class to build reStructuredText files
31
 *
32
 * @package JuliusHaertl\PHPDocToRst\Builder
33
 */
34
class RstBuilder {
35
36
    private $indentLevel = 0;
37
    /** @var string */
38
    protected $content = '.. rst-class:: phpdoctorst' . PHP_EOL . PHP_EOL ;
39
40
    public function getContent() {
41
        return $this->content;
42
    }
43
44
    public static function escape($text) {
45
        // escape all common reStructuredText control chars
46
        $text = preg_replace("/[\'\`\*\_\{\}\[\]\(\)\|\>\#\+\-\.\\\!]/", '\\\\$0', $text);
47
        return $text;
48
    }
49
50
    public function indent() {
51
        $this->indentLevel++;
52
        return $this;
53
    }
54
55
    public function unindent() {
56
        $this->indentLevel--;
57
        $this->addLine();
58
        return $this;
59
    }
60
61
    public function addFieldList($key, $value) {
62
        $this->addLine(':'.self::escape($key).':');
63
        $this->indent()->addMultiline($value, false)->unindent();
64
        return $this;
65
    }
66
67
    /**
68
     * @param string $text
69
     * @return $this
70
     */
71
    public function addH1(string $text) {
72
        $this->addLine($text);
73
        $this->addLine(str_repeat('=', strlen((string)$text)))->addLine();
74
        return $this;
75
    }
76
77
    public function addH2($text) {
78
        $this->addLine($text);
79
        $this->addLine(str_repeat('-', strlen((string)$text)))->addLine();
80
        return $this;
81
    }
82
83
    public function addH3($text) {
84
        $this->addLine($text);
85
        $this->addLine(str_repeat('~', strlen((string)$text)))->addLine();
86
        return $this;
87
    }
88
89
    public function addLine($text = '') {
90
        $this->add(str_repeat("\t", $this->indentLevel) . $text . PHP_EOL);
91
        return $this;
92
    }
93
94
    public function addMultiline($text = '', $blockIndent = false) {
95
        $lines = preg_split('/$\R?^/m', $text);
96
        $i = 0;
97
        foreach ($lines as $line) {
98
            if ($blockIndent && $i++ === 1) {
99
                $this->indent();
100
            }
101
            $this->addLine($line);
102
        }
103
        if ($blockIndent && count($lines) > 1) {
0 ignored issues
show
Bug introduced by
It seems like $lines can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

103
        if ($blockIndent && count(/** @scrutinizer ignore-type */ $lines) > 1) {
Loading history...
104
            $this->unindent();
105
        }
106
        return $this;
107
    }
108
109
    public function addMultilineWithoutRendering($text) {
110
        $lines = preg_split('/$\R?^/m', $text);
111
        foreach ($lines as $line) {
112
            $this->addLine('| ' . $line);
113
        }
114
115
        return $this;
116
    }
117
118
    public function add($text) {
119
        $this->content .= $text;
120
        return $this;
121
    }
122
123
124
}