Completed
Push — master ( 990643...934ce8 )
by Kirill
02:21
created

Properties::__unset()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
rs 8.8571
cc 5
eloc 9
nc 3
nop 1
1
<?php
2
/**
3
 * This file is part of Properties package.
4
 *
5
 * @author Serafim <[email protected]>
6
 * @date 14.04.2016 17:07
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Serafim\Properties;
12
13
use Serafim\Properties\Support\Registry;
14
use Serafim\Properties\Support\Strings as Str;
15
16
/**
17
 * Class Properties
18
 * @package Serafim\Properties
19
 */
20
trait Properties
21
{
22
    use Getters, Setters;
23
24
    /**
25
     * @param $name
26
     * @return bool
27
     */
28
    public function __isset($name)
29
    {
30
        // Try to call `getProperty` method
31
        $getter = Str::getGetter($name);
32
        if (method_exists($this, $getter)) {
33
            return true;
34
        }
35
36
        return Registry::get($this)->has($name);
37
    }
38
39
    /**
40
     * @param $name
41
     * @return bool
42
     */
43
    public function __unset($name)
44
    {
45
        $registry = Registry::get($this);
46
47
        $reflection = new \ReflectionClass($this);
48
49
        if ($registry->has($name) && $reflection->hasProperty($name)) {
50
            $property = $reflection->getProperty($name);
51
52
            if ($property->isProtected() || $property->isPublic()) {
53
                unset($this->$name);
54
                return true;
55
            }
56
57
        }
58
59
        return false;
60
    }
61
}
62