Completed
Push — master ( 063706...c4b070 )
by Dmitry
04:24
created

Entity::__call()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 2
crap 4
1
<?php
2
3
namespace Tarantool\Mapper\Entities;
4
5
use LogicException;
6
7
class Entity implements \Tarantool\Mapper\Contracts\Entity
8
{
9
    private $id;
0 ignored issues
show
Unused Code introduced by
The property $id is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
10
11 65
    public function __construct(array $data = null)
12
    {
13 65
        if ($data) {
14 65
            $this->update($data);
15
        }
16 65
    }
17
18 65
    public function update($data)
19
    {
20 65
        foreach ($data as $k => $v) {
21 65
            $this->$k = $v;
22
        }
23 65
    }
24
25 65
    public function __set($key, $value)
26
    {
27 65
        if ($key == 'id' && $this->getId()) {
28 1
            throw new LogicException('Id property is readonly');
29
        }
30
31 65
        $this->$key = $value;
32 65
    }
33
34 65
    public function __get($key)
35
    {
36 65
        if (property_exists($this, $key)) {
37 65
            return $this->$key;
38
        }
39 2
    }
40
41
    /**
42
     * @return int
43
     */
44 65
    public function getId()
45
    {
46 65
        return $this->__get('id');
47
    }
48
49
    /**
50
     * @return Entity
51
     */
52 65
    public function setId($id)
53
    {
54 65
        $this->__set('id', $id);
55
56 65
        return $this;
57
    }
58
59
    /**
60
     * @return array
61
     */
62 64
    public function toArray($recursive = false)
63
    {
64 64
        $array = [];
65 64
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Tarantool\Mapper\Entities\Entity> is not traversable.
Loading history...
66 64
            $array[$k] = $v;
67 64
            if ($v instanceof Contracts\Entity) {
0 ignored issues
show
Bug introduced by
The class Tarantool\Mapper\Entities\Contracts\Entity does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
68 64
                $array[$k] = $v->toArray($recursive);
69
            }
70
        }
71
72 64
        return $array;
73
    }
74
75 1
    public function __call($name, $params)
76
    {
77 1
        if(strlen($name) > 3) {
78 1
            $property = substr($name, 3);
79 1
            $property[0] = strtolower($property[0]);
80 1
            if(strpos($name, 'get') === 0) {
81
                return $this->__get($property);
82
            }
83 1
            if(strpos($name, 'set') === 0) {
84 1
                return $this->__set($property, $params[0]);
85
            }
86
        }
87
    }
88
}
89