Error::getData()   D
last analyzed

Complexity

Conditions 16
Paths 16

Size

Total Lines 89
Code Lines 70

Duplication

Lines 26
Ratio 29.21 %

Importance

Changes 6
Bugs 2 Features 3
Metric Value
c 6
b 2
f 3
dl 26
loc 89
rs 4.8737
cc 16
eloc 70
nc 16
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Cassandra\Response;
3
use Cassandra\Protocol\Frame;
4
5
class Error extends Response {
6
	const SERVER_ERROR = 0x0000;
7
	const PROTOCOL_ERROR = 0x000A;
8
	const BAD_CREDENTIALS = 0x0100;
9
	const UNAVAILABLE_EXCEPTION = 0x1000;
10
	const OVERLOADED = 0x1001;
11
	const IS_BOOTSTRAPPING = 0x1002;
12
	const TRUNCATE_ERROR = 0x1003;
13
	const WRITE_TIMEOUT = 0x1100;
14
	const READ_TIMEOUT = 0x1200;
15
	const SYNTAX_ERROR = 0x2000;
16
	const UNAUTHORIZED = 0x2100;
17
	const INVALID = 0x2200;
18
	const CONFIG_ERROR = 0x2300;
19
	const ALREADY_EXIST = 0x2400;
20
	const UNPREPARED = 0x2500;
21
	
22
	/**
23
	 * Indicates an error processing a request. The body of the message will be an
24
	 * error code ([int]) followed by a [string] error message. Then, depending on
25
	 * the exception, more content may follow. The error codes are defined in
26
	 * Section 7, along with their additional content if any.
27
	 *
28
	 * @return array
29
	 */
30
	public function getData() {
31
		$this->_stream->offset(0);
32
		$data = [];
33
		$data['code'] = $this->_stream->readInt();
34
35
		switch($data['code']){
36
			case self::SERVER_ERROR:
37
				$data['message'] = "Server error: " . $this->_stream->readString();
38
				break;
39
40
			case self::PROTOCOL_ERROR:
41
				$data['message'] = "Protocol error: " . $this->_stream->readString();
42
				break;
43
44
			case self::BAD_CREDENTIALS:
45
				$data['message'] = "Bad credentials: " . $this->_stream->readString();
46
				break;
47
48 View Code Duplication
			case self::UNAVAILABLE_EXCEPTION:
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...
49
				$data['message'] = "Unavailable exception. Error data: " . var_export([
50
                        'error'=>$this->_stream->readString(),
51
                        'consistency' => $this->_stream->readShort(),
52
                        'node' => $this->_stream->readInt(),
53
						'replica' => $this->_stream->readInt()
54
					], true);
55
				break;
56
57
			case self::OVERLOADED:
58
				$data['message'] = "Overloaded: " . $this->_stream->readString();
59
				break;
60
61
			case self::IS_BOOTSTRAPPING:
62
				$data['message'] = "Is_bootstrapping: " . $this->_stream->readString();
63
				break;
64
65
			case self::TRUNCATE_ERROR:
66
				$data['message'] = "Truncate_error: " . $this->_stream->readString();
67
				break;
68
69 View Code Duplication
			case self::WRITE_TIMEOUT:
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...
70
				$data['message'] = "Write_timeout. Error data: " . var_export([
71
                        'error'=>$this->_stream->readString(),
72
                        'consistency' => $this->_stream->readShort(),
73
						'node' => $this->_stream->readInt(),
74
						'replica' => $this->_stream->readInt(),
75
						'writeType' => $this->_stream->readString()
76
					], true);
77
				break;
78
79 View Code Duplication
			case self::READ_TIMEOUT:
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...
80
				$data['message'] = "Read_timeout. Error data: " . var_export([
81
                        'error'=>$this->_stream->readString(),
82
                        'consistency' => $this->_stream->readShort(),
83
						'node' => $this->_stream->readInt(),
84
						'replica' => $this->_stream->readInt(),
85
						'dataPresent' => $this->_stream->readChar()
86
					], true);
87
				break;
88
89
			case self::SYNTAX_ERROR:
90
				$data['message'] = "Syntax_error: " . $this->_stream->readString();
91
				break;
92
93
			case self::UNAUTHORIZED:
94
				$data['message'] = "Unauthorized: " . $this->_stream->readString();
95
				break;
96
97
			case self::INVALID:
98
				$data['message'] = "Invalid: " . $this->_stream->readString();
99
				break;
100
101
			case self::CONFIG_ERROR:
102
				$data['message'] = "Config_error: " . $this->_stream->readString();
103
				break;
104
105
			case self::ALREADY_EXIST:
106
				$data['message'] = "Already_exists: " . $this->_stream->readString();
107
				break;
108
109
			case self::UNPREPARED:
110
				$data['message'] = "Unprepared: " . $this->_stream->readShort();
111
				break;
112
113
			default:
114
				$data['message'] = 'Unknown error';
115
		}
116
		
117
		return $data;
118
	}
119
	
120
	/**
121
	 * 
122
	 * @return Exception
123
	 */
124
	public function getException(){
125
		$data = $this->getData();
126
		return new Exception($data['message'], $data['code']);
127
	}
128
}
129