Completed
Branch 0.3.0 (b16461)
by Anton
04:03
created

Listview::select()   C

Complexity

Conditions 12
Paths 16

Size

Total Lines 38
Code Lines 14

Duplication

Lines 8
Ratio 21.05 %
Metric Value
dl 8
loc 38
rs 5.1612
cc 12
eloc 14
nc 16
nop 5

How to fix   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
3
namespace Modules\Entitizer\Utils {
4
5
	use Modules\Entitizer, DB;
6
7
	abstract class Listview extends View {
8
9
		# Get default select query
10
11
		private function getDefaultSelectQuery(array $config, array $order_by, int $index, int $display) {
12
13
			return ("SELECT SQL_CALC_FOUND_ROWS " . $this->getSelection() . " ") .
14
15
			       ("FROM " . static::$table . " ent ") .
16
17
				   (('' !== ($condition = $this->getCondition($config))) ? ("WHERE " . $condition . " ") : "") .
18
19
				   ("ORDER BY " . $this->getOrderBy($order_by) . " ") .
20
21
			       (($index > 0) ? ("LIMIT " . ((($index - 1) * $display) . ", " . $display)) : "");
22
		}
23
24
		# Get nesting select query
25
26
		private function getNestingSelectQuery(int $parent_id, array $config, array $order_by, int $index, int $display) {
27
28
			return ("SELECT SQL_CALC_FOUND_ROWS " . $this->getSelection() . ", COUNT(chd.descendant) as children ") .
29
30
			       ("FROM " . static::$table . " ent ") .
31
32
			       ("LEFT JOIN " . static::$table_relations . " chd ON chd.ancestor = ent.id AND chd.depth = 1 ") .
33
34
				   ("LEFT JOIN " . static::$table_relations . " rel ON rel.descendant = ent.id AND rel.depth = 1 ") .
35
36
				   ("WHERE COALESCE(rel.ancestor, 0) = " . $parent_id . " ") .
37
38
				   (('' !== ($condition = $this->getCondition($config))) ? ("AND " . $condition . " ") : "") .
39
40
				   ("GROUP BY ent.id ORDER BY " . $this->getOrderBy($order_by) . " ") .
41
42
			       (($index > 0) ? ("LIMIT " . ((($index - 1) * $display) . ", " . $display)) : "");
43
		}
44
45
		# Get default count query
46
47
		private function getDefaultCountQuery(array $config) {
48
49
			return ("SELECT COUNT(ent.id) as count FROM " . static::$table . " ent ") .
50
51
			       (('' !== ($condition = $this->getCondition($config))) ? ("WHERE " . $condition) : "");
52
		}
53
54
		# Get nesting count query
55
56
		private function getNestingCountQuery(int $parent_id, array $config) {
57
58
			return ("SELECT COUNT(ent.id) as count FROM " . static::$table . " ent ") .
59
60
			       ("LEFT JOIN " . static::$table_relations . " rel ON rel.descendant = ent.id AND rel.depth = 1 ") .
61
62
			       ("WHERE COALESCE(rel.ancestor, 0) = " . $parent_id . " ") .
63
64
			       (('' !== ($condition = $this->getCondition($config))) ? ("AND " . $condition) : "");
65
		}
66
67
		# Select entries from DB
68
69
		private function select(int $parent_id = null, array $config = [], array $order_by = [], int $index = 0, int $display = 0) {
70
71
			if (!((null === $parent_id) || ($parent_id >= 0))) return false;
72
73
			if (!(($index >= 0) && ($display >= 0))) return false;
74
75
			# Select entities
76
77
			$query = ((null === $parent_id) ? $this->getDefaultSelectQuery($config, $order_by, $index, $display) :
78
79
				$this->getNestingSelectQuery($parent_id, $config, $order_by, $index, $display));
80
81
			if (!(DB::send($query) && DB::last()->status)) return false;
82
83
			# Process results
84
85
			$items = ['list' => [], 'total' => 0];
86
87 View Code Duplication
			while (null !== ($data = DB::last()->row())) {
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...
88
89
				$entity = Entitizer::create(static::$table, $data);
90
91
				$items['list'][$entity->id] = ['entity' => $entity];
92
93
				if (null !== $parent_id) $items['list'][$entity->id]['children'] = intval($data['children']);
94
			}
95
96
			# Count total
97
98
			if (DB::send("SELECT FOUND_ROWS() as total") && (DB::last()->rows === 1)) {
99
100
				$items['total'] = intval(DB::last()->row()['total']);
101
			}
102
103
			# ------------------------
104
105
			return $items;
106
		}
107
108
		# Count entries in DB
109
110
		private function count(int $parent_id = null, array $config = []) {
111
112
			if (!((null === $parent_id) || ($parent_id >= 0))) return false;
113
114
			# Count entities
115
116
			$query = ((null === $parent_id) ? $this->getDefaultCountQuery($config) :
117
118
				$this->getNestingCountQuery($parent_id, $config));
119
120
			if (!(DB::send($query) && DB::last()->status)) return false;
121
122
			# ------------------------
123
124
			return intval(DB::last()->row()['count']);
125
		}
126
127
		# Get items
128
129
		public function items(array $config = [], array $order_by = [], int $index = 0, int $display = 0) {
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $order_by is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $index is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $display is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
130
131
			return $this->select(null, ...func_get_args());
132
		}
133
134
		# Get items count
135
136
		public function itemsCount(array $config = []) {
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
137
138
			return $this->count(null, ...func_get_args());
139
		}
140
141
		# Get children
142
143
		public function children(int $parent_id = 0, array $config = [], array $order_by = [], int $index = 0, int $display = 0) {
0 ignored issues
show
Unused Code introduced by
The parameter $parent_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $order_by is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $index is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $display is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
144
145
			if (!static::$nesting) return false;
146
147
			return $this->select(...func_get_args());
0 ignored issues
show
Documentation introduced by
func_get_args() is of type array, but the function expects a null|integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
148
		}
149
150
		# Get children count
151
152
		public function childrenCount(int $parent_id = 0, array $config = []) {
0 ignored issues
show
Unused Code introduced by
The parameter $parent_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
153
154
			if (!static::$nesting) return false;
155
156
			return $this->count(...func_get_args());
0 ignored issues
show
Documentation introduced by
func_get_args() is of type array, but the function expects a null|integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
157
		}
158
	}
159
}
160