Batch   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 24.35 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 10
Bugs 3 Features 2
Metric Value
wmc 10
c 10
b 3
f 2
lcom 1
cbo 3
dl 28
loc 115
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 3
A getBody() 0 5 1
A appendQuery() 10 10 1
A appendQueryId() 10 10 1
B queryParameters() 8 25 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Cassandra\Request;
3
use Cassandra\Protocol\Frame;
4
use Cassandra\Protocol;
5
use Cassandra\Connection;
6
use Cassandra\Type;
7
8
class Batch extends Request{
9
    const TYPE_LOGGED = 0;
10
    const TYPE_UNLOGGED = 1;
11
    const TYPE_COUNTER = 2;
12
    
13
    protected $opcode = Frame::OPCODE_BATCH;
14
    
15
    /**
16
     * @var array
17
     */
18
    protected $_queryArray = [];
19
20
    /**
21
     * @var int
22
     */
23
    protected $_batchType;
24
    
25
    /**
26
     * 
27
     * @var int
28
     */
29
    protected $_consistency;
30
    
31
    /**
32
     * 
33
     * @var array
34
     */
35
    protected $_options;
36
    
37
    /**
38
     * 
39
     * @param string $type
40
     * @param string $consistency
41
     * @param array $options
42
     */
43
    public function __construct($type = null, $consistency = null, $options = []) {
44
        $this->_batchType = $type === null ? Batch::TYPE_LOGGED : $type;
45
        $this->_consistency = $consistency === null ? Request::CONSISTENCY_ONE : $consistency;
46
        $this->_options = $options;
47
    }
48
49
    /**
50
     * Exec transaction
51
     */
52
    public function getBody() {
53
        return pack('C', $this->_batchType)
54
            . pack('n', count($this->_queryArray)) . implode('', $this->_queryArray)
55
            . self::queryParameters($this->_consistency, $this->_options);
56
    }
57
58
    /**
59
     * @param string $cql
60
     * @param array $values
61
     * @return self
62
     */
63 View Code Duplication
    public function appendQuery($cql, array $values = []) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
        $binary = pack('C', 0);
65
    
66
        $binary .= pack('N', strlen($cql)) . $cql;
67
        $binary .= Request::valuesBinary($values, !empty($this->_options['names_for_values']));
68
        
69
        $this->_queryArray[] = $binary;
70
        
71
        return $this;
72
    }
73
    
74
    /**
75
     * 
76
     * @param string $queryId
77
     * @param array $values
78
     * @return self
79
     */
80 View Code Duplication
    public function appendQueryId($queryId, array $values = []) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
        $binary = pack('C', 1);
82
        
83
        $binary .= pack('n', strlen($queryId)) . $queryId;
84
        $binary .= Request::valuesBinary($values, !empty($this->_options['names_for_values']));
85
        
86
        $this->_queryArray[] = $binary;
87
        
88
        return $this;
89
    }
90
    
91
    /**
92
     *
93
     * @param int $consistency
94
     * @param array $options
95
     * @return string
96
     */
97
    public static function queryParameters($consistency, array $options = []){
98
        $flags = 0;
99
        $optional = '';
100
    
101 View Code Duplication
        if (isset($options['serial_consistency'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
            $flags |= Query::FLAG_WITH_SERIAL_CONSISTENCY;
103
            $optional .= pack('n', $options['serial_consistency']);
104
        }
105
    
106 View Code Duplication
        if (isset($options['default_timestamp'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
            $flags |= Query::FLAG_WITH_DEFAULT_TIMESTAMP;
108
            $optional .= Type\Bigint::binary($options['default_timestamp']);
109
        }
110
    
111
        if (!empty($options['names_for_values'])){
112
            /**
113
             * @link https://github.com/duoshuo/php-cassandra/issues/40
114
             */
115
            throw new \Cassandra\Exception('NAMES_FOR_VALUES in batch request seems never work in Cassandra 2.1.x.  Keep NAMES_FOR_VALUES flag false to avoid this bug.');
116
            
117
            $flags |= Query::FLAG_WITH_NAMES_FOR_VALUES;
0 ignored issues
show
Unused Code introduced by
$flags |= \Cassandra\Req..._WITH_NAMES_FOR_VALUES; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
118
        }
119
        
120
        return pack('n', $consistency) . pack('C', $flags) . $optional;
121
    }
122
}
123