Completed
Push — master ( 71be44...8b1d39 )
by Jakob
01:52
created

ObjectConstructor::__construct()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 4
nop 2
1
<?php declare(strict_types=1);
2
3
namespace JSKOS;
4
5
use InvalidArgumentException;
6
7
trait ObjectConstructor
8
{
9
    /**
10
     * Create a new JSKOS object.
11
     *
12
     * @param Array|Object JSON data to copy
13
     */
14
    public function __construct($data=null, bool $strict=false)
15
    {
16
        if (is_array($data) || is_object($data)) {
17
            foreach ($data as $key => $value) {
18
                $this->setField($key, $value, $strict);
0 ignored issues
show
Bug introduced by
It seems like setField() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
19
            }
20
        } elseif ($data !== null) {
21
            throw new InvalidArgumentException(
22
                get_called_class() .
23
                ' constructor expects array, object, or JSON string'
24
            );
25
        }
26
    }
27
}
28