| Conditions | 15 |
| Paths | 22 |
| Total Lines | 49 |
| Code Lines | 15 |
| 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 (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->children())) return false; |
||
| 22 | |||
| 23 | # Remove extension entries |
||
| 24 | |||
| 25 | foreach (static::$extensions as $extension) { |
||
| 26 | |||
| 27 | $entity = self::get($extension, $this->id); |
||
| 28 | |||
| 29 | if ($entity->error() || ((0 !== $entity->id) && !$entity->remove())) return false; |
||
| 30 | } |
||
| 31 | |||
| 32 | # Remove entity |
||
| 33 | |||
| 34 | DB::delete(static::$table, ['id' => $this->id]); |
||
| 35 | |||
| 36 | if (!(DB::last() && DB::last()->status)) return false; |
||
| 37 | |||
| 38 | # Uncache entity |
||
| 39 | |||
| 40 | if (self::$cache[static::$type][$this->id] === $this) unset(self::$cache[static::$type][$this->id]); |
||
| 41 | |||
| 42 | # Reset id |
||
| 43 | |||
| 44 | $this->id = 0; |
||
| 45 | |||
| 46 | # Reset data array |
||
| 47 | |||
| 48 | foreach ($this->definition->params() as $name => $param) $this->data[$name] = $param->cast(null); |
||
| 49 | |||
| 50 | # Reset path |
||
| 51 | |||
| 52 | if (static::$nesting) $this->data['path'] = []; |
||
| 53 | |||
| 54 | # Implement entity |
||
| 55 | |||
| 56 | $this->implement(); |
||
| 57 | |||
| 58 | # ------------------------ |
||
| 59 | |||
| 60 | return true; |
||
| 61 | } |
||
| 62 | |||
| 72 |