Completed
Pull Request — master (#74)
by
unknown
04:47
created

Paragonie_Util_Binary::substr()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 25
rs 8.439
cc 5
eloc 13
nc 5
nop 3
1
<?php
2
/*
3
 * Random_* Compatibility Library 
4
 * for using the new PHP 7 random_* API in PHP 5 projects
5
 * 
6
 * The MIT License (MIT)
7
 * 
8
 * Copyright (c) 2015 Paragon Initiative Enterprises
9
 * 
10
 * Permission is hereby granted, free of charge, to any person obtaining a copy
11
 * of this software and associated documentation files (the "Software"), to deal
12
 * in the Software without restriction, including without limitation the rights
13
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
 * copies of the Software, and to permit persons to whom the Software is
15
 * furnished to do so, subject to the following conditions:
16
 * 
17
 * The above copyright notice and this permission notice shall be included in
18
 * all copies or substantial portions of the Software.
19
 * 
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
 * SOFTWARE.
27
 */
28
29
if (
30
    defined('MB_OVERLOAD_STRING') &&
31
    ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING
32
) {
33
    class Paragonie_Util_Binary
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
34
    {
35
        /**
36
         * strlen() implementation that isn't brittle to mbstring.func_overload
37
         *
38
         * This version uses mb_strlen() in '8bit' mode to treat strings as raw
39
         * binary rather than UTF-8, ISO-8859-1, etc
40
         *
41
         * @param string $binary_string
42
         *
43
         * @throws TypeError
44
         *
45
         * @return int
46
         */
47
        public static function strlen($binary_string)
48
        {
49
            if (!is_string($binary_string)) {
50
                throw new TypeError(
51
                    __METHOD__.'() expects a string'
52
                );
53
            }
54
            return mb_strlen($binary_string, '8bit');
55
        }
56
57
        /**
58
         * substr() implementation that isn't brittle to mbstring.func_overload
59
         *
60
         * This version uses mb_substr() in '8bit' mode to treat strings as raw
61
         * binary rather than UTF-8, ISO-8859-1, etc
62
         *
63
         * @param string $binary_string
64
         * @param int $start
65
         * @param int $length (optional)
66
         *
67
         * @throws TypeError
68
         *
69
         * @return string
70
         */
71
        public static function substr($binary_string, $start, $length = null)
72
        {
73
            if (!is_string($binary_string)) {
74
                throw new TypeError(
75
                    __METHOD__.'(): First argument should be a string'
76
                );
77
            }
78
            if (!is_int($start)) {
79
                throw new TypeError(
80
                    __METHOD__.'(): Second argument should be an integer'
81
                );
82
            }
83
            if ($length === null) {
84
                /**
85
                 * mb_substr($str, 0, NULL, '8bit') returns an empty string on
86
                 * PHP 5.3, so we have to find the length ourselves.
87
                 */
88
                $length = mb_strlen($binary_string, '8bit') - $start;
89
            } elseif (!is_int($length)) {
90
                throw new TypeError(
91
                    __METHOD__.'(): Third argument should be an integer, or omitted'
92
                );
93
            }
94
            return mb_substr($binary_string, $start, $length, '8bit');
95
        }
96
    }
97
} else {
98
    class Paragonie_Util_Binary
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type Paragonie_Util_Binary has been defined more than once; this definition is ignored, only the first definition in this file (L33-96) is considered.

This check looks for classes that have been defined more than once in the same file.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
99
    {
100
        /**
101
         * strlen() implementation that isn't brittle to mbstring.func_overload
102
         *
103
         * This version just used the default strlen()
104
         *
105
         * @param string $binary_string
106
         *
107
         * @throws TypeError
108
         *
109
         * @return int
110
         */
111
        public static function strlen($binary_string)
112
        {
113
            if (!is_string($binary_string)) {
114
                throw new TypeError(
115
                    __METHOD__.'() expects a string'
116
                );
117
            }
118
            return strlen($binary_string);
119
        }
120
121
        /**
122
         * substr() implementation that isn't brittle to mbstring.func_overload
123
         *
124
         * This version just uses the default substr()
125
         *
126
         * @param string $binary_string
127
         * @param int $start
128
         * @param int $length (optional)
129
         *
130
         * @throws TypeError
131
         *
132
         * @return string
133
         */
134
        public static function substr($binary_string, $start, $length = null)
135
        {
136
            if (!is_string($binary_string)) {
137
                throw new TypeError(
138
                    __METHOD__.'(): First argument should be a string'
139
                );
140
            }
141
            if (!is_int($start)) {
142
                throw new TypeError(
143
                    __METHOD__.'(): Second argument should be an integer'
144
                );
145
            }
146
            if ($length !== null) {
147
                if (!is_int($length)) {
148
                    throw new TypeError(
149
                        __METHOD__.'(): Third argument should be an integer, or omitted'
150
                    );
151
                }
152
                return substr($binary_string, $start, $length);
153
            }
154
            return substr($binary_string, $start);
155
        }
156
    }
157
}
158