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 0
Metric Value
cc 4
eloc 9
c 0
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\HttpDoctrineOrmFilter\Query;
6
7
use Atlance\HttpDoctrineOrmFilter\Utils\SetterMethodName;
8
9
/**
10
 * @psalm-suppress MixedArgumentTypeCoercion
11
 */
12
abstract class AbstractCommand
13
{
14 78
    final public function __construct()
15
    {
16 78
    }
17
18 78
    public static function fromArray(array $properties = []): static
19
    {
20 78
        $object = new static();
21
        /** @psalm-var mixed $value */
22 78
        foreach ($properties as $property => $value) {
23
            /** @psalm-var string $property */
24 78
            if (property_exists(static::class, $property)) {
25 78
                $method = SetterMethodName::fromSnakeCasePropertyName($property)->getName();
26 78
                if (\is_callable([$object, $method])) {
27 78
                    $object->{$method}($value);
28
29 76
                    continue;
30
                }
31
32 1
                $object->{$property} = $value;
33
            }
34
        }
35
36 76
        return $object;
37
    }
38
}
39