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 |
||
| 13 | trait DynamicObjectsTrait |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | protected static $dynamicMethods = array(); |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected static $dynamicProperties = array(); |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var CacheInterface |
||
| 27 | */ |
||
| 28 | protected static $cache; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Add a dynamic property. |
||
| 32 | * |
||
| 33 | * @param string $name |
||
| 34 | * The property name. |
||
| 35 | * @param mixed $value |
||
| 36 | * The property value. |
||
| 37 | * @param bool $memoize |
||
| 38 | * Memoize parameter. |
||
| 39 | */ |
||
| 40 | 5 | public static function addDynamicProperty($name, $value, $memoize = false) |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Add a dynamic method. |
||
| 51 | * |
||
| 52 | * @param $name |
||
| 53 | * The method name. |
||
| 54 | * @param \Closure $func |
||
| 55 | * The method. |
||
| 56 | * @param bool $memoize |
||
| 57 | * Memoize parameter. |
||
| 58 | */ |
||
| 59 | 4 | public static function addDynamicMethod($name, \Closure $func, $memoize = false) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Check if a dynamic property exists. |
||
| 70 | * |
||
| 71 | * @param string $name |
||
| 72 | * The property name. |
||
| 73 | * @return bool |
||
| 74 | * True if the property exists, false otherwise. |
||
| 75 | */ |
||
| 76 | 8 | public static function hasDynamicProperty($name) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Check if a dynamic method exists. |
||
| 83 | * |
||
| 84 | * @param string $name |
||
| 85 | * The property name. |
||
| 86 | * @return bool |
||
| 87 | * True if the property exists, false otherwise. |
||
| 88 | */ |
||
| 89 | 6 | public static function hasDynamicMethod($name) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Get a dynamic property. |
||
| 96 | * |
||
| 97 | * @param $name |
||
| 98 | * The property name. |
||
| 99 | * @return mixed|null |
||
| 100 | * The property value if it exists, null otherwise. |
||
| 101 | */ |
||
| 102 | 4 | public static function getDynamicProperty($name) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Get a dynamic method. |
||
| 113 | * |
||
| 114 | * @param $name |
||
| 115 | * The method name. |
||
| 116 | * @return mixed|null |
||
| 117 | * The method if it exists, null otherwise. |
||
| 118 | */ |
||
| 119 | 2 | public static function getDynamicMethod($name) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Clear dynamic properties. |
||
| 127 | */ |
||
| 128 | 1 | public static function clearDynamicProperties() |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Clear dynamic methods. |
||
| 135 | */ |
||
| 136 | 1 | public static function clearDynamicMethods() |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Remove a dynamic property. |
||
| 143 | * |
||
| 144 | * @param string $name |
||
| 145 | * The property name. |
||
| 146 | */ |
||
| 147 | 1 | public static function removeDynamicProperty($name) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Remove a dynamic method. |
||
| 154 | * |
||
| 155 | * @param string $name |
||
| 156 | * The method name. |
||
| 157 | */ |
||
| 158 | 1 | public static function removeDynamicMethod($name) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Set the cache. |
||
| 165 | * |
||
| 166 | * @param \Psr\SimpleCache\CacheInterface $cache |
||
| 167 | */ |
||
| 168 | 3 | public static function setDynamicObjectCacheProvider(CacheInterface $cache) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Get the cache. |
||
| 175 | * |
||
| 176 | * @return \Psr\SimpleCache\CacheInterface |
||
| 177 | */ |
||
| 178 | 4 | public static function getDynamicObjectCacheProvider() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Clear the cache. |
||
| 189 | */ |
||
| 190 | 1 | public static function clearDynamicObjectCache() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @param $method |
||
| 197 | * @param array $parameters |
||
| 198 | * |
||
| 199 | * @return mixed |
||
| 200 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
| 201 | */ |
||
| 202 | 3 | public function __call($method, array $parameters = array()) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * {inheritdoc} |
||
| 228 | */ |
||
| 229 | 3 | public function __set($property, $value) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * {inheritdoc} |
||
| 238 | */ |
||
| 239 | 4 | public function __get($property) |
|
| 267 | } |
||
| 268 |