Passed
Push — master ( a74354...254a60 )
by Andrea
18:29 queued 11s
created

StringUtils::underscore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\String;
4
5
class StringUtils
6
{
7
    /**
8
     * Translates a string with underscores into camel case (e.g. first_name -&gt; firstName).
9
     *
10
     * @param array $parametri
11
     *
12
     * @return string $str translated into camel caps
13
     */
14 3
    public static function toCamelCase($parametri = array())
15
    {
16 3
        $str = $parametri['str'];
17 3
        $capitalise_first_char = isset($parametri['primamaiuscola']) ? $parametri['primamaiuscola'] : false;
18
19 3
        if ($capitalise_first_char) {
20 3
            $str[0] = strtoupper($str[0]);
21
        }
22
        $func = function ($matches) {
23 2
            return strtoupper($matches[1]);
24 3
        };
25
26 3
        return preg_replace_callback('/_([a-z])/', $func, $str);
27
    }
28
29
      /**
30
        * Transforms an under_scored_string to a camelCasedOne
31
        */
32
    /*function camelize($scored) {
33
        return lcfirst(
34
        implode(
35
            '',
36
            array_map(
37
            'ucfirst',
38
            array_map(
39
                'strtolower',
40
                explode(
41
                '_', $scored)))));
42
    }*/
43
44
    /**
45
     * Transforms a camelCasedString to an under_scored_one
46
    */
47
    public static function underscore($cameled)
48
    {
49
        return implode(
50
            '_',
51
            array_map(
52
                'strtolower',
53
                preg_split('/([A-Z]{1}[^A-Z]*)/', $cameled, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY)
0 ignored issues
show
Bug introduced by
It seems like preg_split('/([A-Z]{1}[^...ng\PREG_SPLIT_NO_EMPTY) can also be of type false; however, parameter $arr1 of array_map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
                /** @scrutinizer ignore-type */ preg_split('/([A-Z]{1}[^A-Z]*)/', $cameled, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY)
Loading history...
54
            )
55
        );
56
    }
57
}
58