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

Constructor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 22
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 13 5
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