TextNormalizer   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 20
ccs 5
cts 5
cp 1
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 13 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
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace League\CommonMark\Normalizer;
15
16
/***
17
 * Normalize text input using the steps given by the CommonMark spec to normalize labels
18
 *
19
 * @see https://spec.commonmark.org/0.29/#matches
20
 *
21
 * @psalm-immutable
22
 */
23
final class TextNormalizer implements TextNormalizerInterface
24
{
25
    /**
26
     * {@inheritDoc}
27
     *
28
     * @psalm-pure
29
     */
30 334
    public function normalize(string $text, array $context = []): string
31
    {
32
        // Collapse internal whitespace to single space and remove
33
        // leading/trailing whitespace
34 334
        $text = \preg_replace('/[ \t\r\n]+/', ' ', \trim($text));
35
        \assert(\is_string($text));
36
37
        // Is it strictly ASCII? If so, we can use strtolower() instead (faster)
38 334
        if (\mb_check_encoding($text, 'ASCII')) {
39 294
            return \strtolower($text);
40
        }
41
42 50
        return \mb_convert_case($text, \MB_CASE_FOLD, 'UTF-8');
43
    }
44
}
45