Completed
Push — master ( 55651f...2557fa )
by Garrett
08:01
created

AString::isAscii()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 6
nc 3
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A AString::offsetGet() 0 4 1
1
<?php
2
3
namespace StringObject;
4
5
class AString extends AnyString
6
{
7 View Code Duplication
    public function toArray($delim = '', $limit = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
8
    {
9
        if (empty($delim)) {
10
            return \str_split($this->raw);
11
        }
12
        if (is_int($delim)) {
13
            return \str_split($this->raw, $delim);
14
        }
15
        if ($limit === null) {
16
            return \explode($delim, $this->raw);
17
        }
18
        return \explode($delim, $this->raw, $limit);
19
    }
20
21
    // INFORMATIONAL METHODS
22
23
    public function charAt($offset)
24
    {
25
        return new static($this->raw{$offset});
26
    }
27
28
    /**
29
     * @param integer $offset
30
     */
31
    public function charCodeAt($offset)
32
    {
33
        return \ord($this->raw{$offset});
34
    }
35
36
    public function compareTo($str, $flags = self::NORMAL, $length = 1)
37
    {
38
        // strip out bits we don't understand
39
        $flags &= (self::CASE_INSENSITIVE | self::CURRENT_LOCALE | self::NATURAL_ORDER | self::FIRST_N);
40
41
        $flagsmap = [
42
            self::NORMAL => 'strcmp',
43
            self::CASE_INSENSITIVE => 'strcasecmp',
44
            self::CURRENT_LOCALE => 'strcoll',
45
            self::NATURAL_ORDER => 'strnatcmp',
46
            (self::NATURAL_ORDER | self::CASE_INSENSITIVE) => 'strnatcasecmp',
47
            self::FIRST_N => 'strncmp',
48
            (self::FIRST_N | self::CASE_INSENSITIVE) => 'strncasecmp',
49
        ];
50
51
        if ($flags & self::FIRST_N) {
52
            return \call_user_func($flagsmap[$flags], $this->raw, $str, $length);
53
        }
54
        return \call_user_func($flagsmap[$flags], $this->raw, $str);
55
    }
56
57
    public function indexOf($needle, $offset = 0, $flags = self::NORMAL)
58
    {
59
        // strip out bits we don't understand
60
        $flags &= (self::REVERSE | self::CASE_INSENSITIVE);
61
62
        $flagsmap = [
63
            self::NORMAL => 'strpos',
64
            self::CASE_INSENSITIVE => 'stripos',
65
            self::REVERSE => 'strrpos',
66
            (self::REVERSE | self::CASE_INSENSITIVE) => 'strripos',
67
        ];
68
        return \call_user_func($flagsmap[$flags], $this->raw, $needle, $offset);
69
    }
70
71
    public function length()
72
    {
73
        return \strlen($this->raw);
74
    }
75
76
    // MODIFYING METHODS
77
78
    public function chunk($length = 76, $ending = "\r\n")
79
    {
80
        return $this->replaceWhole(\chunk_split($this->raw, $length, $ending));
81
    }
82
83
    public function escape($flags = self::NORMAL, $charlist = '')
84
    {
85
        // strip out bits we don't understand
86
        $flags &= (self::C_STYLE | self::META);
87
88
        $flagsmap = [
89
            self::NORMAL => 'addslashes',
90
            self::C_STYLE => 'addcslashes',
91
            self::META => 'quotemeta',
92
        ];
93
        if ($flags === self::C_STYLE) {
94
            return $this->replaceWhole(\call_user_func($flagsmap[$flags], $this->raw, $charlist));
95
        }
96
        return $this->replaceWhole(\call_user_func($flagsmap[$flags], $this->raw));
97
    }
98
99
    public function insertAt($str, $offset)
100
    {
101
        return $this->replaceSubstr($str, $offset, 0);
102
    }
103
104
    public function pad($newlength, $padding = ' ', $flags = self::END)
105
    {
106
        return $this->replaceWhole(\str_pad($this->raw, $newlength, $padding, $flags));
107
    }
108
109
    public function prepend($str)
110
    {
111
        return $this->replaceWhole($str . $this->raw);
112
    }
113
114
    public function remove($str, $flags = self::NORMAL)
115
    {
116
        return $this->replace($str, '', $flags);
117
    }
118
119
    public function removeSubstr($start, $length = null)
120
    {
121
        return $this->replaceSubstr('', $start, $length);
122
    }
123
124
    public function repeat($times)
125
    {
126
        return $this->replaceWhole(\str_repeat($this->raw, $times));
127
    }
128
129
    /**
130
     * @param string $replace
131
     */
132
    public function replace($search, $replace, $flags = self::NORMAL)
133
    {
134
        if ($flags & self::CASE_INSENSITIVE) {
135
            return $this->replaceWhole(\str_ireplace($search, $replace, $this->raw));
136
        }
137
        return $this->replaceWhole(\str_replace($search, $replace, $this->raw));
138
    }
139
140
    public function replaceSubstr($replacement, $start, $length = null)
141
    {
142
        if ($length === null) {
143
            $length = $this->length();
144
        }
145
        return $this->replaceWhole(\substr_replace($this->raw, $replacement, $start, $length));
146
    }
147
148
    public function reverse()
149
    {
150
        return $this->replaceWhole(\strrev($this->raw));
151
    }
152
153
    public function shuffle()
154
    {
155
        return $this->replaceWhole(\str_shuffle($this->raw));
156
    }
157
158
    public function substr($start, $length = 'omitted')
159
    {
160
        if ($length === 'omitted') {
161
            return new static(\substr($this->raw, $start));
162
        }
163
        return new static(\substr($this->raw, $start, $length));
164
    }
165
166
    public function translate($search, $replace = '')
167
    {
168
        if (is_array($search)) {
169
            return $this->replaceWhole(\strtr($this->raw, $search));
170
        }
171
        return $this->replaceWhole(\strtr($this->raw, $search, $replace));
172
    }
173
174 View Code Duplication
    public function trim($mask = " \t\n\r\0\x0B", $flags = self::BOTH_ENDS)
175
    {
176
        // strip out bits we don't understand
177
        $flags &= (self::END | self::BOTH_ENDS);
178
179
        $flagsmap = [
180
            self::START => 'ltrim',
181
            self::END => 'rtrim',
182
            self::BOTH_ENDS => 'trim',
183
        ];
184
        return $this->replaceWhole(\call_user_func($flagsmap[$flags], $this->raw, $mask));
185
    }
186
187 View Code Duplication
    public function unescape($flags = self::NORMAL)
188
    {
189
        // strip out bits we don't understand
190
        $flags &= (self::C_STYLE | self::META);
191
192
        $flagsmap = [
193
            self::NORMAL => 'stripslashes',
194
            self::C_STYLE => 'stripcslashes',
195
            self::META => 'stripslashes',
196
        ];
197
        return $this->replaceWhole(\call_user_func($flagsmap[$flags], $this->raw));
198
    }
199
200
    // TESTING METHODS
201
202
    public function contains($needle, $offset = 0, $flags = self::NORMAL)
203
    {
204
        if ($flags & self::EXACT_POSITION) {
205
            return ($this->indexOf($needle, $offset, $flags) === $offset);
206
        }
207
        return ($this->indexOf($needle, $offset, $flags) !== false);
208
    }
209
210
    public function countSubstr($needle, $offset = 0, $length = null)
211
    {
212
        if ($length === null) {
213
            return \substr_count($this->raw, $needle, $offset);
214
        }
215
        return \substr_count($this->raw, $needle, $offset, $length);
216
    }
217
218
    public function endsWith($str, $flags = self::NORMAL)
219
    {
220
        $flags &= self::CASE_INSENSITIVE;
221
        $offset = $this->length() - \strlen($str);
222
        return $this->contains($str, $offset, $flags | self::EXACT_POSITION | self::REVERSE);
223
    }
224
225
    public function startsWith($str, $flags = self::NORMAL)
226
    {
227
        $flags &= self::CASE_INSENSITIVE;
228
        return $this->contains($str, 0, $flags | self::EXACT_POSITION);
229
    }
230
231
    // INTERFACE IMPLEMENTATION METHODS
232
233
    public function count()
234
    {
235
        return \strlen($this->raw);
236
    }
237
238
    public function current()
239
    {
240
        return $this->raw[$this->caret];
241
    }
242
243
    public function offsetGet($offset)
244
    {
245
        return $this->raw{$offset};
246
    }
247
248
    public function offsetSet($offset, $value)
249
    {
250
        throw new \LogicException('Cannot assign ' . $value . ' to immutable AString instance at index ' . $offset);
251
    }
252
253
    public function offsetUnset($offset)
254
    {
255
        throw new \LogicException('Cannot unset index ' . $offset . ' on immutable AString instance');
256
    }
257
}
258