AbstractCommand::fromArray()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 4
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Atlance\HttpDbalFilter\Dto;
6
7
use Atlance\HttpDbalFilter\Utils\SetterMethodName;
8
9
/**
10
 * @psalm-suppress MixedArgumentTypeCoercion
11
 */
12
abstract class AbstractCommand
13
{
14 75
    final public function __construct()
15
    {
16 75
    }
17
18 75
    public static function fromArray(array $properties = []): static
19
    {
20 75
        $object = new static();
21
        /** @psalm-var mixed $value */
22 75
        foreach ($properties as $property => $value) {
23
            /** @psalm-var string $property */
24 75
            if (property_exists(static::class, $property)) {
25 75
                $method = SetterMethodName::fromSnakeCasePropertyName($property)->getName();
26 75
                if (\is_callable([$object, $method])) {
27 75
                    $object->{$method}($value);
28
29 73
                    continue;
30
                }
31
32 1
                $object->{$property} = $value;
33
            }
34
        }
35
36 73
        return $object;
37
    }
38
}
39