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

Paragraph   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 5
dl 0
loc 73
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A canContain() 0 4 1
A isCode() 0 4 1
A matchesNextLine() 0 10 2
A finalize() 0 21 5
A parseReferences() 0 10 3
A handleRemainingContents() 0 8 1
A getStrings() 0 4 1
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\Node\Block;
16
17
use League\CommonMark\Parser\ContextInterface;
18
use League\CommonMark\Parser\Cursor;
19
20
class Paragraph extends AbstractStringContainerBlock implements InlineContainerInterface
21
{
22 75
    public function canContain(AbstractBlock $block): bool
23
    {
24 75
        return false;
25
    }
26
27 489
    public function isCode(): bool
28
    {
29 489
        return false;
30
    }
31
32 882
    public function matchesNextLine(Cursor $cursor): bool
33
    {
34 882
        if ($cursor->isBlank()) {
35 504
            $this->lastLineBlank = true;
36
37 504
            return false;
38
        }
39
40 489
        return true;
41
    }
42
43 2028
    public function finalize(ContextInterface $context, int $endLineNumber): void
44
    {
45 2028
        parent::finalize($context, $endLineNumber);
46
47 2028
        $this->finalStringContents = \preg_replace('/^  */m', '', \implode("\n", $this->getStrings()));
48
49
        // Short-circuit
50 2028
        if ($this->finalStringContents === '' || $this->finalStringContents[0] !== '[') {
51 1671
            return;
52
        }
53
54 432
        $cursor = new Cursor($this->finalStringContents);
55
56 432
        $referenceFound = $this->parseReferences($context, $cursor);
57
58 432
        $this->finalStringContents = $cursor->getRemainder();
59
60 432
        if ($referenceFound && $cursor->isAtEnd()) {
61 222
            $this->detach();
62
        }
63 432
    }
64
65 432
    protected function parseReferences(ContextInterface $context, Cursor $cursor): bool
66
    {
67 432
        $referenceFound = false;
68 432
        while ($cursor->getCharacter() === '[' && $context->getReferenceParser()->parse($cursor)) {
69 231
            $this->finalStringContents = $cursor->getRemainder();
70 231
            $referenceFound = true;
71
        }
72
73 432
        return $referenceFound;
74
    }
75
76 309
    public function handleRemainingContents(ContextInterface $context, Cursor $cursor): void
77
    {
78 309
        $cursor->advanceToNextNonSpaceOrTab();
79
80
        /** @var self $tip */
81 309
        $tip = $context->getTip();
82 309
        $tip->addLine($cursor->getRemainder());
83 309
    }
84
85
    /**
86
     * @return string[]
87
     */
88 2130
    public function getStrings(): array
89
    {
90 2130
        return $this->strings->toArray();
91
    }
92
}
93