Completed
Branch dev (27a5c9)
by Jakob
02:47
created

Constructor::__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
c 0
b 0
f 0
rs 8.8571
cc 5
eloc 8
nc 4
nop 2
1
<?php declare(strict_types=1);
2
3
namespace JSKOS;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Initialize field values of a new JSKOS Object.
9
 */
10
trait Constructor
11
{
12
    /**
13
     * Create a new JSKOS object with given field values.
14
     *
15
     * @param Array|Object fields to copy
16
     * @param bool strict throw error on unknown fields
17
     */
18
    public function __construct($data=null, bool $strict=false)
19
    {
20
        if (is_array($data) || is_object($data)) {
21
            foreach ($data as $key => $value) {
22
                $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...
23
            }
24
        } elseif ($data !== null) {
25
            throw new InvalidArgumentException(
26
                get_called_class() .
27
                ' constructor expects array, object, or null'
28
            );
29
        }
30
    }
31
}
32