• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taobig / php-helper / 10330426697

10 Aug 2024 08:58AM UTC coverage: 91.718% (-0.1%) from 91.853%
10330426697

push

github

taobig
remove @deprecated methods & constants:
1. taobig\helpers\composer\Version::PACKAGE_PROD(use Version::PACKAGE instead)
2. taobig\helpers\StringHelper::leftPadding() (use StringHelper::prepend() instead)
3. taobig\helpers\ArrayHelper::groupBy() (use groupByColumn() instead)
4. taobig\helpers\ArrayHelper::removeEmptyElement() (use removeEmpty() instead)
5. taobig\helpers\ArrayHelper::removeSpecifiedElement() (use removeSpecified() instead)
6. taobig\helpers\lock\Lock::name() (use getName() instead)
7. taobig\helpers\RandomHelper::getRandomEnglishCharacters() (use str() instead)

443 of 483 relevant lines covered (91.72%)

1.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.08
/src/StringHelper.php
1
<?php
2

3
namespace taobig\helpers;
4

5
class StringHelper
6
{
7

8
    /**
9
     * All the parameters are case-sensitive.
10
     * @param string $str
11
     * @param string $characters
12
     * @return bool
13
     * @see str_starts_with() since PHP 8.0
14
     */
15
    public static function startsWith(string $str, string $characters): bool
16
    {
17
        return str_starts_with($str, $characters);
2✔
18
    }
19

20
    /**
21
     * All the parameters are case-sensitive.
22
     * @param string $str
23
     * @param string $characters
24
     * @return bool
25
     * @see str_ends_with() since PHP 8.0
26
     */
27
    public static function endsWith(string $str, string $characters): bool
28
    {
29
        return str_ends_with($str, $characters);
2✔
30
    }
31

32
    /**
33
     * case-sensitive
34
     * @param string $str
35
     * @param string $characters
36
     * @return bool
37
     */
38
    public static function contains(string $str, string $characters): bool
39
    {
40
        return str_contains($str, $characters);
1✔
41
    }
42

43
    /**
44
     * All the parameters are case-sensitive.
45
     * @param string $str
46
     * @param string $characters
47
     * @return bool|string
48
     */
49
    public static function stripLeft(string $str, string $characters)
50
    {
51
        if (self::startsWith($str, $characters)) {
1✔
52
            return substr($str, strlen($characters));
1✔
53
        }
54
        return $str;
1✔
55
    }
56

57
    /**
58
     * All the parameters are case-sensitive.
59
     * @param string $str
60
     * @param string $characters
61
     * @return bool|string
62
     */
63
    public static function stripRight(string $str, string $characters)
64
    {
65
        if (self::endsWith($str, $characters)) {
1✔
66
            return substr($str, 0, strlen($str) - strlen($characters));
1✔
67
        }
68
        return $str;
1✔
69
    }
70

71
    public static function mb_rtrim(string $str, string $trim, string $encoding = 'UTF-8'): string
72
    {
73
        $mask = [];
1✔
74
        $trimLength = mb_strlen($trim, $encoding);
1✔
75
        for ($i = 0; $i < $trimLength; ++$i) {
1✔
76
            $item = mb_substr($trim, $i, 1, $encoding);
1✔
77
            $mask[] = $item;
1✔
78
        }
79

80
        $len = mb_strlen($str, $encoding);
1✔
81
        $pos = $len;
1✔
82
        if ($len > 0) {
1✔
83
            for ($i = $len - 1; $i >= 0; --$i) {
1✔
84
                $item = mb_substr($str, $i, 1, $encoding);
1✔
85
                if (in_array($item, $mask)) {
1✔
86
                    --$pos;
1✔
87
                } else {
88
                    break;
1✔
89
                }
90
            }
91
            return mb_substr($str, 0, $pos, $encoding);
1✔
92
        }
93
        return $str;
1✔
94
    }
95

96
    /**
97
     * @param string $str
98
     * @param int $length
99
     * @param string $paddingChar Must be a character
100
     * @return string
101
     */
102
    public static function prepend(string $str, int $length, string $paddingChar): string
103
    {
104
        if (strlen($paddingChar) !== 1) {
1✔
105
            return $str;
1✔
106
        }
107
        if (strlen($str) >= $length) {
1✔
108
            return $str;
1✔
109
        }
110
        $paddingLength = $length - strlen($str);
1✔
111
        for ($i = 0; $i < $paddingLength; ++$i) {
1✔
112
            $str = "{$paddingChar}{$str}";
1✔
113
        }
114
        return $str;
1✔
115
    }
116

117
    //合并多个连续空格为一个
118
    public static function combineSpaces(string $str): string
119
    {
120
        return preg_replace("/\s(?=\s)/", "\\1", $str);
1✔
121
    }
122

123
    //合并多个连续空格为一个
124
    public static function combineSpacesInUtf8String(string $str): ?string
125
    {
126
        return preg_replace("/\s(?=\s)/u", "\\1", $str);
1✔
127
    }
128

129
    public static function snakeCase2CamelCase(string $str): string
130
    {
131
        return lcfirst(implode('', array_map('ucfirst', explode('_', $str))));
2✔
132
    }
133

134
    public static function camelCase2KebabCase(string $input): string
135
    {
136
        return strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', $input));
1✔
137
    }
138

139
    public static function camelCase2SnakeCase(string $input): string
140
    {
141
        return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $input));
1✔
142
    }
143

144
    /**
145
     * @param string $str
146
     * @param string $separator Can't be an empty string
147
     * @param bool $keepEmptyParts
148
     * @return array
149
     * @throws \ValueError
150
     */
151
    public static function split(string $str, string $separator = ',', bool $keepEmptyParts = true)
152
    {
153
        if (PHP_MAJOR_VERSION < 8) {
2✔
154
            if (strlen($separator) === 0) {
×
155
                throw new \ValueError("Empty delimiter");
×
156
            }
157
        }
158
        $list = explode($separator, $str);
2✔
159
        if (!$keepEmptyParts) {
1✔
160
            $validList = [];
1✔
161
            foreach ($list as $item) {
1✔
162
                if ($item === '') {
1✔
163
                    continue;
1✔
164
                }
165
                $validList[] = $item;
1✔
166
            }
167
            $list = $validList;
1✔
168
        }
169
        return $list;
1✔
170
    }
171

172

173
    public static function equals(string $str1, string $str2): bool
174
    {
175
        return $str1 === $str2;
1✔
176
    }
177

178
    public static function equalsIgnoreCase(string $str1, string $str2): bool
179
    {
180
//        return strtolower($str1) === strtolower($str2);
181
        return strcasecmp($str1, $str2) == 0;
1✔
182
    }
183

184
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc