Passed
Push — master ( 23e15f...93b7a9 )
by ma
02:33 queued 14s
created
src/UniqueId.php 2 patches
Spacing   +46 added lines, -46 removed lines patch added patch discarded remove patch
@@ -34,7 +34,7 @@  discard block
 block discarded – undo
34 34
  *
35 35
  * php -r 'echo dechex( 0b00111111 );'
36 36
  */
37
-class UniqueId{
37
+class UniqueId {
38 38
 
39 39
 
40 40
     protected static $g_cInstanceDId;
@@ -50,12 +50,12 @@  discard block
 block discarded – undo
50 50
     /**
51 51
      * __construct
52 52
      */
53
-    public function __construct(){}
53
+    public function __construct() {}
54 54
 
55 55
     /**
56 56
      * __destruct
57 57
      */
58
-    public function __destruct(){}
58
+    public function __destruct() {}
59 59
 
60 60
     /**
61 61
      * getInstance
@@ -63,7 +63,7 @@  discard block
 block discarded – undo
63 63
      */
64 64
     static function getInstance()
65 65
     {
66
-        if ( is_null( self::$g_cInstanceDId ) || ! isset( self::$g_cInstanceDId ) )
66
+        if (is_null(self::$g_cInstanceDId) || !isset(self::$g_cInstanceDId))
67 67
         {
68 68
             self::$g_cInstanceDId = new self();
69 69
         }
@@ -80,42 +80,42 @@  discard block
 block discarded – undo
80 80
      *	@param $arrData &array	details about the id
81 81
      *	@return int(64)	id
82 82
      */
83
-    public function createId( $nCenter, $nNode, $sSource = null, & $arrData = null )
83
+    public function createId($nCenter, $nNode, $sSource = null, & $arrData = null)
84 84
     {
85
-        if ( ! $this->isValidCenterId( $nCenter ) )
85
+        if (!$this->isValidCenterId($nCenter))
86 86
         {
87 87
             return null;
88 88
         }
89
-        if ( ! $this->isValidNodeId( $nNode ) )
89
+        if (!$this->isValidNodeId($nNode))
90 90
         {
91 91
             return null;
92 92
         }
93 93
 
94 94
         //	...
95
-        $nRet	= 0;
96
-        $nTime	= $this->getEscapedTime();
95
+        $nRet = 0;
96
+        $nTime = $this->getEscapedTime();
97 97
 
98
-        if ( is_string( $sSource ) && strlen( $sSource ) > 0 )
98
+        if (is_string($sSource) && strlen($sSource) > 0)
99 99
         {
100 100
             //	use crc32 hash value instead of rand
101
-            $nRand	= crc32( $sSource );
101
+            $nRand = crc32($sSource);
102 102
         }
103 103
         else
104 104
         {
105 105
             //	0 ~ 4095
106
-            $nRand	= rand( 0, 0xFFF );
106
+            $nRand = rand(0, 0xFFF);
107 107
         }
108 108
 
109 109
         //	...
110
-        $nCenterV	= ( ( $nCenter << 17 ) & 0x00000000003E0000 );
111
-        $nNodeV		= ( ( $nNode   << 12 ) & 0x000000000001F000 );
112
-        $nTimeV		= ( ( $nTime   << 22 ) & 0x7FFFFFFFFFC00000 );
113
-        $nRandV		= ( ( $nRand   << 0  ) & 0x0000000000000FFF );
110
+        $nCenterV = (($nCenter << 17) & 0x00000000003E0000);
111
+        $nNodeV		= (($nNode << 12) & 0x000000000001F000);
112
+        $nTimeV		= (($nTime << 22) & 0x7FFFFFFFFFC00000);
113
+        $nRandV		= (($nRand << 0) & 0x0000000000000FFF);
114 114
 
115
-        $nRet		= ( $nCenterV + $nNodeV + $nTimeV + $nRandV );
115
+        $nRet = ($nCenterV + $nNodeV + $nTimeV + $nRandV);
116 116
 
117 117
         //	...
118
-        if ( ! is_null( $arrData ) )
118
+        if (!is_null($arrData))
119 119
         {
120 120
             $arrData =
121 121
                 [
@@ -126,7 +126,7 @@  discard block
 block discarded – undo
126 126
                 ];
127 127
         }
128 128
 
129
-        return intval( $nRet );
129
+        return intval($nRet);
130 130
     }
131 131
 
132 132
     /**
@@ -135,18 +135,18 @@  discard block
 block discarded – undo
135 135
      *	@param $nId int		64 bits unique id
136 136
      *	@return array		details about the id
137 137
      */
138
-    public function parseId( $nId )
138
+    public function parseId($nId)
139 139
     {
140
-        if ( ! is_numeric( $nId ) || $nId <= 0 )
140
+        if (!is_numeric($nId) || $nId <= 0)
141 141
         {
142 142
             return null;
143 143
         }
144 144
 
145 145
         //	...
146
-        $nCenter	= ( ( $nId & 0x00000000003E0000 ) >> 17 );
147
-        $nNode		= ( ( $nId & 0x000000000001F000 ) >> 12 );
148
-        $nTime		= ( ( $nId & 0x7FFFFFFFFFC00000 ) >> 22 );
149
-        $nRand		= ( ( $nId & 0x0000000000000FFF ) >> 0  );
146
+        $nCenter = (($nId & 0x00000000003E0000) >> 17);
147
+        $nNode		= (($nId & 0x000000000001F000) >> 12);
148
+        $nTime		= (($nId & 0x7FFFFFFFFFC00000) >> 22);
149
+        $nRand		= (($nId & 0x0000000000000FFF) >> 0);
150 150
 
151 151
         return
152 152
             [
@@ -163,20 +163,20 @@  discard block
 block discarded – undo
163 163
      *	@param $nVal int	64 bits unique id
164 164
      *	@return boolean		true or false
165 165
      */
166
-    public function isValidId( $nVal )
166
+    public function isValidId($nVal)
167 167
     {
168 168
         $bRet	= false;
169
-        $arrD	= $this->parseId( $nVal );
170
-        if ( is_array( $arrD ) &&
171
-            array_key_exists( 'center', $arrD ) &&
172
-            array_key_exists( 'node', $arrD ) &&
173
-            array_key_exists( 'time', $arrD ) &&
174
-            array_key_exists( 'rand', $arrD ) )
169
+        $arrD	= $this->parseId($nVal);
170
+        if (is_array($arrD) &&
171
+            array_key_exists('center', $arrD) &&
172
+            array_key_exists('node', $arrD) &&
173
+            array_key_exists('time', $arrD) &&
174
+            array_key_exists('rand', $arrD))
175 175
         {
176
-            if ( $this->isValidCenterId( $arrD[ 'center' ] ) &&
177
-                $this->isValidNodeId( $arrD[ 'node' ] ) &&
178
-                $this->isValidTime( $arrD[ 'time' ] ) &&
179
-                $this->isValidRand( $arrD[ 'rand' ] ) )
176
+            if ($this->isValidCenterId($arrD['center']) &&
177
+                $this->isValidNodeId($arrD['node']) &&
178
+                $this->isValidTime($arrD['time']) &&
179
+                $this->isValidRand($arrD['rand']))
180 180
             {
181 181
                 $bRet = true;
182 182
             }
@@ -190,36 +190,36 @@  discard block
 block discarded – undo
190 190
      * @param $nVal
191 191
      * @return bool
192 192
      */
193
-    public function isValidCenterId( $nVal )
193
+    public function isValidCenterId($nVal)
194 194
     {
195
-        return is_numeric( $nVal ) && ( $nVal >= 0 ) && ( $nVal <= 31 );
195
+        return is_numeric($nVal) && ($nVal >= 0) && ($nVal <= 31);
196 196
     }
197 197
 
198 198
     /**
199 199
      * @param $nVal
200 200
      * @return bool
201 201
      */
202
-    public function isValidNodeId( $nVal )
202
+    public function isValidNodeId($nVal)
203 203
     {
204
-        return is_numeric( $nVal ) && ( $nVal >= 0 ) && ( $nVal <= 31 );
204
+        return is_numeric($nVal) && ($nVal >= 0) && ($nVal <= 31);
205 205
     }
206 206
 
207 207
     /**
208 208
      * @param $nVal
209 209
      * @return bool
210 210
      */
211
-    public function isValidTime( $nVal )
211
+    public function isValidTime($nVal)
212 212
     {
213
-        return is_numeric( $nVal ) && ( $nVal >= 0 );
213
+        return is_numeric($nVal) && ($nVal >= 0);
214 214
     }
215 215
 
216 216
     /**
217 217
      * @param $nVal
218 218
      * @return bool
219 219
      */
220
-    public function isValidRand( $nVal )
220
+    public function isValidRand($nVal)
221 221
     {
222
-        return is_numeric( $nVal ) && ( $nVal >= 0 ) && ( $nVal <= 0xFFF );
222
+        return is_numeric($nVal) && ($nVal >= 0) && ($nVal <= 0xFFF);
223 223
     }
224 224
 
225 225
 
@@ -230,7 +230,7 @@  discard block
 block discarded – undo
230 230
      */
231 231
     public function getUnixTimestamp()
232 232
     {
233
-        return floor( microtime( true ) * 1000 );
233
+        return floor(microtime(true) * 1000);
234 234
     }
235 235
 
236 236
     /**
@@ -240,6 +240,6 @@  discard block
 block discarded – undo
240 240
      */
241 241
     public function getEscapedTime()
242 242
     {
243
-        return intval( $this->getUnixTimestamp() - self::EPOCH_OFFSET );
243
+        return intval($this->getUnixTimestamp() - self::EPOCH_OFFSET);
244 244
     }
245 245
 }
Please login to merge, or discard this patch.
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -99,8 +99,7 @@
 block discarded – undo
99 99
         {
100 100
             //	use crc32 hash value instead of rand
101 101
             $nRand	= crc32( $sSource );
102
-        }
103
-        else
102
+        } else
104 103
         {
105 104
             //	0 ~ 4095
106 105
             $nRand	= rand( 0, 0xFFF );
Please login to merge, or discard this patch.