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

Remove::remove()   C

Complexity

Conditions 11
Paths 8

Size

Total Lines 37
Code Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 37
rs 5.2653
cc 11
eloc 12
nc 8
nop 0

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 Remove {
8
9
		protected $definition = null, $error = false, $modifiable = false, $data = [];
10
11
		# Remove entity entry from DB
12
13
		public function remove() {
14
15
			if (!$this->modifiable || (0 === $this->id)) return false;
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
			# Check if entity is removable
18
19
			if (static::$super && ($this->id === 1)) return false;
20
21
			if (static::$nesting && (0 !== $this->subtreeCount())) return false;
0 ignored issues
show
Bug introduced by
It seems like subtreeCount() 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...
22
23
			# Remove entity
24
25
			DB::delete(static::$table, ['id' => $this->id]);
26
27
			if (!(DB::last() && DB::last()->status)) return false;
28
29
			# Uncache entity
30
31
			if ((self::$cache[static::$table][$this->id] ?? null) === $this) {
32
33
				unset(self::$cache[static::$table][$this->id]);
34
			}
35
36
			# Reset data
37
38
			$this->data = $this->definition->cast([], true);
39
40
			if (static::$nesting) $this->data['parent_id'] = 0;
41
42
			# Implement entity
43
44
			$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...
45
46
			# ------------------------
47
48
			return true;
49
		}
50
	}
51
}
52