Entity   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 85%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 20
c 1
b 0
f 0
dl 0
loc 47
ccs 17
cts 20
cp 0.85
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequiredProperties() 0 12 3
A __construct() 0 25 6
1
<?php
2
3
namespace Anticaptcha;
4
5
use InvalidArgumentException;
6
use ReflectionClass;
7
use ReflectionProperty;
8
9
abstract class Entity
10
{
11 7
    public function __construct(array $properties = [])
12
    {
13
        /* Validate required */
14
15 7
        foreach ($this->getRequiredProperties() as $property) {
16 5
            if (!isset($properties[$property])) {
17 1
                throw new InvalidArgumentException(
18 1
                    sprintf('Property "%s" is required.', $property)
19
                );
20
            }
21
        }
22
23
        /**
24
         * @var string $option
25
         * @var mixed $value
26
         */
27 7
        foreach ($properties as $option => $value) {
28 7
            $setter = 'set' . ucfirst($option);
29 7
            if (method_exists($this, $setter)) {
30
                call_user_func([$this, $setter], $value);
31 7
            } elseif (property_exists($this, $option)) {
32 7
                $this->$option = $value;
33
            } else {
34
                throw new InvalidArgumentException(
35
                    sprintf('Property "%s" not found in class "%s".', $option, static::class)
36
                );
37
            }
38
        }
39
    }
40
41
    /**
42
     * @return string[]
43
     */
44 7
    protected function getRequiredProperties(): array
45
    {
46 7
        $properties = [];
47
48 7
        foreach ((new ReflectionClass(static::class))->getProperties(ReflectionProperty::IS_PUBLIC) as $reflection) {
49 7
            if ($reflection->hasDefaultValue()) {
50 7
                continue;
51
            }
52 5
            $properties[] = $reflection->getName();
53
        }
54
55 7
        return $properties;
56
    }
57
}
58