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 |
||
| 19 | class Common extends ViewModel |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Returns the API resources. |
||
| 24 | * |
||
| 25 | * @return array |
||
| 26 | */ |
||
| 27 | public function getResources() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Deals with the resource groups definitions. |
||
| 59 | * |
||
| 60 | * @return array |
||
| 61 | */ |
||
| 62 | public function getResourceGroups() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Returns the man index/section string. |
||
| 91 | * |
||
| 92 | * @return integer |
||
| 93 | */ |
||
| 94 | public function getManTocSection() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * _def - view helper. |
||
| 111 | * |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | public function _def() |
||
| 120 | |||
| 121 | public function debug($data=null) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Assigns a property. |
||
| 130 | * |
||
| 131 | * // This value can be accessed as {{foo}} within the template |
||
| 132 | * $view->set('foo', 'my value'); |
||
| 133 | * |
||
| 134 | * You can also use an array to set several values at once: |
||
| 135 | * |
||
| 136 | * // Create the values {{food}} and {{beverage}} in the template |
||
| 137 | * $view->set(array('food' => 'bread', 'beverage' => 'water')); |
||
| 138 | * |
||
| 139 | * @param string|array $blob A string key or an associative array to set. |
||
| 140 | * @param mixed $value The value to set if the blob is a string. |
||
| 141 | * @return $this |
||
| 142 | */ |
||
| 143 | View Code Duplication | public function set($blob, $value = null) |
|
| 155 | |||
| 156 | View Code Duplication | public static function htmlizer($string) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Assigns a value by reference. The benefit of binding is that values can |
||
| 174 | * be altered without re-setting them. It is also possible to bind variables |
||
| 175 | * before they have values. Assigned values will be available as a |
||
| 176 | * variable within the template file: |
||
| 177 | * |
||
| 178 | * // This reference can be accessed as {{ref}} within the template |
||
| 179 | * $view->bind('ref', $bar); |
||
| 180 | * |
||
| 181 | * @param string variable name |
||
| 182 | * @param mixed referenced variable |
||
| 183 | * @return $this |
||
| 184 | */ |
||
| 185 | public function bind($key, & $value) |
||
| 191 | |||
| 192 | /* ---- generic helpers --- */ |
||
| 193 | |||
| 194 | View Code Duplication | public function hasMany($mix) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Returns this view model layout. |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public function getLayout() |
||
| 214 | |||
| 215 | } |
||
| 216 |
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: