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

NewlineParser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 5
dl 0
loc 31
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCharacters() 0 4 1
A parse() 0 23 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