Completed
Pull Request — master (#267)
by Richard
08:54
created

Util   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A safeStrlen() 0 7 2
A safeSubstr() 0 9 3
1
<?php
2
/**
3
 * The Mixer strategy interface.
4
 *
5
 * All mixing strategies must implement this interface
6
 *
7
 * PHP version 5.3
8
 *
9
 * @category   PHPPasswordLib
10
 * @package    Hash
11
 * @author     Anthony Ferrara <[email protected]>
12
 * @copyright  2011 The Authors
13
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
14
 * @version    Build @@version@@
15
 */
16
17
namespace SecurityLib;
18
19
/**
20
 * The Utility trait.
21
 *
22
 * Contains methods used internally to this library.
23
 *
24
 * @category   PHPPasswordLib
25
 * @package    Random
26
 * @author     Scott Arciszewski <[email protected]>
27
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
28
 * @codeCoverageIgnore
29
 */
30
abstract class Util {
31
32
    /**
33
     * Return the length of a string, even in the presence of
34
     * mbstring.func_overload
35
     *
36
     * @param string $string the string we're measuring
37
     * @return int
38
     */
39
    public static function safeStrlen($string)
40
    {
41
        if (\function_exists('mb_strlen')) {
42
            return \mb_strlen($string, '8bit');
43
        }
44
        return \strlen($string);
45
    }
46
47
    /**
48
     * Return a string contained within a string, even in the presence of
49
     * mbstring.func_overload
50
     *
51
     * @param string $string The string we're searching
52
     * @param int $start What offset should we begin
53
     * @param int|null $length How long should the substring be?
54
     *                         (default: the remainder)
55
     * @return string
56
     */
57
    public static function safeSubstr($string, $start = 0, $length = null)
58
    {
59
        if (\function_exists('mb_substr')) {
60
            return \mb_substr($string, $start, $length, '8bit');
61
        } elseif ($length !== null) {
62
            return \substr($string, $start, $length);
63
        }
64
        return \substr($string, $start);
65
    }
66
}
67