Completed
Push — master ( 2378d4...3741f7 )
by Colin
28:04 queued 26:46
created

DefaultSlugGenerator   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A createSlug() 0 13 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
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\CommonMark\Extension\HeadingPermalink\Slug;
13
14
/**
15
 * Creates URL-friendly strings
16
 */
17
final class DefaultSlugGenerator implements SlugGeneratorInterface
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 84
    public function createSlug(string $input): string
23
    {
24
        // Trim whitespace
25 84
        $slug = \trim($input);
26
        // Convert to lowercase
27 84
        $slug = \mb_strtolower($slug);
28
        // Try replacing whitespace with a dash
29 84
        $slug = \preg_replace('/\s+/u', '-', $slug) ?? $slug;
30
        // Try removing non-alphanumeric and non-dash characters
31 84
        $slug = \preg_replace('/[^\p{Lu}\p{Ll}\p{Lt}\p{Nd}\p{Nl}\-]/u', '', $slug) ?? $slug;
32
33 84
        return $slug;
34
    }
35
}
36