AnonymizeTrait::convertToAnonymous()
last analyzed

Size

Total Lines 40
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 40
ccs 20
cts 20
cp 1
c 3
b 0
f 0
eloc 20
nc 36
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\Anonymize;
6
7
/**
8
 * Trait AnonymizeTrait.
9
 */
10
trait AnonymizeTrait
11
{
12
    /**
13
     * Convert an object into an anonymous object.
14
     *
15
     * @param object $object
16
     *
17
     * @throws \ReflectionException
18
     *
19
     * @return Anonymize
20
     */
21 1
    public static function convertToAnonymous($object)
22
    {
23 1
        $reflection = new \ReflectionClass($object);
24
        $class = new class() extends Anonymize {
25
        };
26
27 1
        $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
28 1
29 1
        foreach ($methods as $method) {
30
            $closure = $method->getClosure($object);
31 1
32 1
            if ($closure instanceof \Closure) {
33
                $class::addDynamicMethod($method->name, $closure);
34
            }
35
        }
36 1
37 1
        $methods = $reflection->getMethods(\ReflectionMethod::IS_PROTECTED | \ReflectionMethod::IS_PRIVATE);
38 1
39
        foreach ($methods as $method) {
40 1
            $closure = $method->getClosure($object);
41 1
42
            if ($closure instanceof \Closure) {
43
                $class::addDynamicMethod($method->name, $closure);
44
            }
45 1
        }
46 1
47 1
        $properties = $reflection->getProperties(\ReflectionProperty::IS_PUBLIC);
48
49
        foreach ($properties as $property) {
50 1
            $class::addDynamicProperty($property->name, $property->getValue($object));
51 1
        }
52 1
53 1
        $properties = $reflection->getProperties(\ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_PRIVATE);
54
55
        foreach ($properties as $property) {
56 1
            $property->setAccessible(true);
57
            $class::addDynamicProperty($property->name, $property->getValue($object));
58
        }
59
60
        return $class;
61
    }
62
}
63