Skip to content

Commit 73016e5

Browse files
committedDec 5, 2024·
Removed retuning closures where it has no advantages
1 parent a20e737 commit 73016e5

File tree

1 file changed

+25
-33
lines changed

1 file changed

+25
-33
lines changed
 

‎src/Map.php

+25-33
Original file line numberDiff line numberDiff line change
@@ -203,28 +203,26 @@ public static function delimiter( ?string $char = null ) : string
203203
*/
204204
public static function explode( string $delimiter, string $string, int $limit = PHP_INT_MAX ) : self
205205
{
206-
return new static( function() use ( $delimiter, $string, $limit ) {
206+
if( $delimiter !== '' ) {
207+
return new static( explode( $delimiter, $string, $limit ) );
208+
}
207209

208-
if( $delimiter !== '' ) {
209-
return explode( $delimiter, $string, $limit );
210-
}
210+
$limit = $limit ?: 1;
211+
$parts = mb_str_split( $string );
211212

212-
$limit = $limit ?: 1;
213-
$parts = mb_str_split( $string );
213+
if( $limit < 1 ) {
214+
return new static( array_slice( $parts, 0, $limit ) );
215+
}
214216

215-
if( $limit < 1 ) {
216-
return array_slice( $parts, 0, $limit );
217-
}
217+
if( $limit < count( $parts ) )
218+
{
219+
$result = array_slice( $parts, 0, $limit );
220+
$result[] = join( '', array_slice( $parts, $limit ) );
218221

219-
if( $limit < count( $parts ) )
220-
{
221-
$result = array_slice( $parts, 0, $limit );
222-
$result[] = join( '', array_slice( $parts, $limit ) );
223-
return $result;
224-
}
222+
return new static( $result );
223+
}
225224

226-
return $parts;
227-
} );
225+
return new static( $parts );
228226
}
229227

230228

@@ -286,14 +284,11 @@ public static function from( $elements = [] ) : self
286284
*/
287285
public static function fromJson( string $json, int $options = JSON_BIGINT_AS_STRING ) : self
288286
{
289-
return new static( function() use ( $json, $options ) {
290-
291-
if( ( $result = json_decode( $json, true, 512, $options ) ) !== null ) {
292-
return $result;
293-
}
287+
if( ( $result = json_decode( $json, true, 512, $options ) ) !== null ) {
288+
return new static( $result );
289+
}
294290

295-
throw new \RuntimeException( 'Not a valid JSON string: ' . $json );
296-
} );
291+
throw new \RuntimeException( 'Not a valid JSON string: ' . $json );
297292
}
298293

299294

@@ -357,17 +352,14 @@ public static function method( string $method, ?\Closure $fcn = null ) : ?\Closu
357352
*/
358353
public static function times( int $num, \Closure $callback ) : self
359354
{
360-
return new static( function() use ( $num, $callback ) {
361-
362-
$list = [];
355+
$list = [];
363356

364-
for( $i = 0; $i < $num; $i++ ) {
365-
$key = $i;
366-
$list[$key] = $callback( $i, $key );
367-
}
357+
for( $i = 0; $i < $num; $i++ ) {
358+
$key = $i;
359+
$list[$key] = $callback( $i, $key );
360+
}
368361

369-
return $list;
370-
} );
362+
return new static( $list );
371363
}
372364

373365

0 commit comments

Comments
 (0)
Please sign in to comment.