Accessors   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 4 1
A __set() 0 4 1
A callMutator() 0 8 2
A getAccessorName() 0 6 1
1
<?php
2
3
namespace N7olkachev\LaravelAccessors;
4
5
trait Accessors
6
{
7
    public function __get($key)
8
    {
9
        return $this->callMutator('get', $key);
10
    }
11
12
    public function __set($key, $value)
13
    {
14
        return $this->callMutator('set', $key, $value);
15
    }
16
17
    protected function callMutator($type, $key, $value = null)
18
    {
19
        if (method_exists($this, $method = $this->getAccessorName($type, $key))) {
20
            return $this->$method($value);
21
        } else {
22
            throw new \Exception("Undefined property: $key");
23
        }
24
    }
25
26
    protected function getAccessorName($type, $key)
27
    {
28
        $key = str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key)));
29
30
        return $type . $key . 'Attribute';
31
    }
32
}