Completed
Push — master ( 0b0b24...54ff37 )
by Colin
02:37
created

NewlineParser::parse()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 13
cts 13
cp 1
rs 9.552
c 0
b 0
f 0
cc 4
nc 6
nop 1
crap 4
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Parser\Inline;
16
17
use League\CommonMark\Node\Inline\Newline;
18
use League\CommonMark\Node\Inline\Text;
19
use League\CommonMark\Parser\InlineParserContext;
20
21
final class NewlineParser implements InlineParserInterface
22
{
23 2490
    public function getCharacters(): array
24
    {
25 2490
        return ["\n"];
26
    }
27
28 438
    public function parse(InlineParserContext $inlineContext): bool
29
    {
30 438
        $inlineContext->getCursor()->advanceBy(1);
31
32
        // Check previous inline for trailing spaces
33 438
        $spaces = 0;
34 438
        $lastInline = $inlineContext->getContainer()->lastChild();
35 438
        if ($lastInline instanceof Text) {
36 399
            $trimmed = \rtrim($lastInline->getContent(), ' ');
37 399
            $spaces = \strlen($lastInline->getContent()) - \strlen($trimmed);
38 399
            if ($spaces) {
39 30
                $lastInline->setContent($trimmed);
40
            }
41
        }
42
43 438
        if ($spaces >= 2) {
44 21
            $inlineContext->getContainer()->appendChild(new Newline(Newline::HARDBREAK));
45
        } else {
46 420
            $inlineContext->getContainer()->appendChild(new Newline(Newline::SOFTBREAK));
47
        }
48
49 438
        return true;
50
    }
51
}
52