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

Modify::create()   D

Complexity

Conditions 10
Paths 19

Size

Total Lines 38
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 38
rs 4.8196
cc 10
eloc 13
nc 19
nop 1

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\Entity {
4
5
	use Modules\Entitizer, DB;
6
7
	trait Modify {
8
9
		protected $definition = null, $error = false, $modifiable = false, $data = [];
10
11
		# Init entity subtree connection
12
13
		private function initSubtree() {
14
15
			$dataset = ['ancestor' => $this->id, 'descendant' => $this->id, 'depth' => 0];
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16
17
			DB::insert(static::$table_relations, $dataset, false, true);
18
19
			# ------------------------
20
21
			return (DB::last() && DB::last()->status);
22
		}
23
24
		# Disconnect subtree from current position
25
26 View Code Duplication
		private function disconnectSubtree() {
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...
27
28
			$query = ("DELETE rla FROM " . static::$table_relations . " rla ") .
29
30
			         ("JOIN "  . static::$table_relations . " rlb ON rlb.descendant = rla.descendant ") .
31
32
					 ("LEFT JOIN " . static::$table_relations . " rlx ") .
33
34
					 ("ON rlx.ancestor = rlb.ancestor AND rlx.descendant = rla.ancestor ") .
35
36
					 ("WHERE rlb.ancestor = " . $this->id . " AND rlx.ancestor IS NULL");
37
38
			if (!(DB::send($query) && DB::last()->status)) return false;
39
40
			# Set path
41
42
			$this->data['parent_id'] = 0;
43
44
			# ------------------------
45
46
			return true;
47
		}
48
49
		# Connect subtree under new position
50
51 View Code Duplication
		private function connectSubtree(int $parent_id) {
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...
52
53
			$query = ("INSERT INTO " . static::$table_relations . " (ancestor, descendant, depth) ") .
54
55
			         ("SELECT sup.ancestor, sub.descendant, sup.depth + sub.depth + 1 ") .
56
57
					 ("FROM " . static::$table_relations . " sup ") .
58
59
					 ("JOIN " . static::$table_relations . " sub ") .
60
61
					 ("WHERE sub.ancestor = " . $this->id . " AND sup.descendant = " . $parent_id);
62
63
			if (!(DB::send($query) && DB::last()->status)) return false;
64
65
			# Set path
66
67
			$this->data['parent_id'] = $parent_id;
68
69
			# ------------------------
70
71
			return true;
72
		}
73
74
		# Create entity entry in DB
75
76
		public function create(array $data) {
77
78
			if (!$this->modifiable || (0 !== $this->id)) return false;
79
80
			$data = $this->definition->cast($data);
81
82
			if (static::$auto_increment && isset($data['id'])) unset($data['id']);
83
84
			# Insert entity
85
86
			DB::insert(static::$table, $data);
87
88
			if (!(DB::last() && DB::last()->status)) return false;
89
90
			# Set data
91
92
			$this->error = false;
93
94
			if (static::$auto_increment) $this->data['id'] = DB::last()->id;
95
96
			foreach ($data as $name => $value) $this->data[$name] = $value;
97
98
			# Implement entity
99
100
			$this->implement();
0 ignored issues
show
Bug introduced by
It seems like implement() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
101
102
			# Init subtreee
103
104
			if (static::$nesting) $this->initSubtree();
105
106
			# Cache entity
107
108
			self::$cache[static::$table][$this->id] = $this;
109
110
			# ------------------------
111
112
			return true;
113
		}
114
115
		# Edit entity entry in DB
116
117
		public function edit(array $data) {
118
119
			if (!$this->modifiable || (0 === $this->id)) return false;
120
121
			$data = $this->definition->cast($data);
122
123
			if (isset($data['id'])) unset($data['id']);
124
125
			# Update entity
126
127
			DB::update(static::$table, $data, ['id' => $this->id]);
128
129
			if (!(DB::last() && DB::last()->status)) return false;
130
131
			# Set data
132
133
			foreach ($data as $name => $value) $this->data[$name] = $value;
134
135
			# Implement entity
136
137
			$this->implement();
0 ignored issues
show
Bug introduced by
It seems like implement() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
138
139
			# Init subtreee
140
141
			if (static::$nesting) $this->initSubtree();
142
143
			# ------------------------
144
145
			return true;
146
		}
147
148
		# Move entity to new parent
149
150
		public function move(int $parent_id) {
151
152
			if (!$this->modifiable || (0 === $this->id)) return false;
153
154
			# Re-connect entity if not in tree
155
156
			if (!$this->initSubtree()) return false;
157
158
			# Create parent entity
159
160
			if (0 !== ($parent = Entitizer::get(static::$table, $parent_id))->id) {
161
162
				if (false === ($path = $parent->path())) return false;
163
164
				if (false === ($depth = $this->subtreeDepth())) return false;
0 ignored issues
show
Bug introduced by
It seems like subtreeDepth() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
165
166
				if ((count($path) + $depth + 1) > CONFIG_ENTITIZER_MAX_DEPTH) return false;
167
			}
168
169
			# Disconnect subtree from current position
170
171
			if (!$this->disconnectSubtree()) return false;
172
173
			# Connect subtree under new position
174
175
		 	if (0 !== $parent->id) $this->connectSubtree($parent->id);
176
177
			# ------------------------
178
179
			return true;
180
		}
181
	}
182
}
183