GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Slugger::slugify()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
c 2
b 0
f 0
dl 0
loc 13
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * It's free open-source software released under the MIT License.
7
 *
8
 * @author Anatoly Fenric <[email protected]>
9
 * @copyright Copyright (c) 2018, Anatoly Fenric
10
 * @license https://github.com/sunrise-php/slugger/blob/master/LICENSE
11
 * @link https://github.com/sunrise-php/slugger
12
 */
13
14
namespace Sunrise\Slugger;
15
16
/**
17
 * Import classes
18
 */
19
use Sunrise\Slugger\Exception\InvalidArgumentException;
20
use Sunrise\Slugger\Exception\UnableToCreateTransliteratorException;
21
use Sunrise\Slugger\Exception\UnableToTransliterateException;
22
use Transliterator;
23
24
/**
25
 * Import functions
26
 */
27
use function in_array;
28
use function preg_replace;
29
use function str_replace;
30 7
use function strtr;
31
use function trim;
32 7
33
/**
34 7
 * Slugger
35
 */
36 1
class Slugger implements SluggerInterface
37 1
{
38
39
    /**
40
     * Default Basic ID
41 6
     *
42 6
     * @var string
43
     *
44
     * @link http://userguide.icu-project.org/transforms/general#TOC-Basic-IDs
45
     */
46
    protected const DEFAULT_BASIC_ID = 'Russian-Latin/BGN';
47 9
48
    /**
49 9
     * Transliterator
50
     *
51
     * @var Transliterator
52
     */
53
    protected $transliterator;
54
55 8
    /**
56
     * Replacements
57 8
     *
58
     * @var array<string, string>
59
     */
60
    protected $replacements = [];
61
62
    /**
63 7
     * Constructor of the class
64
     *
65 7
     * @param string|null $basicId
66
     * @param array<string, string> $replacements
67 7
     *
68
     * @throws InvalidArgumentException
69 7
     * @throws UnableToCreateTransliteratorException
70
     */
71 7
    public function __construct(?string $basicId = null, array $replacements = [])
72
    {
73 1
        // http://userguide.icu-project.org/transforms/general#TOC-Basic-IDs
74 1
        /** @var string */
75
        $basicId = $basicId ?? static::DEFAULT_BASIC_ID;
76
        if (!in_array($basicId, Transliterator::listIDs(), true)) {
77
            throw new InvalidArgumentException('Unknown Basic ID');
78 6
        }
79
80 6
        // http://userguide.icu-project.org/transforms/general#TOC-Compound-IDs
81
        $compoundIds = $basicId . '; Any-Latin; Latin-ASCII; Lower()';
82
        $transliterator = Transliterator::create($compoundIds);
83
        if ($transliterator === null) {
84
            throw new UnableToCreateTransliteratorException('Unable to create transliterator');
85
        }
86
87 6
        $this->transliterator = $transliterator;
88
        $this->replacements = $replacements;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93 4
     *
94
     * @throws UnableToTransliterateException
95 4
     */
96
    public function slugify(string $string, string $separator = '-') : string
97 4
    {
98
        $result = $this->transliterator->transliterate($string);
99 4
        if ($result === false) {
100
            throw new UnableToTransliterateException('Unable to transliterate');
101 4
        }
102
103 4
        $result = strtr($result, $this->replacements);
104
        $result = str_replace(['"', "'"], '', $result);
105
        $result = preg_replace('/[^0-9A-Za-z]++/', $separator, $result);
106
        $result = trim($result, $separator);
107
108
        return $result;
109
    }
110
}
111