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

Util::copy()   D

Complexity

Conditions 10
Paths 17

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 23
nc 17
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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