Completed
Push — master ( 26dc26...9f091f )
by Camilo
02:31
created

TelegramTypes   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 45
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B populateObject() 0 21 6
A mapSubObjects() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace unreal4u\Abstracts;
6
7
abstract class TelegramTypes
8
{
9 14
    public function __construct(\stdClass $data = null)
10
    {
11 14
        $this->populateObject($data);
12 14
    }
13
14
    /**
15
     * Fills the class with the data passed on through the constructor
16
     *
17
     * @param \stdClass $data
18
     * @return TelegramTypes
19
     */
20 14
    final protected function populateObject(\stdClass $data = null): TelegramTypes
21
    {
22 14
        if (!is_null($data)) {
23 14
            $subObjects = $this->mapSubObjects();
24 14
            foreach ($data as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $data of type object<stdClass> is not traversable.
Loading history...
25 14
                if (!empty($subObjects) && array_key_exists($key, $subObjects)) {
26 12
                    $className = 'unreal4u\\Telegram\\Types\\' . $subObjects[$key];
27 12
                    $candidateKey = new $className($value);
28 12
                    if (isset($candidateKey->data)) {
29 4
                        $this->$key = $candidateKey->data;
30
                    } else {
31 12
                        $this->$key = $candidateKey;
32
                    }
33
                } else {
34 14
                    $this->$key = $value;
35
                }
36
            }
37
        }
38
39 14
        return $this;
40
    }
41
42
    /**
43
     * The default is that we have no subobjects at all
44
     *
45
     * @return array
46
     */
47 13
    protected function mapSubObjects(): array
48
    {
49 13
        return [];
50
    }
51
}
52