Passed
Branch testing (fdbc4a)
by Roman
09:06
created

CassandraTypesTrait   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 23
lcom 0
cbo 0
dl 0
loc 110
ccs 75
cts 75
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isCassandraValueObject() 0 4 1
D valueFromCassandraObject() 0 89 22
1
<?php
2
3
4
namespace lroman242\LaravelCassandra;
5
6
use \Cassandra\Value;
7
8
trait CassandraTypesTrait
9
{
10
    /**
11
     * Check if object is instance of any cassandra object types
12
     *
13
     * @param $obj
14
     * @return bool
15
     */
16 35
    public function isCassandraValueObject($obj)
17
    {
18 35
        return $obj instanceof Value;
0 ignored issues
show
Bug introduced by
The class Cassandra\Value does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
19
    }
20
21
    /**
22
     * Returns comparable value from cassandra object type
23
     *
24
     * @param $obj
25
     * @return mixed
26
     */
27 21
    public function valueFromCassandraObject($obj)
28
    {
29 21
        if (is_array($obj)) {
30 2
            return array_map(function ($item) {
31 2
                return $this->valueFromCassandraObject($item);
32 2
            }, $obj);
33
        }
34
35 21
        if (!is_object($obj)) {
36 4
            return $obj;
37
        }
38
39 21
        $class = get_class($obj);
40
41 21
        $value = $obj;
42
        switch ($class) {
43 21
            case 'Cassandra\Date':
44 2
                $value = $obj->seconds();
45 2
                break;
46 20
            case 'Cassandra\Time':
47 1
                $value = $obj->__toString();
48 1
                break;
49 19
            case 'Cassandra\Timestamp':
50 1
                $value = $obj->time();
51 1
                break;
52 18
            case 'Cassandra\Float':
53 4
                $value = $obj->value();
54 4
                break;
55 17
            case 'Cassandra\Decimal':
56 1
                $value = $obj->value();
57 1
                break;
58 16
            case 'Cassandra\Inet':
59 2
                $value = $obj->address();
60 2
                break;
61 14
            case 'Cassandra\Uuid':
62 2
                $value = $obj->uuid();
63 2
                break;
64 13
            case 'Cassandra\Bigint':
65 1
                $value = $obj->value();
66 1
                break;
67 12
            case 'Cassandra\Blob':
68 1
                $value = $obj->toBinaryString();
69 1
                break;
70 11
            case 'Cassandra\Smallint':
71 1
                $value = $obj->value();
72 1
                break;
73 10
            case 'Cassandra\Timeuuid':
74 1
                $value = $obj->uuid();
75 1
                break;
76 9
            case 'Cassandra\Tinyint':
77 1
                $value = $obj->value();
78 1
                break;
79 8
            case 'Cassandra\Varint':
80 1
                $value = $obj->value();
81 1
                break;
82 7
            case 'Cassandra\Collection':
83 1
                $value = [];
84 1
                foreach ($obj->values() as $item) {
85 1
                    $value[] = $this->valueFromCassandraObject($item);
86
                }
87 1
                break;
88
//            //TODO: convert to \DateInterval
89
//            case 'Cassandra\Duration':
90
//                $value = $obj->nanos();
91
//                break;
92 6
            case 'Cassandra\Map':
93 1
                $values = array_map(function ($item) {
94 1
                    return $this->valueFromCassandraObject($item);
95 1
                }, $obj->values());
96
97 1
                $value = array_combine($obj->keys(), $values);
98 1
                break;
99 5
            case 'Cassandra\Set':
100 1
                $value = array_map(function ($item) {
101 1
                    return $this->valueFromCassandraObject($item);
102 1
                }, $obj->values());
103 1
                break;
104 4
            case 'Cassandra\Tuple':
105 1
                $value = array_map(function ($item) {
106 1
                    return $this->valueFromCassandraObject($item);
107 1
                }, $obj->values());
108 1
                break;
109 3
            case 'Cassandra\UserTypeValue':
110 1
                $value = $this->valueFromCassandraObject($obj->values());
111 1
                break;
112
        }
113
114 21
        return $value;
115
    }
116
117
}