Test Failed
Push — master ( da5def...342071 )
by Christian
04:54
created

FromArray   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 47
ccs 10
cts 13
cp 0.7692
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A snakeCaseToCamelCase() 0 3 1
A setMethod() 0 9 2
A fromArray() 0 10 2
1
<?php
2
3
namespace Omatech\Mage\Core\Domains\Shared\Traits;
4
5
use Omatech\Mage\Core\Domains\Shared\Exceptions\MethodDoesNotExistsException;
6
7
trait FromArray
8
{
9
    /**
10
     * @param array $array
11
     *
12
     * @throws MethodDoesNotExistsException
13
     *
14
     * @return static
15
     */
16 3
    public static function fromArray(array $array): self
17
    {
18 3
        $self = new static();
19
20 3
        foreach ($array as $property => $value) {
21 3
            $method = $self->setMethod($property);
22
            $self->$method($value);
23
        }
24
25
        return $self;
26
    }
27
28
    /**
29
     * @param string $property
30
     *
31
     * @throws MethodDoesNotExistsException
32
     *
33
     * @return string
34
     */
35 3
    private function setMethod(string $property): string
36
    {
37 3
        $method = $this->snakeCaseToCamelCase('set'.$property);
38
39 3
        if (! method_exists($this, $method)) {
40 3
            throw new MethodDoesNotExistsException("Method {$method} does not exists.");
41
        }
42
43
        return $method;
44
    }
45
46
    /**
47
     * @param string $string
48
     *
49
     * @return string
50
     */
51 3
    private function snakeCaseToCamelCase(string $string): string
52
    {
53 3
        return str_replace('_', '', lcfirst(ucwords($string, '_')));
54
    }
55
}
56