Test Setup Failed
Push — master ( 298fd3...56daff )
by Matthew
17:14
created

Util   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 0
dl 0
loc 43
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D copy() 0 34 10
1
<?php
2
3
namespace Dtc\QueueBundle\Util;
4
5
class Util
6
{
7
    /**
8
     * Copies the members of obj1 that have public getters to obj2 if there exists a public setter in obj2 of the same suffix, it will also copy public variables.
9
     *
10
     * @param $obj1
11
     * @param $obj2
12
     */
13
    public static function copy($obj1, $obj2)
14
    {
15
        if (!is_object($obj1)) {
16
            throw new \Exception('$obj1 must be an object, not '.gettype($obj1));
17
        }
18
        if (!is_object($obj2)) {
19
            throw new \Exception('$obj2 must be an object, not '.gettype($obj2));
20
        }
21
        $reflection1 = new \ReflectionObject($obj1);
22
        $reflection2 = new \ReflectionObject($obj2);
23
24
        $methods = $reflection1->getMethods(\ReflectionMethod::IS_PUBLIC);
25
        $publicVars = $reflection1->getProperties(\ReflectionProperty::IS_PUBLIC);
26
        foreach ($methods as $method) {
27
            $methodName = $method->getName();
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
28
            if (0 === strpos($methodName, 'get')) {
29
                $getMethod = $methodName;
30
                $setMethod = $methodName;
31
                $setMethod[0] = 's';
32
                if ($reflection2->hasMethod($setMethod)) {
33
                    $value = $obj1->$getMethod();
34
                    if (null !== $value) {
35
                        $obj2->$setMethod($value);
36
                    }
37
                }
38
            }
39
        }
40
        foreach ($publicVars as $property) {
41
            $propertyName = $property->getName();
42
            if ($reflection2->hasPropery($propertyName) && $reflection2->getProperty($propertyName)->isPublic()) {
0 ignored issues
show
Bug introduced by
The method hasPropery() does not exist on ReflectionObject. Did you maybe mean hasProperty()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
43
                $obj2->$propertyName = $obj1->$propertyName;
44
            }
45
        }
46
    }
47
}
48