CyrillicTransliterationTransformer::transform()   C
last analyzed

Complexity

Conditions 17
Paths 6

Size

Total Lines 48
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 17

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 48
ccs 19
cts 19
cp 1
rs 5.2166
cc 17
nc 6
nop 1
crap 17

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of riesenia/pohoda package.
5
 *
6
 * Licensed under the MIT License
7
 * (c) RIESENIA.com
8
 */
9
10
declare(strict_types=1);
11
12
namespace Riesenia\Pohoda\ValueTransformer;
13
14
/**
15
 * A transformer that rewrites Cyrillic script to its Latin alphabet equivalent.
16
 *
17
 * Note: This transformation is a phonetic representation and does not provide a translation of the text.
18
 */
19
class CyrillicTransliterationTransformer implements ValueTransformerInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 4
    public function transform(string $value): string
25
    {
26 4
        $normalized = \Normalizer::normalize($value, \Normalizer::FORM_C);
27
28 4
        if (false === $normalized) {
29
            // @codeCoverageIgnoreStart
30
            // must do some error with Normalizer class
31
            return $value;
32
        }
33
        // @codeCoverageIgnoreEnd
34
35 4
        $transformer = \Transliterator::create('Any-Latin; Latin-ASCII');
36
37 4
        if (is_null($transformer)) {
38
            // @codeCoverageIgnoreStart
39
            // this can happen only if there is no latin encoding installed - not happen with Pohoda which need that
40
            return $value;
41
        }
42
        // @codeCoverageIgnoreEnd
43
44 4
        $chars = \preg_split('//u', $normalized, -1, PREG_SPLIT_NO_EMPTY);
45
46 4
        if (false === $chars) {
47
            // @codeCoverageIgnoreStart
48
            // can happen only when something in preg_split fails
49
            return $value;
50
        }
51
        // @codeCoverageIgnoreEnd
52
53 4
        $result = '';
54
55 4
        foreach ($chars as $char) {
56 4
            $codePoint = \mb_ord($char, 'UTF-8');
57
58 4
            if ((0x00400 <= $codePoint && 0x004FF >= $codePoint)    // Cyrillic Basic
59 4
                || (0x00500 <= $codePoint && 0x0052F >= $codePoint) // Cyrillic Supplement
60 4
                || (0x02DE0 <= $codePoint && 0x02DFF >= $codePoint) // Cyrillic Extended-A
61 4
                || (0x0A640 <= $codePoint && 0x0A69F >= $codePoint) // Cyrillic Extended-B
62 4
                || (0x01C80 <= $codePoint && 0x01C8F >= $codePoint) // Cyrillic Extended-C
63 4
                || (0x1E030 <= $codePoint && 0x1E08F >= $codePoint) // Cyrillic Extended-D
64
            ) {
65 1
                $result .= $transformer->transliterate($char);
66
            } else {
67 4
                $result .= $char;
68
            }
69
        }
70
71 4
        return $result;
72
    }
73
}
74