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  | 
            ||
| 11 | class Form  | 
            ||
| 12 | { | 
            ||
| 13 | /**  | 
            ||
| 14 | * The form fields.  | 
            ||
| 15 | *  | 
            ||
| 16 | * @var Collection|Field[]  | 
            ||
| 17 | */  | 
            ||
| 18 | protected $fields;  | 
            ||
| 19 | |||
| 20 | /**  | 
            ||
| 21 | * The form model.  | 
            ||
| 22 | *  | 
            ||
| 23 | * @var Model  | 
            ||
| 24 | */  | 
            ||
| 25 | protected $model;  | 
            ||
| 26 | |||
| 27 | /**  | 
            ||
| 28 | * The form theme.  | 
            ||
| 29 | *  | 
            ||
| 30 | * @var string  | 
            ||
| 31 | */  | 
            ||
| 32 | protected $theme;  | 
            ||
| 33 | |||
| 34 | /**  | 
            ||
| 35 | * The helper of former.  | 
            ||
| 36 | *  | 
            ||
| 37 | * @var FormerHelper  | 
            ||
| 38 | */  | 
            ||
| 39 | protected $helper;  | 
            ||
| 40 | |||
| 41 | /**  | 
            ||
| 42 | * The action of form.  | 
            ||
| 43 | *  | 
            ||
| 44 | * @var string  | 
            ||
| 45 | */  | 
            ||
| 46 | protected $action;  | 
            ||
| 47 | |||
| 48 | /**  | 
            ||
| 49 | * The method of form.  | 
            ||
| 50 | *  | 
            ||
| 51 | * @var string  | 
            ||
| 52 | */  | 
            ||
| 53 | protected $method = 'POST';  | 
            ||
| 54 | |||
| 55 | /**  | 
            ||
| 56 | * Form constructor.  | 
            ||
| 57 | *  | 
            ||
| 58 | * @param Model|null $model  | 
            ||
| 59 | * @param FormerHelper $formerHelper  | 
            ||
| 60 | */  | 
            ||
| 61 | public function __construct(Model $model = null, FormerHelper $formerHelper)  | 
            ||
| 68 | |||
| 69 | /**  | 
            ||
| 70 | * Get the model fields.  | 
            ||
| 71 | *  | 
            ||
| 72 | * @return Collection  | 
            ||
| 73 | */  | 
            ||
| 74 | public function getFields(): Collection  | 
            ||
| 78 | |||
| 79 | /**  | 
            ||
| 80 | * Set the model fields.  | 
            ||
| 81 | *  | 
            ||
| 82 | * @param string $input  | 
            ||
| 83 | * @param string $name  | 
            ||
| 84 | * @param array $rules  | 
            ||
| 85 | */  | 
            ||
| 86 | public function addField($input, $name, array $rules)  | 
            ||
| 96 | |||
| 97 | /**  | 
            ||
| 98 | * Get the form model.  | 
            ||
| 99 | *  | 
            ||
| 100 | * @return Model  | 
            ||
| 101 | */  | 
            ||
| 102 | public function getModel(): Model  | 
            ||
| 106 | |||
| 107 | public function getHelper(): FormerHelper  | 
            ||
| 111 | |||
| 112 | /**  | 
            ||
| 113 | * @return string  | 
            ||
| 114 | */  | 
            ||
| 115 | public function getAction(): string  | 
            ||
| 119 | |||
| 120 | /**  | 
            ||
| 121 | * @return string  | 
            ||
| 122 | */  | 
            ||
| 123 | public function getMethod(): string  | 
            ||
| 127 | |||
| 128 | /**  | 
            ||
| 129 | * Set the form model.  | 
            ||
| 130 | *  | 
            ||
| 131 | * @param Model $model  | 
            ||
| 132 | */  | 
            ||
| 133 | public function setModel(Model $model)  | 
            ||
| 137 | |||
| 138 | /**  | 
            ||
| 139 | * Set the form theme.  | 
            ||
| 140 | *  | 
            ||
| 141 | * @param string|null $theme  | 
            ||
| 142 | */  | 
            ||
| 143 | public function setTheme($theme)  | 
            ||
| 147 | |||
| 148 | public function setAction($action)  | 
            ||
| 152 | |||
| 153 | public function setActionFromRoute($route)  | 
            ||
| 157 | |||
| 158 | public function setMethod($method)  | 
            ||
| 162 | |||
| 163 | public function start()  | 
            ||
| 171 | |||
| 172 | public function end()  | 
            ||
| 180 | |||
| 181 | View Code Duplication | public function field(string $name)  | 
            |
| 192 | |||
| 193 | View Code Duplication | public function fields()  | 
            |
| 205 | }  | 
            ||
| 206 | 
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: