ParameterManipulations   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 13
c 1
b 0
f 0
dl 0
loc 38
ccs 15
cts 15
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A fromCamelCase() 0 8 3
A fillWithSnakeParams() 0 8 3
1
<?php
2
3
4
namespace Pheanstalk\Structure;
5
6
use Doctrine\Common\Collections\ArrayCollection;
7
8
class ParameterManipulations
9
{
10
    /**
11
     * ParameterManipulations constructor.
12
     *
13
     * @param array $params
14
     *
15
     * @throws \ReflectionException
16
     */
17 37
    public function __construct(array $params)
18
    {
19 37
        $this->fillWithSnakeParams($params);
20
    }
21
22
    /**
23
     * @param        $input
24
     * @param string $glue
25
     *
26
     * @return string
27
     */
28 50
    protected function fromCamelCase($input, $glue = '_')
29
    {
30 50
        preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
31 50
        $ret = $matches[0];
32 50
        foreach ($ret as &$match) {
33 50
            $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
34
        }
35 50
        return implode($glue, $ret);
36
    }
37
38 45
    public function fillWithSnakeParams(array $params, $glue = '_')
39
    {
40 45
        $thisObject = new \ReflectionClass($this);
41 45
        $properties = $thisObject->getProperties();
42 45
        foreach ($properties as $property) {
43 45
            $snakeProperty = $this->fromCamelCase($property->getName(), $glue);
44 45
            if (isset($params[$snakeProperty])) {
45 20
                $this->{$property->getName()} = $params[$snakeProperty];
46
            }
47
        }
48
    }
49
}
50