Issues (75)

components/EncodingDetector.php (1 issue)

Severity
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
$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
}