GettersAndSetters   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 18
c 1
b 0
f 0
dl 0
loc 38
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __call() 0 23 6
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Traits;
4
5
trait GettersAndSetters
6
{
7
    /**
8
     * creates magic getters and setters
9
     * if you call $this->getFooBar() then it will get the variable FooBar even if the method
10
     * getFooBar does not exist.
11
     *
12
     * if you call $this->setFooBar('hello') then it will set the variable FooBar even if the method
13
     * setFooBar does not exist.
14
     *
15
     * See: http://php.net/manual/en/language.oop5.overloading.php#object.call
16
     *
17
     * @param  string   $function name of the function
18
     * @param  array    $args     parameters provided to the getter / setter
19
     */
20
    public function __call($function, $args)
21
    {
22
        $getOrSet = substr($function, 0, 3);
23
        if ($getOrSet === 'set' || $getOrSet === 'get') {
24
            $var = lcfirst(ltrim($function, $getOrSet));
25
            if (property_exists($this, $var)) {
26
                if ($getOrSet === 'get') {
27
                    return $this->{$var};
28
                } elseif ($getOrSet === 'set') {
29
                    $this->{$var} = $args[0];
30
31
                    return $this;
32
                }
33
            } else {
34
                user_error(
35
                    'Fatal error: can not get/set variable in ModuleUpgraderBaseWithVariables::' . $var,
36
                    E_USER_ERROR
37
                );
38
            }
39
        } else {
40
            user_error(
41
                'Fatal error: Call to undefined method ModuleUpgraderBaseWithVariables::' . $function . '()',
42
                E_USER_ERROR
43
            );
44
        }
45
    }
46
}
47