Completed
Push — master ( c62de5...a864f4 )
by Johnny
02:23
created

EntityAbstract::__call()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 32
ccs 0
cts 28
cp 0
rs 8.439
cc 5
eloc 23
nc 5
nop 2
crap 30
1
<?php
2
namespace Redbox\Twitch\Entity;
3
4
abstract class EntityAbstract
5
{
6
    /**
7
     * Generate magic functions.
8
     *
9
     * @param string $name
10
     * @param array $args
11
     * @return null
12
     */
13
    public function __call($name = '', $args = [])
14
    {
15
        $action   = strtolower(substr($name,0, 3));
16
        $trace    = debug_backtrace();
17
18
        switch($action) {
19
            case 'get':
20
                $name   = substr($name, 3);
21
                $param  = lcfirst($name);
22
23
                if (property_exists($this, $param) === true){
24
                    return $this->$param;
25
                } else {
26
                    $this->trigger_error($name, $trace);
27
                }
28
                break;
29
30
            case 'set':
31
                $name   = substr($name, 3);
32
                $param = lcfirst($name);
33
34
                if (property_exists($this, $param) === true){
35
                    $this->$param = $args[0];
36
                } else {
37
                    $this->trigger_error($name, $trace);
38
                }
39
            break;
40
            default:
41
                $this->trigger_error($name, $trace);
42
                break;
43
        }
44
    }
45
46
    /**
47
     * @param string $name
48
     * @param array $trace
49
     */
50
    private function trigger_error($name = '', $trace = array()) {
51
        trigger_error(
52
            'Call to undefined method: '.$name.
53
            ' in '.$trace[0]['file'].
54
            ' on line '.$trace[0]['line'],
55
            E_USER_ERROR);
56
    }
57
58
}