Completed
Push — master ( c0d815...6cf326 )
by Pol
01:32
created

AnonymizeTrait::convertToAnonymous()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 13
nc 8
nop 1
crap 4
1
<?php
2
3
namespace drupol\Anonymize;
4
5
use drupol\DynamicObjects\DynamicObject;
6
use drupol\DynamicObjects\DynamicObjectsTrait;
7
8
/**
9
 * Trait AnonymizeTrait.
10
 *
11
 * @package drupol\Anonymize
12
 */
13
trait AnonymizeTrait
14
{
15
    use DynamicObjectsTrait;
16
17
    /**
18
     * Convert an object into an anonymous object.
19
     *
20
     * @param $object
21
     *
22
     * @return Anonymize
0 ignored issues
show
Documentation introduced by
Should the return type not be anonymous//src/AnonymizeTrait.php$0?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
23
     */
24 1
    public static function convertToAnonymous($object)
25
    {
26 1
        $reflexion = new \ReflectionClass($object);
27
        $class = new class extends DynamicObject {
28
        };
29
30 1
        foreach ($reflexion->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
31 1
            $method->setAccessible(true);
32 1
            $class::addDynamicMethod($method->name, $method->getClosure($object));
33
        }
34
35 1
        foreach ($reflexion->getMethods(\ReflectionMethod::IS_PROTECTED | \ReflectionMethod::IS_PRIVATE) as $method) {
36 1
            $method->setAccessible(false);
37 1
            $class::addDynamicMethod($method->name, $method->getClosure($object));
38
        }
39
40 1
        foreach ($reflexion->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
41 1
            $property->setAccessible(true);
42 1
            $class::addDynamicProperty($property->name, $property->getValue($object));
43
        }
44
45 1
        return $class;
46
    }
47
}
48