UnderscoreKeysTransformer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 44
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 6 1
A underscoreKeys() 0 14 3
A underscore() 0 6 1
1
<?php
2
3
namespace TreeHouse\Feeder\Modifier\Item\Transformer;
4
5
use Symfony\Component\HttpFoundation\ParameterBag;
6
7
class UnderscoreKeysTransformer implements TransformerInterface
8
{
9
    /**
10
     * @inheritdoc
11
     */
12 2
    public function transform(ParameterBag $item)
13
    {
14 2
        $parameters = $item->all();
15 2
        $this->underscoreKeys($parameters);
16 2
        $item->replace($parameters);
17 2
    }
18
19
    /**
20
     * @param array $arr
21
     */
22 2
    protected function underscoreKeys(array &$arr)
23
    {
24 2
        $new = [];
25
26 2
        foreach ($arr as $key => &$value) {
27 2
            if (is_array($value)) {
28 2
                $this->underscoreKeys($value);
29 2
            }
30
31 2
            $new[$this->underscore($key)] = $value;
32 2
        }
33
34 2
        $arr = $new;
35 2
    }
36
37
    /**
38
     * Copied from Doctrine's UnderscoreNamingStrategy.
39
     *
40
     * @param string $string
41
     *
42
     * @return string
43
     */
44 2
    protected function underscore($string)
45
    {
46 2
        return strtolower(
47 2
            str_replace('-', '_', preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $string))
48 2
        );
49
    }
50
}
51