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 |
||
| 14 | class TableApi extends Table |
||
| 15 | { |
||
| 16 | private $alterTable = false; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Constructor. |
||
| 20 | * |
||
| 21 | * @param string $table Table name. |
||
| 22 | * @param Schema $schema |
||
| 23 | * @param Manipulation $manipulation |
||
| 24 | * @param array $actions |
||
| 25 | */ |
||
| 26 | public function __construct($name, Schema $schema = null, Manipulation $manipulation, &$actions) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param string $method A method name. |
||
| 35 | * @param array $params Parameters. |
||
| 36 | * |
||
| 37 | * @return TableApi Self. |
||
| 38 | */ |
||
| 39 | public function __call($method, $params) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Drop a column. |
||
| 56 | * |
||
| 57 | * @param string $name Column name. |
||
| 58 | * |
||
| 59 | * @return TableApi Self. |
||
| 60 | */ |
||
| 61 | public function dropColumn($name) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Add a foreign key. |
||
| 75 | * |
||
| 76 | * @param string $referenceTable Referenced table name. |
||
| 77 | * @param string $referenceColumns Columns of referenced table. |
||
| 78 | * @param array $options Optional options. |
||
| 79 | * |
||
| 80 | * @return TableApi Self. |
||
| 81 | */ |
||
| 82 | public function addForeignKey($referenceTable, $referenceColumns, array $options = []) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Drop a foreign key. |
||
| 109 | * |
||
| 110 | * @param string $referenceTable Referenced table name. |
||
| 111 | * @param string $referenceColumns Columns of referenced table. |
||
| 112 | * @param array $options Optional options. |
||
| 113 | * |
||
| 114 | * @return TableApi Self. |
||
| 115 | */ |
||
| 116 | public function dropForeignKey($referenceTable, $referenceColumns, array $options = []) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Add a unique constraint. |
||
| 133 | * |
||
| 134 | * @param array $columns Unique columns. |
||
| 135 | * |
||
| 136 | * @return TableApi Self. |
||
| 137 | */ |
||
| 138 | View Code Duplication | public function addUnique($columns) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Drop a unique constraint. |
||
| 151 | * |
||
| 152 | * @param array $columns Unique columns. |
||
| 153 | * |
||
| 154 | * @return TableApi Self. |
||
| 155 | */ |
||
| 156 | View Code Duplication | public function dropUnique($columns) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Add a index on columns. |
||
| 169 | * |
||
| 170 | * @param array $columns Index columns. |
||
| 171 | * |
||
| 172 | * @return TableApi Self. |
||
| 173 | */ |
||
| 174 | View Code Duplication | public function addIndex($columns) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Drop a index on columns. |
||
| 187 | * |
||
| 188 | * @param array $columns Index columns. |
||
| 189 | * |
||
| 190 | * @return TableApi Self. |
||
| 191 | */ |
||
| 192 | View Code Duplication | public function dropIndex($columns) |
|
| 202 | } |
||
| 203 |
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: