@@ -9,17 +9,16 @@ abstract class AbstractString implements StringObject
99{
1010 // PROPERTIES
1111
12- protected $ raw ;
13- protected $ caret = 0 ;
14- protected $ token = false ;
12+ protected string $ raw ;
13+ protected int $ caret = 0 ;
14+ protected bool $ token = false ;
1515
1616 /**
17- * @param mixed $thing Anything that can be cast to a string
17+ * @param string|object $thing Anything that can be cast to a string
1818 */
1919 final public function __construct ($ thing )
2020 {
21- static ::stringableOrFail ($ thing );
22- $ this ->raw = (string ) $ thing ;
21+ $ this ->raw = static ::stringify ($ thing );
2322 }
2423
2524 public function __get (string $ name ): mixed
@@ -76,12 +75,12 @@ public function valid(): bool
7675
7776 // END Iterator methods }
7877
78+ /**
79+ * @param string|object $str
80+ */
7981 public function equals ($ str ): bool
8082 {
81- static ::stringableOrFail ($ str );
82-
83- $ str = (string ) $ str ;
84- return ($ str == $ this ->raw );
83+ return (static ::stringify ($ str ) == $ this ->raw );
8584 }
8685
8786 public function length (): int
@@ -111,12 +110,12 @@ public function isEmpty(): bool
111110 return empty ($ this ->raw );
112111 }
113112
114- public function append ($ str ): static
113+ public function append (string $ str ): static
115114 {
116115 return new static ($ this ->raw . $ str );
117116 }
118117
119- public function concat ($ str ): static
118+ public function concat (string $ str ): static
120119 {
121120 return $ this ->append ($ str );
122121 }
@@ -139,7 +138,7 @@ public function escape(int $mode = self::NORMAL, string $charlist = ''): static
139138
140139 public function hexDecode (): static
141140 {
142- return new static (\hex2bin ($ this ->raw ));
141+ return new static (\hex2bin ($ this ->raw ) ?: '' );
143142 }
144143
145144 public function hexEncode (): static
@@ -152,7 +151,7 @@ public function prepend(string $str): static
152151 return new static ($ str . $ this ->raw );
153152 }
154153
155- public function remove (string $ str , $ mode = self ::NORMAL ): static
154+ public function remove (string $ str , int $ mode = self ::NORMAL ): static
156155 {
157156 return $ this ->replace ($ str , '' , $ mode );
158157 }
@@ -175,6 +174,9 @@ public function replace(string $search, string $replace, int $mode = self::NORMA
175174 return new static (\str_replace ($ search , $ replace , $ this ->raw ));
176175 }
177176
177+ /**
178+ * @param string|string[] $search
179+ */
178180 public function translate (mixed $ search , string $ replace = '' ): static
179181 {
180182 if (is_array ($ search )) {
@@ -212,10 +214,10 @@ public function unescape(int $mode = self::NORMAL): static
212214 public function nextToken (string $ delim ): static
213215 {
214216 if ($ this ->token ) {
215- return new static (\strtok ($ delim ));
217+ return new static (\strtok ($ delim ) ?: '' );
216218 }
217219 $ this ->token = true ;
218- return new static (\strtok ($ this ->raw , $ delim ));
220+ return new static (\strtok ($ this ->raw , $ delim ) ?: '' );
219221 }
220222
221223 public function resetToken (): void
@@ -230,21 +232,25 @@ public function times(int $times): static
230232
231233 public function uuDecode (): static
232234 {
233- return new static (\convert_uudecode ($ this ->raw ));
235+ return new static (\convert_uudecode ($ this ->raw ) ?: '' );
234236 }
235237
236238 public function uuEncode (): static
237239 {
238240 return new static (\convert_uuencode ($ this ->raw ));
239241 }
240242
241- protected static function stringableOrFail ($ thing ): bool
243+ /**
244+ * @param string|object $thing
245+ * @throws InvalidArgumentException
246+ */
247+ protected static function stringify (mixed $ thing ): string
242248 {
243- if (
244- is_string ( $ thing)
245- || ( \is_object ( $ thing ) && \method_exists ( $ thing , ' __toString ' ))
246- ) {
247- return true ;
249+ if (\is_object ( $ thing ) && \method_exists ( $ thing , ' __toString ' )) {
250+ return $ thing-> __toString ();
251+ }
252+ if ( \is_string ( $ thing ) ) {
253+ return $ thing ;
248254 }
249255
250256 // return false;
0 commit comments