Passed
Push — feature/6.x ( 53903f...32797a )
by Schlaefer
05:38 queued 02:15
created

Markup::citeText()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 58
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 35
nc 9
nop 1
dl 0
loc 58
rs 8.1155
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Saito - The Threaded Web Forum
6
 *
7
 * @copyright Copyright (c) the Saito Project Developers
8
 * @link https://github.com/Schlaefer/Saito
9
 * @license http://opensource.org/licenses/MIT
10
 */
11
12
namespace BbcodeParser\Lib;
13
14
use App\View\Helper\ParserHelper;
15
use Saito\Markup\MarkupInterface;
16
use Saito\Markup\MarkupSettings;
17
18
class Markup implements MarkupInterface
19
{
20
    /**
21
     * @var \BbcodeParser\Lib\Editor|null
22
     */
23
    protected $editor;
24
    /**
25
     * @var \BbcodeParser\Lib\Parser|null
26
     */
27
    protected $parser;
28
    /**
29
     * @var \BbcodeParser\Lib\Preprocessor|null
30
     */
31
    protected $preproccesor;
32
    /**
33
     * @var \Saito\Markup\MarkupSettings|null
34
     */
35
    protected $settings;
36
37
    /**
38
     * {@inheritDoc}
39
     */
40
    public function __construct(MarkupSettings $settings)
41
    {
42
        $this->settings = $settings;
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    public function getEditorHelp(ParserHelper $helper): string
49
    {
50
        return $this->getEditor()->getEditorHelp($helper);
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    public function getMarkupSet(): array
57
    {
58
        return $this->getEditor()->getMarkupSet();
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    public function parse(string $string, ParserHelper $helper, array $options = []): string
65
    {
66
        if (!$this->parser) {
67
            $this->parser = new Parser($helper, $this->settings);
68
        }
69
70
        return $this->parser->parse($string, $options);
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    public function preprocess($string): string
77
    {
78
        if (!$this->preproccesor) {
79
            $this->preproccesor = new Preprocessor($this->settings);
80
        }
81
82
        return $this->preproccesor->process($string);
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88
    public function citeText(string $string): string
89
    {
90
        if (empty($string)) {
91
            return '';
92
        }
93
        $quoteSymbol = $this->settings->get('quote_symbol');
94
        $out = '';
95
        // split already quoted lines
96
        $citeLines = preg_split(
97
            "/(^{$quoteSymbol}.*?$\n)/m",
98
            $string,
99
            -1,
100
            PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
101
        );
102
        foreach ($citeLines as $citeLine) {
103
            if (mb_strpos($citeLine, $quoteSymbol) === 0) {
104
                // already quoted lines need no further processing
105
                $out .= $citeLine;
106
                continue;
107
            }
108
            // split [bbcode]
109
            $matches = preg_split(
110
                '`(\[(.+?)=?.*?\].+?\[/\2\])`',
111
                $citeLine,
112
                -1,
113
                PREG_SPLIT_DELIM_CAPTURE
114
            );
115
            $i = 0;
116
            $line = '';
117
            foreach ($matches as $match) {
118
                // the [bbcode] preg_split uses a backreference \2 which is in the $matches
119
                // but is not needed in the results
120
                // @td @sm elegant solution
121
                $i++;
122
                if ($i % 3 == 0) {
123
                    continue;
124
                }
125
                // wrap long lines
126
                if (mb_strpos($match, '[') !== 0) {
127
                    $line .= wordwrap($match);
128
                } else {
129
                    $line .= $match;
130
                }
131
                // add newline to wrapped lines
132
                if (mb_strlen($line) > 60) {
133
                    $out .= $line . "\n";
134
                    $line = '';
135
                }
136
            }
137
            $out .= $line;
138
        }
139
        $out = preg_replace(
140
            "/^/m",
141
            $quoteSymbol . " ",
142
            $out
143
        );
144
145
        return $out;
146
    }
147
148
    /**
149
     * Get editor
150
     *
151
     * @return \BbcodeParser\Lib\Editor
152
     */
153
    protected function getEditor(): Editor
154
    {
155
        if (!$this->editor) {
156
            $this->editor = new Editor();
157
        }
158
159
        return $this->editor;
160
    }
161
162
    /**
163
     * Reset class
164
     *
165
     * @return void
166
     */
167
    protected function reset()
168
    {
169
        $this->editor = null;
170
        $this->parser = null;
171
        $this->preproccesor = null;
172
    }
173
}
174