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 |
||
12 | class Singleton |
||
13 | { |
||
14 | use SingletonTrait; |
||
15 | /** |
||
16 | * @var bool Flag that indicated if the class is already loaded |
||
17 | */ |
||
18 | protected $loaded = false; |
||
19 | |||
20 | 2 | public function __construct() |
|
24 | |||
25 | /** |
||
26 | * Magic setter |
||
27 | * @param $variable |
||
28 | * @param $value |
||
29 | */ |
||
30 | 8 | public function __set($variable, $value) |
|
36 | |||
37 | /** |
||
38 | * Magic getter |
||
39 | * @param string $variable |
||
40 | * @return $mixed |
||
41 | */ |
||
42 | 1 | public function __get($variable) |
|
46 | |||
47 | /** |
||
48 | * Método que devuelve si una clase está isntanciada correctamente |
||
49 | * @return bool |
||
50 | */ |
||
51 | 2 | public function isLoaded() |
|
55 | |||
56 | /** |
||
57 | * Método que configura como cargada una clase |
||
58 | * @param bool $loaded |
||
59 | */ |
||
60 | 2 | public function setLoaded($loaded = true) |
|
64 | |||
65 | /** |
||
66 | * HELPERS |
||
67 | */ |
||
68 | |||
69 | /** |
||
70 | * Método que extrae el nombre de la clase |
||
71 | * @return string |
||
72 | */ |
||
73 | 1 | public function getShortName() |
|
78 | |||
79 | /** |
||
80 | * Dependency inyector service invoker |
||
81 | * @param string $variable |
||
82 | * @param bool $singleton |
||
83 | * @param string $classNameSpace |
||
84 | * @return $this |
||
85 | */ |
||
86 | 2 | public function load($variable, $singleton = true, $classNameSpace = null) |
|
102 | |||
103 | /** |
||
104 | * Método que inyecta automáticamente las dependencias en la clase |
||
105 | */ |
||
106 | 2 | public function init() |
|
128 | |||
129 | /** |
||
130 | * Método que extrae todas las propiedades inyectables de una clase |
||
131 | * @param null $class |
||
132 | * @return array |
||
133 | */ |
||
134 | 2 | private function getClassProperties($class = null) |
|
156 | |||
157 | /** |
||
158 | * Método que extrae el tipo de instancia de la variable |
||
159 | * @param $doc |
||
160 | * @return null|string |
||
161 | */ |
||
162 | 2 | private function extractVarType($doc) |
|
170 | |||
171 | /** |
||
172 | * Create the depecency injected |
||
173 | * @param string $variable |
||
174 | * @param bool $singleton |
||
175 | * @param string $classNameSpace |
||
176 | * @param string $calledClass |
||
177 | * @return mixed |
||
178 | */ |
||
179 | 2 | private function constructInyectableInstance($variable, $singleton, $classNameSpace, $calledClass) |
|
192 | } |
||
193 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.