| Conditions | 11 |
| Paths | 8 |
| Total Lines | 37 |
| Code Lines | 12 |
| Lines | 0 |
| Ratio | 0 % |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 13 | public function remove() { |
||
| 14 | |||
| 15 | if (!$this->modifiable || (0 === $this->id)) return false; |
||
|
|
|||
| 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; |
||
| 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(); |
||
| 45 | |||
| 46 | # ------------------------ |
||
| 47 | |||
| 48 | return true; |
||
| 49 | } |
||
| 50 | } |
||
| 52 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: