Completed
Pull Request — develop (#27)
by Chris
13:09
created

AutoGetterSetterTrait::__call()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 2
1
<?php
2
3
namespace Chrisyue\PhpM3u8\Model;
4
5
use Stringy\Stringy as S;
6
7
trait AutoGetterSetterTrait
8
{
9
    public function __call($method, array $arguments)
10
    {
11
        $string = S::create($method);
12
        if (!$string->startsWithAny(['has', 'get', 'is', 'set'])) {
13
            throw new \BadMethodCallException($method);
14
        }
15
16
        $property = lcfirst(preg_replace('/^(has|get|is|set)/', '', $method));
17
        if ($string->startsWithAny(['set'])) {
18
            $this->$property = current($arguments);
19
20
            return $this;
21
        }
22
23
        if (property_exists($this, $property)) {
24
            return $this->$property;
25
        }
26
27
        throw new \BadMethodCallException();
28
    }
29
}
30