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 |
||
| 11 | class Singleton |
||
| 12 | { |
||
| 13 | use SingletonTrait; |
||
| 14 | /** |
||
| 15 | * @var bool Flag that indicated if the class is already loaded |
||
| 16 | */ |
||
| 17 | protected $loaded = false; |
||
| 18 | |||
| 19 | 1 | public function __construct() |
|
| 20 | { |
||
| 21 | 1 | Logger::log(get_class($this) . ' constructor invoked'); |
|
| 22 | 1 | } |
|
| 23 | |||
| 24 | /** |
||
| 25 | * prevent the instance from being cloned |
||
| 26 | * |
||
| 27 | * @return void |
||
| 28 | */ |
||
| 29 | private function __clone() {} |
||
| 30 | |||
| 31 | /** |
||
| 32 | * prevent from being unserialized |
||
| 33 | * |
||
| 34 | * @return void |
||
| 35 | */ |
||
| 36 | private function __wakeup() {} |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Magic setter |
||
| 40 | * @param $variable |
||
| 41 | * @param $value |
||
| 42 | */ |
||
| 43 | 7 | public function __set($variable, $value) { |
|
| 44 | 7 | if(property_exists(get_class($this), $variable)) { |
|
| 45 | 7 | $this->$variable = $value; |
|
| 46 | 7 | } |
|
| 47 | 7 | } |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Magic getter |
||
| 51 | * @param string $variable |
||
| 52 | * @return $mixed |
||
| 53 | */ |
||
| 54 | public function __get($variable) { |
||
|
|
|||
| 55 | return property_exists(get_class($this), $variable) ? $this->$variable : null; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Método que devuelve si una clase está isntanciada correctamente |
||
| 60 | * @return bool |
||
| 61 | */ |
||
| 62 | 1 | public function isLoaded() { |
|
| 63 | 1 | return $this->loaded; |
|
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Método que configura como cargada una clase |
||
| 68 | * @param bool $loaded |
||
| 69 | */ |
||
| 70 | 1 | public function setLoaded($loaded = true) { |
|
| 71 | 1 | $this->loaded = $loaded; |
|
| 72 | 1 | } |
|
| 73 | |||
| 74 | /** |
||
| 75 | * HELPERS |
||
| 76 | */ |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Método que extrae el nombre de la clase |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | public function getShortName() { |
||
| 83 | $reflector = new \ReflectionClass(get_class($this)); |
||
| 84 | return $reflector->getShortName(); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Dependency inyector service invoker |
||
| 89 | * @param string $variable |
||
| 90 | * @param bool $singleton |
||
| 91 | * @param string $classNameSpace |
||
| 92 | * @return $this |
||
| 93 | */ |
||
| 94 | 1 | public function load($variable, $singleton = true, $classNameSpace = null) { |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Método que inyecta automáticamente las dependencias en la clase |
||
| 112 | */ |
||
| 113 | 1 | public function init() { |
|
| 114 | 1 | if (!$this->isLoaded()) { |
|
| 115 | 1 | $cacheFilename = "reflections".DIRECTORY_SEPARATOR.sha1(get_class($this)).".json"; |
|
| 116 | /** @var \PSFS\base\Cache $cacheService */ |
||
| 117 | 1 | $cacheService = Cache::getInstance(); |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Método que extrae todas las propiedades inyectables de una clase |
||
| 137 | * @param null $class |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | 1 | private function getClassProperties($class = null) { |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Método que extrae el tipo de instancia de la variable |
||
| 164 | * @param $doc |
||
| 165 | * @return null|string |
||
| 166 | */ |
||
| 167 | 1 | private function extractVarType($doc) { |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Create the depecency injected |
||
| 177 | * @param string $variable |
||
| 178 | * @param bool $singleton |
||
| 179 | * @param string $classNameSpace |
||
| 180 | * @param string $calledClass |
||
| 181 | * @return mixed |
||
| 182 | */ |
||
| 183 | 1 | private function constructInyectableInstance($variable, $singleton, $classNameSpace, $calledClass) |
|
| 196 | } |
||
| 197 |
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
@returnannotation as described here.