Completed
Pull Request — development (#404)
by Mirko
08:26
created

ArrayUtil::humanLangImplode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace AppBundle\Util;
4
5
class ArrayUtil
6
{
7
    /**
8
     * @param string $delimiter
9
     * @param string $string
10
     *
11
     * @return array
12
     */
13
    public static function trimExplode($delimiter, $string)
14
    {
15
        $result = [];
16
        $temp = explode($delimiter, $string);
17
        foreach ($temp as $value) {
18
            $value = trim($value);
19
            if ($value !== '') {
20
                $result[] = $value;
21
            }
22
        }
23
24
        return $result;
25
    }
26
27
    /**
28
     * @param array $pieces
29
     * @param string $conjunction
30
     * @param string $glue
31
     *
32
     * @return mixed|string
33
     */
34
    public static function humanLangImplode(array $pieces, $conjunction = 'and', $glue = ',')
35
    {
36
        $lastElement = array_pop($pieces);
37
        if ($pieces) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pieces of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
38
            return implode($glue . ' ', $pieces) . ' ' . $conjunction . ' ' . $lastElement;
39
        }
40
41
        return $lastElement;
42
    }
43
}
44