Passed
Push — master ( 662d52...2a8232 )
by eXeCUT
03:58
created

EncodingDetector::getEncodingsList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 7
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * User: execut
4
 * Date: 01.08.16
5
 * Time: 17:09
6
 */
7
8
namespace execut\import\components;
9
10
11
use yii\base\Component;
12
13
class EncodingDetector extends Component
14
{
15
    public $source = null;
16
    public $expected = null;
17
    public function detect() {
18
        if ($this->source === $this->expected) {
19
            return [];
20
        }
21
22
        $encodings = self::getEncodingsList();
23
        foreach ($encodings as $fromEncoding) {
24
            foreach ($encodings as $toEncoding) {
25
                if ($fromEncoding === $toEncoding) {
26
                    continue;
27
                }
28
29
                if ($result = $this->tryConvert($this->source, $this->expected, $fromEncoding, $toEncoding)) {
30
                    return $result;
31
                }
32
            }
33
        }
34
    }
35
36
    protected function tryConvert($source, $expected, $from, $to, $isTryLevel = true) {
37
        $currentResult = \mb_convert_encoding($source, $to, $from);
38
        if ($currentResult === $expected) {
39
            return [$from, $to];
40
        }
41
42
        if (!$isTryLevel) {
43
            return;
44
        }
45
46
        $encodings = self::getEncodingsList();
47
        foreach ($encodings as $fromEncoding) {
48
            foreach ($encodings as $toEncoding) {
49
                if ($fromEncoding === $toEncoding) {
50
                    continue;
51
                }
52
53
                if ($result = $this->tryConvert($currentResult, $this->expected, $fromEncoding, $toEncoding, false)) {
54
                    return $result;
55
                }
56
            }
57
        }
58
    }
59
60
    public static function getEncodingsList() {
61
        $encodings = explode(', ', 'KOI8-R, KOI8-U, UTF-8, UTF-16, CP1251, CP1252, ISO-8859-1');
62
        return $encodings;
63
        $encodings = \mb_list_encodings();
0 ignored issues
show
Unused Code introduced by
$encodings = mb_list_encodings() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
64
        unset($encodings[0]);
65
        unset($encodings[1]);
66
        return $encodings;
67
    }
68
}