Passed
Push — master ( a19423...673e99 )
by Domenico
02:59 queued 29s
created

CatUtils::contains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 2
rs 10
1
<?php
2
3
namespace Matecat\SubFiltering\Utils;
4
5
class CatUtils {
6
7
    /**
8
     * @param string $needle
9
     * @param string $haystack
10
     *
11
     * @return bool
12
     */
13
    public static function contains( $needle, $haystack ) {
14
        return mb_strpos( $haystack, $needle ) !== false;
15
    }
16
17
    /**
18
     * Get the char code from a multi byte char
19
     *
20
     * 2/3 times faster than the old implementation
21
     *
22
     * @param $mb_char string Unicode Multibyte Char String
23
     *
24
     * @return int
25
     *
26
     */
27
    public static function fastUnicode2ord( $mb_char ) {
28
        switch ( strlen( $mb_char ) ) {
29
            case 1:
30
                return ord( $mb_char );
31
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
32
            case 2:
33
                return ( ord( $mb_char[ 0 ] ) - 0xC0 ) * 0x40 +
34
                        ord( $mb_char[ 1 ] ) - 0x80;
35
                break;
36
            case 3:
37
                return ( ord( $mb_char[ 0 ] ) - 0xE0 ) * 0x1000 +
38
                        ( ord( $mb_char[ 1 ] ) - 0x80 ) * 0x40 +
39
                        ord( $mb_char[ 2 ] ) - 0x80;
40
                break;
41
            case 4:
42
                return ( ord( $mb_char[ 0 ] ) - 0xF0 ) * 0x40000 +
43
                        ( ord( $mb_char[ 1 ] ) - 0x80 ) * 0x1000 +
44
                        ( ord( $mb_char[ 2 ] ) - 0x80 ) * 0x40 +
45
                        ord( $mb_char[ 3 ] ) - 0x80;
46
                break;
47
        }
48
49
        return 20; //as default return a space ( should never happen )
50
    }
51
52
    /**
53
     * @param $str
54
     *
55
     * @return string
56
     */
57
    public static function htmlentitiesFromUnicode( $str ) {
58
        return "&#" . self::fastUnicode2ord( $str[ 1 ] ) . ";";
59
    }
60
61
    /**
62
     * multibyte string manipulation functions
63
     * source : http://stackoverflow.com/questions/9361303/can-i-get-the-unicode-value-of-a-character-or-vise-versa-with-php
64
     * original source : PHPExcel libary (http://phpexcel.codeplex.com/)
65
     * get the char from unicode code
66
     *
67
     * @param $o
68
     *
69
     * @return string
70
     */
71
    public static function unicode2chr( $o ) {
72
        if ( function_exists( 'mb_convert_encoding' ) ) {
73
            return mb_convert_encoding( '&#' . intval( $o ) . ';', 'UTF-8', 'HTML-ENTITIES' );
0 ignored issues
show
Bug Best Practice introduced by
The expression return mb_convert_encodi...TF-8', 'HTML-ENTITIES') also could return the type array which is incompatible with the documented return type string.
Loading history...
74
        }
75
76
        return chr( intval( $o ) );
77
    }
78
}