Project

General

Profile

Actions

Feature #88754

closed

Codesmell GeneralUtility::trimExplode()

Added by Dieter Porth almost 5 years ago. Updated almost 5 years ago.

Status:
Closed
Priority:
Should have
Assignee:
-
Category:
Performance
Target version:
-
Start date:
2019-07-15
Due date:
% Done:

0%

Estimated time:
PHP Version:
Tags:
Complexity:
Sprint Focus:

Description

Optimize Coding of GeneralUtility:trimExplode. The Foreach-loop and the slice-Operation seems not good in the current method.

I guess, the following-code is faster.


public static function trimExplode($delim, $string, $removeEmptyValues = false, $limit = 0) {
    if ($limit>0)  {
        $result = array_map('trim',
            explode($delim, $string,$limit));
    } else  {
        $result = array_map('trim',
            explode($delim, $string));
    }  
    if ($removeEmptyValues === true) {
        $result = array_filter($result, function($k){
             return  $k !== ''
        });
    }
    return $result;
 }

The code-example and the performance is not tested yet.

Perhaps: If the $removeEmptyValues ist related to the php-Function empty(), then the filter-function is obsolete.
$result = array_filter($result)

Actions

Also available in: Atom PDF