AbstractEntity::build()   B
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
rs 8.8571
cc 5
eloc 9
nc 9
nop 1
1
<?php
2
/*
3
 *   This file is part of the Vultr PHP library.
4
 *
5
 *   (c) Albert Leitato <[email protected]>
6
 *
7
 *   For the full copyright and license information, please view the LICENSE
8
 *   file that was distributed with this source code.
9
 */
10
namespace Vultr\Entity;
11
12
use Vultr\Support\Str;
13
14
abstract class AbstractEntity
15
{
16
    use Str;
17
18
    /**
19
     * @param \stdClass|array|null $parameters
20
     */
21
    public function __construct($parameters = null)
22
    {
23
        if (!$parameters) {
24
            return;
25
        }
26
27
        if ($parameters instanceof \stdClass) {
28
            $parameters = \get_object_vars($parameters);
29
        }
30
31
        $this->build($parameters);
32
    }
33
34
    /**
35
     * @param array $parameters
36
     */
37
    public function build(array $parameters)
38
    {
39
        foreach ($parameters as $property => $value) {
40
            $property = static::convertToCamelCase($property);
41
42
            if (\property_exists($this, $property)) {
43
                $this->$property = $value;
44
            }
45
        }
46
        if (\property_exists($this, 'dates')) {
47
            foreach ($this->dates as $value) {
0 ignored issues
show
Bug introduced by
The property dates 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...
48
                $property        = static::convertToCamelCase($value);
49
                $this->$property = static::convertDateTime($this->$property);
50
            }
51
        }
52
    }
53
}
54