Completed
Push — master ( eb778b...3662e6 )
by Chris
04:40
created

Result   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 111
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A snakeToCamel() 5 5 1
A __construct() 7 7 1
A setInfo() 13 13 3
A __get() 3 3 1

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 Darya\Storage;
3
4
use Darya\Storage\Error;
5
use Darya\Storage\Query;
6
7
/**
8
 * Darya's storage query result.
9
 * 
10
 * @property array $data       Result data
11
 * @property Query $query      Query that produced this result
12
 * @property int   $count      Result count
13
 * @property Error $error      Result error
14
 * @property array $fields     Field names for each result data row
15
 * @property int   $insertId   Insert ID
16
 * @property int   $affected   Rows affected
17
 * 
18
 * @author Chris Andrew <[email protected]>
19
 */
20 View Code Duplication
class Result {
0 ignored issues
show
Duplication introduced by
This class 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...
21
	
22
	/**
23
	 * The storage query that produced this result.
24
	 * 
25
	 * @var Query
26
	 */
27
	protected $query;
28
	
29
	/**
30
	 * An associative array of the result data.
31
	 * 
32
	 * @var array
33
	 */
34
	protected $data;
35
	
36
	/**
37
	 * The error that occurred when executing the query, if any.
38
	 * 
39
	 * @var Error|null
40
	 */
41
	protected $error;
42
	
43
	/**
44
	 * The number of rows in the result data.
45
	 * 
46
	 * @var int
47
	 */
48
	protected $count;
49
	
50
	/**
51
	 * The set of fields available for each row in the result.
52
	 * 
53
	 * @var array
54
	 */
55
	protected $fields;
56
	
57
	/**
58
	 * Auto incremented primary key of an inserted row.
59
	 * 
60
	 * @var int
61
	 */
62
	protected $insertId;
63
	
64
	/**
65
	 * Error captured from the query that produced this result.
66
	 * 
67
	 * @var Error
68
	 */
69
	protected $error;
70
	
71
	/**
72
	 * Convert a string from snake_case to camelCase.
73
	 * 
74
	 * @param string $string
75
	 * @return string
76
	 */
77
	protected static function snakeToCamel($string) {
78
		return preg_replace_callback('/_(.)/', function($matches) {
79
			return strtoupper($matches[1]);
80
		}, $string);
81
	}
82
	
83
	/**
84
	 * Instantiate a new storage query result.
85
	 * 
86
	 * @param Query $query
87
	 * @param array $data  [optional]
88
	 * @param array $info  [optional]
89
	 * @param Error $error [optional]
90
	 */
91
	public function __construct(Query $query, array $data = array(), array $info = array(), Error $error = null) {
92
		$this->query = $query;
93
		$this->data = $data;
94
		$this->error = $error;
95
		
96
		$this->setInfo($info);
97
	}
98
	
99
	/**
100
	 * Set the result info.
101
	 * 
102
	 * Accepts the keys 'affected', 'count', 'insert_id' and 'fields'.
103
	 * 
104
	 * @param array $info
105
	 */
106
	protected function setInfo(array $info) {
107
		$defaults = array(
108
			'count' => 0,
109
			'fields' => array(),
110
			'affected' => 0,
111
			'insert_id' => 0
112
		);
113
		
114
		foreach ($defaults as $key => $default) {
115
			$property = static::snakeToCamel($key);
116
			$this->$property = isset($info[$key]) ? $info[$key] : $default;
117
		}
118
	}
119
	
120
	/**
121
	 * Dynamically retrieve the given property.
122
	 * 
123
	 * @param string $property
124
	 * @return mixed
125
	 */
126
	public function __get($property) {
127
		return $this->$property;
128
	}
129
	
130
}
131