Completed
Push — unique-permalinks ( ca79cc )
by Colin
01:24
created

UniqueSlugNormalizer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 10
cts 10
cp 1
c 0
b 0
f 0
wmc 3
lcom 1
cbo 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A normalize() 0 14 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
final class UniqueSlugNormalizer implements TextNormalizerInterface
17
{
18
    /** @var SlugNormalizer */
19
    private $slugNormalizer;
20
21
    /** @var array<string, int> */
22
    private $usages;
23
24 81
    public function __construct()
25
    {
26 81
        $this->slugNormalizer = new SlugNormalizer();
27 81
    }
28
29 75
    public function normalize(string $text, $context = null): string
30
    {
31 75
        $slug = $this->slugNormalizer->normalize($text, $context);
32
33 75
        if (!isset($this->usages[$slug])) {
34 75
            $this->usages[$slug] = 1;
35
36 75
            return $slug;
37
        }
38
39 9
        $count = $this->usages[$slug]++;
40
41 9
        return $slug . '-' . $count;
42
    }
43
}
44