Constructor   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 34
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
setField() 0 1 ?
B __construct() 0 20 8
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
     * Set a field value.
14
     */
15
    abstract protected function setField(string $field, $value, bool $strict = true);
16
17
    /**
18
     * Create a new JSKOS object with given field values.
19
     *
20
     * @param Array|Object fields to copy
21
     * @param bool strict throw error on unknown fields
22
     */
23
    public function __construct($data = null, bool $strict = false)
24
    {
25
        if (is_array($data) || is_object($data)) {
26
            foreach ($data as $key => $value) {
27
                $this->setField($key, $value, $strict);
28
            }
29
        } elseif ($data !== null) {
30
            throw new InvalidArgumentException(
31
                get_called_class() .
32
                ' constructor expects array, object, or null'
33
            );
34
        }
35
36
        if ($this instanceof Resource && !count($this->type ?? [])) {
0 ignored issues
show
Documentation introduced by
The property $type is declared protected in JSKOS\Resource. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
37
            $class = get_called_class();
38
            if (count($class::TYPES)) {
39
                $this->type = new Listing([$class::TYPES[0]]);
0 ignored issues
show
Documentation introduced by
The property $type is declared protected in JSKOS\Resource. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
40
            }
41
        }
42
    }
43
}
44