Completed
Push — master ( 77b660...93750f )
by Andrii
02:18
created

HydratorTrait::hydrate()   B

Complexity

Conditions 8
Paths 24

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 7.7777
c 0
b 0
f 0
cc 8
eloc 12
nc 24
nop 1
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing;
12
13
/**
14
 * Hydrator Trait.
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
trait HydratorTrait
19
{
20
    public static function create(array $data)
21
    {
22
        return (new static())->hydrate($data);
23
    }
24
25
    protected $properties = [];
26
27
    protected function hydrate(array $data)
28
    {
29
        foreach ($def as $key => $creator) {
0 ignored issues
show
Bug introduced by
The variable $def does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
30
            if (isset($data[$key])) {
31
                if ($creator && is_array($data[$key])) {
32
                    $this->{$key} = call_user_func($creator, $data[$key]);
33
                } else {
34
                    $this->{$key} = $data[$key];
35
                }
36
            }
37
        }
38
        if (isset($data['id'])) {
39
            $this->id = $data['id'];
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40
        }
41
        if (isset($data['target'])) {
42
            $this->target = is_array($data['target']) ? AbstractTarget::create($data['target']) : $data['target'];
0 ignored issues
show
Bug introduced by
The property target does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The method create() does not seem to exist on object<hiqdev\php\billing\AbstractTarget>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
        }
44
45
        return $this;
46
    }
47
}
48