Passed
Push — master ( 09b005...3754d7 )
by Petr
13:20 queued 20s
created

CyrillicTransliterationTransformer::transform()   C

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