Passed
Push — master ( 32f3de...67568c )
by Matthew
07:57
created

Util::resetLocaleInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 1
f 0
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 object $obj1
11
     * @param object $obj2
12
     */
13 38
    public static function copy($obj1, $obj2)
14
    {
15 38
        if (!is_object($obj1)) {
16
            throw new \InvalidArgumentException('$obj1 must be an object, not '.gettype($obj1));
17
        }
18 38
        if (!is_object($obj2)) {
19
            throw new \InvalidArgumentException('$obj2 must be an object, not '.gettype($obj2));
20
        }
21 38
        $reflection1 = new \ReflectionObject($obj1);
22 38
        $reflection2 = new \ReflectionObject($obj2);
23 38
        self::copyMethods([$obj1, $obj2], $reflection1, $reflection2);
24 38
        self::copyProperties([$obj1, $obj2], $reflection1, $reflection2);
25 38
    }
26
27
    /**
28
     * @param array[object, object] $payload
0 ignored issues
show
Documentation Bug introduced by
The doc comment array[object, object] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
29
     * @param \ReflectionObject     $reflection1
30
     * @param \ReflectionObject     $reflection2
31
     */
32 38
    private static function copyProperties(array $payload, \ReflectionObject $reflection1, \ReflectionObject $reflection2)
33
    {
34 38
        list($obj1, $obj2) = $payload;
35 38
        $publicVars = $reflection1->getProperties(\ReflectionProperty::IS_PUBLIC);
36 38
        foreach ($publicVars as $property) {
37
            $propertyName = $property->getName();
38
            if ($reflection2->hasProperty($propertyName) && $reflection2->getProperty($propertyName)->isPublic()) {
39
                $obj2->$propertyName = $obj1->$propertyName;
40
            }
41
        }
42 38
    }
43
44
    /**
45
     * @param array[object, object] $payload
0 ignored issues
show
Documentation Bug introduced by
The doc comment array[object, object] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
46
     * @param \ReflectionObject     $reflection1
47
     * @param \ReflectionObject     $reflection2
48
     */
49 38
    private static function copyMethods(array $payload, \ReflectionObject $reflection1, \ReflectionObject $reflection2)
50
    {
51 38
        list($obj1, $obj2) = $payload;
52 38
        $methods = $reflection1->getMethods(\ReflectionMethod::IS_PUBLIC);
53 38
        foreach ($methods as $method) {
54 38
            $methodName = $method->name;
55 38
            if (0 === strpos($methodName, 'get')) {
56 38
                $getMethod = $methodName;
57 38
                $setMethod = $methodName;
58 38
                $setMethod[0] = 's';
59 38
                self::copyMethod([$obj1, $obj2], $getMethod, $setMethod, $reflection2);
60
            }
61
        }
62 38
    }
63
64
    /**
65
     * @param array[object, object] $payload
0 ignored issues
show
Documentation Bug introduced by
The doc comment array[object, object] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
66
     * @param string                $getMethod
67
     * @param string                $setMethod
68
     * @param \ReflectionObject     $reflection2
69
     */
70 38
    private static function copyMethod(array $payload, $getMethod, $setMethod, \ReflectionObject $reflection2)
71
    {
72 38
        list($obj1, $obj2) = $payload;
73 38
        if ($reflection2->hasMethod($setMethod)) {
74 38
            $value = $obj1->$getMethod();
75 38
            if (null !== $value) {
76 38
                $obj2->$setMethod($value);
77
            }
78
        }
79 38
    }
80
81
    /**
82
     * @param string $varName
83
     * @param int    $var
84
     * @param int    $pow
85
     */
86 4
    public static function validateIntNull($varName, $var, $pow)
87
    {
88 4
        if (null === $var) {
0 ignored issues
show
introduced by
The condition null === $var is always false.
Loading history...
89 4
            return null;
90
        }
91 4
        if (!ctype_digit(strval($var))) {
92 1
            throw new \InvalidArgumentException("$varName must be an integer");
93
        }
94
95 4
        if (strval(intval($var)) !== strval($var) || $var < 0 || $var >= pow(2, $pow)) {
96 1
            throw new \InvalidArgumentException("$varName must be an base 10 integer within 2^$pow");
97
        }
98
99 4
        return intval($var);
100
    }
101
102 130
    public static function getMicrotimeStr()
103
    {
104 130
        $parts = explode(' ', microtime());
105 130
        $pos1 = strpos($parts[0], '.');
106
107 130
        $timeU = $parts[1].'.'.substr($parts[0], $pos1 + 1, 6);
108
109 130
        return $timeU;
110
    }
111
112 8
    public static function getMicrotimeFloatDateTime($microtime)
113
    {
114 8
        if (!is_float($microtime)) {
115
            throw new \RuntimeException("Could not create date time expected-float microtime: $microtime");
116
        }
117 8
        $result = \DateTime::createFromFormat('U.u', number_format($microtime, 6, '.', ''), new \DateTimeZone(date_default_timezone_get()));
118 8
        if (!$result) {
119
            throw new \RuntimeException("Could not create date time from float microtime: $microtime");
120
        }
121
122 8
        return $result;
123
    }
124
125
    /**
126
     * @throws \RuntimeException
127
     *
128
     * @return \DateTime
129
     */
130 129
    public static function getMicrotimeDateTime()
131
    {
132 129
        $result = \DateTime::createFromFormat('U.u', $microtime = self::getMicrotimeStr(), new \DateTimeZone(date_default_timezone_get()));
133 129
        if (!$result) {
134
            throw new \RuntimeException("Could not create date time from $microtime");
135
        }
136
137 129
        return $result;
138
    }
139
140 25
    public static function getMicrotimeInteger()
141
    {
142 25
        return self::getMicrotimeIntegerFormat(self::getMicrotimeDateTime());
143
    }
144
145 48
    public static function getMicrotimeIntegerFormat(\DateTime $dateTime)
146
    {
147 48
        $dateTimeUs = $dateTime->format('Uu');
148 48
        $dateTimeUs = str_pad($dateTimeUs, 18, '0', STR_PAD_RIGHT);
149
150 48
        return $dateTimeUs;
151
    }
152
153 34
    public static function getDateTimeFromDecimalFormat($decimal)
154
    {
155 34
        $len = strlen((string) time());
156 34
        $timePart = substr($decimal, 0, $len);
157 34
        $decimalPart = substr($decimal, $len, 6);
158
159 34
        return \DateTime::createFromFormat('U.u', "${timePart}.${decimalPart}", new \DateTimeZone(date_default_timezone_get()));
160
    }
161
}
162