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 |
||
| 25 | class ToolbarHelper |
||
| 26 | { |
||
| 27 | |||
| 28 | const ACTION_SAVE = 'save'; |
||
| 29 | const ACTION_DELETE = 'delete'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Toolbar instance name. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected static $_toolbar = Toolbar::DEFAULT_NAME; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Setup toolbar instance name. |
||
| 40 | * |
||
| 41 | * @param $name |
||
| 42 | */ |
||
| 43 | public static function setToolbar($name) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Create link output. |
||
| 50 | * |
||
| 51 | * @param string $title |
||
| 52 | * @param string $url |
||
| 53 | * @param array $options |
||
| 54 | */ |
||
| 55 | View Code Duplication | public static function link($title, $url, array $options = []) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Create add link. |
||
| 68 | * |
||
| 69 | * @param string|null $title |
||
| 70 | * @param array|string $url |
||
| 71 | * @param string $icon |
||
| 72 | * @param array $options |
||
| 73 | */ |
||
| 74 | View Code Duplication | public static function add($title = null, $url = ['action' => 'add'], $icon = 'plus', array $options = []) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Delete for process form. |
||
| 87 | * |
||
| 88 | * @param string|null $title |
||
| 89 | */ |
||
| 90 | public static function delete($title = null) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Save form button. |
||
| 99 | * |
||
| 100 | * @param null $title |
||
| 101 | */ |
||
| 102 | public static function save($title = null) |
||
| 113 | } |
||
| 114 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.