Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php namespace Comodojo\Cache\Traits; |
||
| 21 | trait GenericManagerTrait { |
||
| 22 | |||
| 23 | 79 | public function genericAddProvider($provider, $weight = 0) { |
|
| 30 | |||
| 31 | 2 | public function removeProvider($id) { |
|
| 32 | |||
| 33 | 2 | return $this->stack->remove($id); |
|
| 34 | |||
| 35 | } |
||
| 36 | |||
| 37 | 2 | public function getProvider($id) { |
|
| 38 | |||
| 39 | 2 | return $this->stack->get($id); |
|
| 40 | |||
| 41 | } |
||
| 42 | |||
| 43 | 2 | public function getProviders($enabled = false) { |
|
| 44 | |||
| 45 | 2 | return $this->stack->getAll($enabled); |
|
| 46 | |||
| 47 | } |
||
| 48 | |||
| 49 | 29 | public function getSelectedProvider() { |
|
| 54 | |||
| 55 | 11 | View Code Duplication | public function clear() { |
| 56 | |||
| 57 | 11 | $result = []; |
|
| 58 | |||
| 59 | 11 | foreach ( $this->stack->getAll() as $provider ) { |
|
| 60 | |||
| 61 | 9 | $result[] = $provider->clear(); |
|
| 62 | |||
| 63 | } |
||
| 64 | |||
| 65 | 11 | return !in_array(false, $result); |
|
| 66 | |||
| 67 | } |
||
| 68 | |||
| 69 | 6 | public function setNamespace($namespace = null) { |
|
| 80 | |||
| 81 | 4 | View Code Duplication | public function clearNamespace() { |
| 82 | |||
| 83 | 4 | $result = []; |
|
| 84 | |||
| 85 | 4 | foreach ( $this->stack->getAll() as $provider ) { |
|
| 86 | 2 | $result[] = $provider->clearNamespace(); |
|
| 87 | } |
||
| 88 | |||
| 89 | 4 | return !in_array(false, $result); |
|
| 90 | |||
| 91 | } |
||
| 92 | |||
| 93 | 2 | public function getStats() { |
|
| 104 | |||
| 105 | 66 | protected function selectProvider() { |
|
| 106 | |||
| 129 | |||
| 130 | } |
||
| 131 |
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: