Completed
Push — master ( a6a584...311c12 )
by Camilo
02:12
created

AbstractFiller::populateObject()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6
Metric Value
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 8.7624
cc 6
eloc 14
nc 2
nop 1
crap 6
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 7 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

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