Completed
Push — master ( 745336...aadd5d )
by Matthew
06:17
created

Util::validateIntNull()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 8
nc 4
nop 3
crap 6
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 22
    public static function copy($obj1, $obj2)
14
    {
15 22
        if (!is_object($obj1)) {
16
            throw new \Exception('$obj1 must be an object, not '.gettype($obj1));
17
        }
18 22
        if (!is_object($obj2)) {
19
            throw new \Exception('$obj2 must be an object, not '.gettype($obj2));
20
        }
21 22
        $reflection1 = new \ReflectionObject($obj1);
22 22
        $reflection2 = new \ReflectionObject($obj2);
23 22
        self::copyMethods($obj1, $obj2, $reflection1, $reflection2);
24 22
        self::copyProperties($obj1, $obj2, $reflection1, $reflection2);
25 22
    }
26
27 22
    private static function copyProperties($obj1, $obj2, \ReflectionObject $reflection1, \ReflectionObject $reflection2)
28
    {
29 22
        $publicVars = $reflection1->getProperties(\ReflectionProperty::IS_PUBLIC);
30 22
        foreach ($publicVars as $property) {
31
            $propertyName = $property->getName();
32
            if ($reflection2->hasProperty($propertyName) && $reflection2->getProperty($propertyName)->isPublic()) {
33
                $obj2->$propertyName = $obj1->$propertyName;
34
            }
35
        }
36 22
    }
37
38 22
    private static function copyMethods($obj1, $obj2, \ReflectionObject $reflection1, \ReflectionObject $reflection2)
39
    {
40 22
        $methods = $reflection1->getMethods(\ReflectionMethod::IS_PUBLIC);
41 22
        foreach ($methods as $method) {
42 22
            $methodName = $method->name;
43 22
            if (0 === strpos($methodName, 'get')) {
44 22
                $getMethod = $methodName;
45 22
                $setMethod = $methodName;
46 22
                $setMethod[0] = 's';
47 22
                self::copyMethod($obj1, $obj2, $getMethod, $setMethod, $reflection2);
48
            }
49
        }
50 22
    }
51
52 22
    private static function copyMethod($obj1, $obj2, $getMethod, $setMethod, \ReflectionObject $reflection2)
53
    {
54 22
        if ($reflection2->hasMethod($setMethod)) {
55 22
            $value = $obj1->$getMethod();
56 22
            if (null !== $value) {
57 22
                $obj2->$setMethod($value);
58
            }
59
        }
60 22
    }
61
62
    /**
63
     * @param string $varName
64
     * @param int    $pow
65
     */
66 4
    public static function validateIntNull($varName, $var, $pow)
67
    {
68 4
        if (null === $var) {
69 4
            return null;
70
        }
71 4
        if (!ctype_digit(strval($var))) {
72 1
            throw new \Exception("$varName must be an integer");
73
        }
74
75 4
        if (strval(intval($var)) !== strval($var) || $var <= 0 || $var >= pow(2, $pow)) {
76 3
            throw new \Exception("$varName must be an base 10 integer within 2^$pow");
77
        }
78
79 4
        return intval($var);
80
    }
81
}
82