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 | 1 | public function __construct() |
|
24 | |||
25 | /** |
||
26 | * prevent the instance from being cloned |
||
27 | * |
||
28 | * @return void |
||
29 | */ |
||
30 | private function __clone() |
||
33 | |||
34 | /** |
||
35 | * prevent from being unserialized |
||
36 | * |
||
37 | * @return void |
||
38 | */ |
||
39 | private function __wakeup() |
||
42 | |||
43 | /** |
||
44 | * Magic setter |
||
45 | * @param $variable |
||
46 | * @param $value |
||
47 | */ |
||
48 | 7 | public function __set($variable, $value) |
|
54 | |||
55 | /** |
||
56 | * Magic getter |
||
57 | * @param string $variable |
||
58 | * @return $mixed |
||
59 | */ |
||
60 | public function __get($variable) |
||
64 | |||
65 | /** |
||
66 | * Método que devuelve si una clase está isntanciada correctamente |
||
67 | * @return bool |
||
68 | */ |
||
69 | 1 | public function isLoaded() |
|
73 | |||
74 | /** |
||
75 | * Método que configura como cargada una clase |
||
76 | * @param bool $loaded |
||
77 | */ |
||
78 | 1 | public function setLoaded($loaded = true) |
|
82 | |||
83 | /** |
||
84 | * HELPERS |
||
85 | */ |
||
86 | |||
87 | /** |
||
88 | * Método que extrae el nombre de la clase |
||
89 | * @return string |
||
90 | */ |
||
91 | public function getShortName() |
||
96 | |||
97 | /** |
||
98 | * Dependency inyector service invoker |
||
99 | * @param string $variable |
||
100 | * @param bool $singleton |
||
101 | * @param string $classNameSpace |
||
102 | * @return $this |
||
103 | */ |
||
104 | 1 | public function load($variable, $singleton = true, $classNameSpace = null) |
|
120 | |||
121 | /** |
||
122 | * Método que inyecta automáticamente las dependencias en la clase |
||
123 | */ |
||
124 | 1 | public function init() |
|
146 | |||
147 | /** |
||
148 | * Método que extrae todas las propiedades inyectables de una clase |
||
149 | * @param null $class |
||
150 | * @return array |
||
151 | */ |
||
152 | 1 | private function getClassProperties($class = null) |
|
174 | |||
175 | /** |
||
176 | * Método que extrae el tipo de instancia de la variable |
||
177 | * @param $doc |
||
178 | * @return null|string |
||
179 | */ |
||
180 | 1 | private function extractVarType($doc) |
|
188 | |||
189 | /** |
||
190 | * Create the depecency injected |
||
191 | * @param string $variable |
||
192 | * @param bool $singleton |
||
193 | * @param string $classNameSpace |
||
194 | * @param string $calledClass |
||
195 | * @return mixed |
||
196 | */ |
||
197 | 1 | private function constructInyectableInstance($variable, $singleton, $classNameSpace, $calledClass) |
|
210 | } |
||
211 |
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.