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  | 
            ||
| 8 | class Navigation  | 
            ||
| 9 | { | 
            ||
| 10 | /**  | 
            ||
| 11 | * @var int  | 
            ||
| 12 | */  | 
            ||
| 13 | private $rootID = 0;  | 
            ||
| 14 | |||
| 15 | /**  | 
            ||
| 16 | * @var string  | 
            ||
| 17 | */  | 
            ||
| 18 | private $type;  | 
            ||
| 19 | |||
| 20 | /**  | 
            ||
| 21 | * @var MenuItemCollection  | 
            ||
| 22 | */  | 
            ||
| 23 | private $menuItemsCollection;  | 
            ||
| 24 | /**  | 
            ||
| 25 | * @var int  | 
            ||
| 26 | */  | 
            ||
| 27 | private $nesting;  | 
            ||
| 28 | |||
| 29 | /**  | 
            ||
| 30 | * Set root ID.  | 
            ||
| 31 | *  | 
            ||
| 32 | * @param int $id ID of element to start create tree. Set 0 to create full tree  | 
            ||
| 33 | */  | 
            ||
| 34 | public function setRootID($id)  | 
            ||
| 38 | |||
| 39 | /**  | 
            ||
| 40 | * @return int  | 
            ||
| 41 | */  | 
            ||
| 42 | public function getRootID()  | 
            ||
| 46 | |||
| 47 | /**  | 
            ||
| 48 | * Menu type defined in menu_types table.  | 
            ||
| 49 | *  | 
            ||
| 50 | * @param string $type  | 
            ||
| 51 | */  | 
            ||
| 52 | public function setType($type)  | 
            ||
| 56 | |||
| 57 | /**  | 
            ||
| 58 | * @return string  | 
            ||
| 59 | */  | 
            ||
| 60 | public function getType()  | 
            ||
| 64 | |||
| 65 | /**  | 
            ||
| 66 | * Set items.  | 
            ||
| 67 | *  | 
            ||
| 68 | * @param MenuItemCollection $items  | 
            ||
| 69 | */  | 
            ||
| 70 | public function setItems(MenuItemCollection $items)  | 
            ||
| 74 | |||
| 75 | /**  | 
            ||
| 76 | * @return array  | 
            ||
| 77 | */  | 
            ||
| 78 | public function getItems()  | 
            ||
| 82 | |||
| 83 | /**  | 
            ||
| 84 | * Set generated menu code nesting.  | 
            ||
| 85 | *  | 
            ||
| 86 | * @param int $nesting  | 
            ||
| 87 | */  | 
            ||
| 88 | public function setNesting($nesting)  | 
            ||
| 92 | |||
| 93 | /**  | 
            ||
| 94 | * @return mixed  | 
            ||
| 95 | */  | 
            ||
| 96 | public function getNesting()  | 
            ||
| 100 | |||
| 101 | /**  | 
            ||
| 102 | * Put value is not empty.  | 
            ||
| 103 | *  | 
            ||
| 104 | * @param string $attribute  | 
            ||
| 105 | * @param string|array $value  | 
            ||
| 106 | *  | 
            ||
| 107 | * @return string  | 
            ||
| 108 | */  | 
            ||
| 109 | View Code Duplication | private function isAttribute($attribute, $value)  | 
            |
| 120 | |||
| 121 | |||
| 122 | /**  | 
            ||
| 123 | * Menu creator.  | 
            ||
| 124 | *  | 
            ||
| 125 | * @link http://pastebin.com/GAFvSew4  | 
            ||
| 126 | *  | 
            ||
| 127 | * @author J. Bruni - original author  | 
            ||
| 128 | *  | 
            ||
| 129 | * @return string|bool  | 
            ||
| 130 | */  | 
            ||
| 131 | public function create()  | 
            ||
| 257 | }  | 
            ||
| 258 | 
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: