Reference   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 90
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getLabel() 0 4 1
A getDestination() 0 4 1
A getTitle() 0 4 1
A normalizeReference() 0 15 2
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\Reference;
16
17
final class Reference implements ReferenceInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $label;
23
24
    /**
25
     * @var string
26
     */
27
    protected $destination;
28
29
    /**
30
     * @var string
31
     */
32
    protected $title;
33
34
    /**
35
     * @var array
36
     *
37
     * Source: https://github.com/symfony/polyfill-mbstring/blob/master/Mbstring.php
38
     */
39
    private static $caseFold = [
40
        ['µ', 'ſ', "\xCD\x85", 'ς', "\xCF\x90", "\xCF\x91", "\xCF\x95", "\xCF\x96", "\xCF\xB0", "\xCF\xB1", "\xCF\xB5", "\xE1\xBA\x9B", "\xE1\xBE\xBE", "\xC3\x9F", "\xE1\xBA\x9E"],
41
        ['μ', 's', 'ι',        'σ', 'β',        'θ',        'φ',        'π',        'κ',        'ρ',        'ε',        "\xE1\xB9\xA1", 'ι',            'ss',       'ss'],
42
    ];
43
44
    /**
45
     * Constructor
46
     *
47
     * @param string $label
48
     * @param string $destination
49
     * @param string $title
50
     */
51 246
    public function __construct(string $label, string $destination, string $title)
52
    {
53 246
        $this->label = self::normalizeReference($label);
54 246
        $this->destination = $destination;
55 246
        $this->title = $title;
56 246
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 246
    public function getLabel(): string
62
    {
63 246
        return $this->label;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 198
    public function getDestination(): string
70
    {
71 198
        return $this->destination;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 198
    public function getTitle(): string
78
    {
79 198
        return $this->title;
80
    }
81
82
    /**
83
     * Normalize reference label
84
     *
85
     * This enables case-insensitive label matching
86
     *
87
     * @param string $string
88
     *
89
     * @return string
90
     */
91 333
    public static function normalizeReference(string $string): string
92
    {
93
        // Collapse internal whitespace to single space and remove
94
        // leading/trailing whitespace
95 333
        $string = \preg_replace('/\s+/', ' ', \trim($string));
96
97 333
        if (!\defined('MB_CASE_FOLD')) {
98
            // We're not on a version of PHP (7.3+) which has this feature
99 222
            $string = \str_replace(self::$caseFold[0], self::$caseFold[1], $string);
100
101 222
            return \mb_strtoupper($string, 'UTF-8');
102
        }
103
104 111
        return \mb_convert_case($string, \MB_CASE_FOLD, 'UTF-8');
105
    }
106
}
107