Completed
Push — master ( 96b2f8...da2c9c )
by Kirill
05:58
created

Serializable   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 34
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __sleep() 0 4 1
A __debugInfo() 0 4 1
A getObjectFields() 0 12 2
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Reflection\Common;
11
12
/**
13
 * Trait Serializable
14
 */
15
trait Serializable
16
{
17
    /**
18
     * @return array
19
     */
20
    public function __sleep(): array
21
    {
22
        return \array_keys(\iterator_to_array($this->getObjectFields()));
23
    }
24
25
    /**
26
     * @return array
27
     */
28
    public function __debugInfo(): array
29
    {
30
        return \iterator_to_array($this->getObjectFields());
31
    }
32
33
    /**
34
     * @return iterable|\Generator
35
     */
36
    protected function getObjectFields(): iterable
37
    {
38
        $reflection = new \ReflectionObject($this);
39
40
        $flags = \ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_PUBLIC;
41
42
        foreach ($reflection->getProperties($flags) as $field) {
43
            $field->setAccessible(true);
44
45
            yield $field->getName() => $field->getValue($this);
46
        }
47
    }
48
}
49