FromArrayTrait::fromArray()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Traits;
6
7
trait FromArrayTrait
8
{
9
    /**
10
    * Returns an instance of the current class
11
    *
12
    * @param array<string,mixed> $data
13
    * @return mixed
14
    */
15
    public static function fromArray(array $data = [])
16
    {
17
        $object = new static();
18
        foreach (\get_object_vars($object) as $property => $default) {
19
            if (!\array_key_exists($property, $data)) {
20
                continue;
21
            }
22
23
            $object->{$property} = $data[$property];
24
        }
25
        return $object;
26
    }
27
}
28