AbstractEntity   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 40
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
B build() 0 16 5
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