Paragraph   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 4
dl 0
loc 95
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\Block\Element;
16
17
use League\CommonMark\ContextInterface;
18
use League\CommonMark\Cursor;
19
20
class Paragraph extends AbstractStringContainerBlock implements InlineContainerInterface
21
{
22
    /**
23
     * Returns true if this block can contain the given block as a child node
24
     *
25
     * @param AbstractBlock $block
26
     *
27
     * @return bool
28
     */
29 69
    public function canContain(AbstractBlock $block): bool
30
    {
31 69
        return false;
32
    }
33
34
    /**
35
     * Whether this is a code block
36
     *
37
     * @return bool
38
     */
39 381
    public function isCode(): bool
40
    {
41 381
        return false;
42
    }
43
44 717
    public function matchesNextLine(Cursor $cursor): bool
45
    {
46 717
        if ($cursor->isBlank()) {
47 435
            $this->lastLineBlank = true;
48
49 435
            return false;
50
        }
51
52 381
        return true;
53
    }
54
55 1695
    public function finalize(ContextInterface $context, int $endLineNumber)
56
    {
57 1695
        parent::finalize($context, $endLineNumber);
58
59 1695
        $this->finalStringContents = \preg_replace('/^  */m', '', \implode("\n", $this->getStrings()));
60
61
        // Short-circuit
62 1695
        if ($this->finalStringContents === '' || $this->finalStringContents[0] !== '[') {
63 1347
            return;
64
        }
65
66 420
        $cursor = new Cursor($this->finalStringContents);
67
68 420
        $referenceFound = $this->parseReferences($context, $cursor);
69
70 420
        $this->finalStringContents = $cursor->getRemainder();
71
72 420
        if ($referenceFound && $cursor->isAtEnd()) {
73 222
            $this->detach();
74
        }
75 420
    }
76
77
    /**
78
     * @param ContextInterface $context
79
     * @param Cursor           $cursor
80
     *
81
     * @return bool
82
     */
83 420
    protected function parseReferences(ContextInterface $context, Cursor $cursor)
84
    {
85 420
        $referenceFound = false;
86 420
        while ($cursor->getCharacter() === '[' && $context->getReferenceParser()->parse($cursor)) {
87 231
            $this->finalStringContents = $cursor->getRemainder();
88 231
            $referenceFound = true;
89
        }
90
91 420
        return $referenceFound;
92
    }
93
94
    /**
95
     * @param ContextInterface $context
96
     * @param Cursor           $cursor
97
     */
98 276
    public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
99
    {
100 276
        $cursor->advanceToNextNonSpaceOrTab();
101
102
        /** @var self $tip */
103 276
        $tip = $context->getTip();
104 276
        $tip->addLine($cursor->getRemainder());
105 276
    }
106
107
    /**
108
     * @return string[]
109
     */
110 1734
    public function getStrings(): array
111
    {
112 1734
        return $this->strings->toArray();
113
    }
114
}
115