Passed
Push — latest ( 2a2bd2...5b2ad0 )
by Colin
02:22 queued 11s
created

Mention::findLabelNode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
c 1
b 0
f 0
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace League\CommonMark\Extension\Mention;
18
19
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
20
use League\CommonMark\Node\Inline\Text;
21
22
class Mention extends Link
23
{
24
    /** @var string */
25
    private $name;
26
27
    /** @var string */
28
    private $prefix;
29
30
    /** @var string */
31
    private $identifier;
32
33 63
    public function __construct(string $name, string $prefix, string $identifier, ?string $label = null)
34
    {
35 63
        $this->name       = $name;
36 63
        $this->prefix     = $prefix;
37 63
        $this->identifier = $identifier;
38
39 63
        parent::__construct('', $label ?? \sprintf('%s%s', $prefix, $identifier));
40 63
    }
41
42 18
    public function getLabel(): ?string
43
    {
44 18
        if (($labelNode = $this->findLabelNode()) === null) {
45 3
            return null;
46
        }
47
48 18
        return $labelNode->getLiteral();
49
    }
50
51 33
    public function getIdentifier(): string
52
    {
53 33
        return $this->identifier;
54
    }
55
56 3
    public function getName(): ?string
57
    {
58 3
        return $this->name;
59
    }
60
61 12
    public function getPrefix(): string
62
    {
63 12
        return $this->prefix;
64
    }
65
66 15
    public function hasUrl(): bool
67
    {
68 15
        return $this->url !== '';
69
    }
70
71
    /**
72
     * @return $this
73
     */
74 12
    public function setLabel(string $label): self
75
    {
76 12
        if (($labelNode = $this->findLabelNode()) === null) {
77 3
            $labelNode = new Text();
78 3
            $this->prependChild($labelNode);
79
        }
80
81 12
        $labelNode->setLiteral($label);
82
83 12
        return $this;
84
    }
85
86 18
    private function findLabelNode(): ?Text
87
    {
88 18
        foreach ($this->children() as $child) {
89 18
            if ($child instanceof Text) {
90 18
                return $child;
91
            }
92
        }
93
94 3
        return null;
95
    }
96
}
97