Failed Conditions
Branch master (69f595)
by Gawain
07:16
created

MagicAttributeTrait::__call()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 31
rs 5.1234
cc 13
eloc 20
nc 13
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Bolt\Storage\Entity;
3
4
/**
5
 * Provides access to entity attributes and the schema-less _fields
6
 * attribute via __get and __set magic methods.
7
 *
8
 * @author Ross Riley <[email protected]>
9
 */
10
trait MagicAttributeTrait
11
{
12
    public $_fields = [];
13
14
    public function __get($key)
15
    {
16
        $method = 'get' . ucfirst($key);
17
        if (in_array($key, $this->getFields())) {
18
            return $this->$method();
19
        }
20
    }
21
22
    public function __set($key, $value)
23
    {
24
        $method = 'set' . ucfirst($key);
25
        $this->$method($value);
26
    }
27
28
    public function __isset($key)
29
    {
30
        if ($this->has($key)) {
31
            return true;
32
        }
33
34
        return false;
35
    }
36
37
    public function __unset($key)
38
    {
39
        if ($this->has($key) && property_exists($this, $key)) {
40
            unset($this->$key);
41
        } elseif ($this->has($key)) {
42
            unset($this->_fields[$key]);
43
        }
44
45
        return false;
46
    }
47
48
    public function __call($method, $arguments)
49
    {
50
        $var = lcfirst(substr($method, 3));
51
        $underscored = $this->underscore($var);
52
53
        if (strncasecmp($method, 'get', 3) == 0) {
54
            if ($this->has($var) && property_exists($this, $var)) {
55
                return $this->$var;
56
            } elseif ($this->has($underscored) && property_exists($this, $underscored)) {
57
                return $this->$underscored;
58
            } elseif ($this->has($var)) {
59
                return $this->_fields[$var];
60
            }
61
        }
62
63
        if (strncasecmp($method, 'serialize', 9) == 0) {
64
            $method = 'get' . substr($method, 9);
65
66
            return $this->$method();
67
        }
68
69
        if (strncasecmp($method, 'set', 3) == 0) {
70
            if ($this->has($var) && property_exists($this, $var)) {
71
                $this->$var = $arguments[0];
72
            } elseif ($this->has($underscored) && property_exists($this, $underscored)) {
73
                $this->$underscored = $arguments[0];
74
            } else {
75
                $this->_fields[$var] = $arguments[0];
76
            }
77
        }
78
    }
79
80
    /**
81
     * An internal method that builds a list of available fields depending on context
82
     *
83
     * @return array
84
     **/
85
    protected function getFields()
86
    {
87
        $fields = [];
88
89
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Bolt\Storage\Entity\MagicAttributeTrait> is not traversable.
Loading history...
90
            if (strpos($k, '_') !== 0) {
91
                $fields[] = $k;
92
            }
93
        }
94
95
        foreach ($this->_fields as $k => $v) {
96
            $fields[] = $k;
97
        }
98
99
        return $fields;
100
    }
101
102
    /**
103
     * Boolean check on whether entity has field
104
     *
105
     * @param string $field
106
     *
107
     * @return bool
108
     */
109
    protected function has($field)
110
    {
111
        return in_array($field, $this->getFields());
112
    }
113
114
    /**
115
     * Converts a string from underscored to Camel Case.
116
     *
117
     * @param string $id A string to camelize
118
     *
119
     * @return string The camelized string
120
     */
121
    public function camelize($id)
122
    {
123
        return strtr(ucwords(strtr($id, array('_' => ' ', '.' => '_ ', '\\' => '_ '))), array(' ' => ''));
124
    }
125
126
    /**
127
     * Converts a string from camel case to underscored.
128
     *
129
     * @param string $id The string to underscore
130
     *
131
     * @return string The underscored string
132
     */
133
    public function underscore($id)
134
    {
135
        return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), str_replace('_', '.', $id)));
136
    }
137
138
}
139