Varint   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 6
c 4
b 1
f 1
lcom 0
cbo 2
dl 0
loc 33
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A binary() 0 5 1
A parse() 0 8 2
1
<?php
2
namespace Cassandra\Type;
3
4
class Varint extends Bigint{
5
    /**
6
     * @param int|string $value
7
     * @throws Exception
8
     */
9
    public function __construct($value = null){
10
        if ($value === null)
11
            return;
12
    
13
        if (!is_numeric($value))
14
            throw new Exception('Incoming value must be type of int.');
15
    
16
        $this->_value = (int) $value;
17
    }
18
    
19
    public static function binary($value){
20
        $higher = ($value & 0xffffffff00000000) >>32;
21
        $lower = $value & 0x00000000ffffffff;
22
        return pack('NN', $higher, $lower);
23
    }
24
    
25
    /**
26
     * @return int
27
     */
28
    public static function parse($binary){
29
        $value = 0;
30
        $length = strlen($binary);
31
        foreach (unpack('C*', $binary) as $i => $byte)
32
            $value |= $byte << (($length - $i) * 8);
33
        $shift = (\PHP_INT_SIZE - $length) * 8;
34
        return $value << $shift >> $shift;
35
    }
36
}
37