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 |
||
| 20 | class Config implements ArrayAccess |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $config; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Config constructor. |
||
| 29 | * |
||
| 30 | * @param array $config |
||
| 31 | */ |
||
| 32 | public function __construct(array $config) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get an item from an array using "dot" notation. |
||
| 39 | * |
||
| 40 | * @param string $key |
||
| 41 | * @param mixed $default |
||
| 42 | * |
||
| 43 | * @return mixed |
||
| 44 | */ |
||
| 45 | View Code Duplication | public function get($key, $default = null) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Set an array item to a given value using "dot" notation. |
||
| 67 | * |
||
| 68 | * @param string $key |
||
| 69 | * @param mixed $value |
||
| 70 | * |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | public function set($key, $value) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Determine if the given configuration value exists. |
||
| 96 | * |
||
| 97 | * @param string $key |
||
| 98 | * |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | public function has($key) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Whether a offset exists. |
||
| 108 | * |
||
| 109 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
| 110 | * |
||
| 111 | * @param mixed $offset <p> |
||
| 112 | * An offset to check for. |
||
| 113 | * </p> |
||
| 114 | * |
||
| 115 | * @return bool true on success or false on failure. |
||
| 116 | * </p> |
||
| 117 | * <p> |
||
| 118 | * The return value will be casted to boolean if non-boolean was returned |
||
| 119 | * |
||
| 120 | * @since 5.0.0 |
||
| 121 | */ |
||
| 122 | public function offsetExists($offset) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Offset to retrieve. |
||
| 129 | * |
||
| 130 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
| 131 | * |
||
| 132 | * @param mixed $offset <p> |
||
| 133 | * The offset to retrieve. |
||
| 134 | * </p> |
||
| 135 | * |
||
| 136 | * @return mixed Can return all value types |
||
| 137 | * |
||
| 138 | * @since 5.0.0 |
||
| 139 | */ |
||
| 140 | public function offsetGet($offset) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Offset to set. |
||
| 147 | * |
||
| 148 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
| 149 | * |
||
| 150 | * @param mixed $offset <p> |
||
| 151 | * The offset to assign the value to. |
||
| 152 | * </p> |
||
| 153 | * @param mixed $value <p> |
||
| 154 | * The value to set. |
||
| 155 | * </p> |
||
| 156 | * |
||
| 157 | * @since 5.0.0 |
||
| 158 | */ |
||
| 159 | public function offsetSet($offset, $value) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Offset to unset. |
||
| 166 | * |
||
| 167 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
| 168 | * |
||
| 169 | * @param mixed $offset <p> |
||
| 170 | * The offset to unset. |
||
| 171 | * </p> |
||
| 172 | * |
||
| 173 | * @since 5.0.0 |
||
| 174 | */ |
||
| 175 | public function offsetUnset($offset) |
||
| 179 | } |
||
| 180 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: