Sleepify   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 0
cbo 0
dl 0
loc 41
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __sleep() 0 8 1
A __wakeup() 0 10 2
A getPropertyValue() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the light/traits.
5
 *
6
 * (c) lichunqiang <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Light\Traits\Contract;
13
14
use ReflectionClass;
15
16
trait Sleepify
17
{
18
    /**
19
     * Magic method of sleep.
20
     */
21 1
    public function __sleep()
22
    {
23 1
        $properties = (new ReflectionClass($this))->getProperties();
24
25 1
        return array_map(function ($property) {
26 1
            return $property->getName();
27 1
        }, $properties);
28
    }
29
    /**
30
     * Magic method of wake up.
31
     */
32 1
    public function __wakeup()
33
    {
34 1
        $properties = (new ReflectionClass($this))->getProperties();
35 1
        foreach ($properties as $property) {
36 1
            $property->setValue(
37 1
                $this,
38 1
                $this->getPropertyValue($property)
39 1
            );
40 1
        }
41 1
    }
42
43
    /**
44
     * Get the property value for the given property.
45
     *
46
     * @param \ReflectionProperty $property
47
     *
48
     * @return mixed
49
     */
50 1
    protected function getPropertyValue(\ReflectionProperty $property)
51
    {
52 1
        $property->setAccessible(true);
53
54 1
        return $property->getValue($this);
55
    }
56
}
57