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 |
||
| 17 | View Code Duplication | class Item |
|
|
|
|||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Name. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $name = ''; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Data for refill item. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $data = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Source. |
||
| 35 | * |
||
| 36 | * Can set the source to source item to avoid the next search for this item |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $source = ''; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Image. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $image = ''; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Description. |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $description = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Construct. |
||
| 58 | * |
||
| 59 | * @param string $name |
||
| 60 | * @param array $data |
||
| 61 | * @param string $source |
||
| 62 | * @param string|null $image |
||
| 63 | * @param string|null $description |
||
| 64 | */ |
||
| 65 | 1 | public function __construct($name, array $data, $source, $image = '', $description = '') |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Get name. |
||
| 76 | * |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | 1 | public function getName() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Get data for refill item. |
||
| 86 | * |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | 1 | public function getData() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Get source. |
||
| 96 | * |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | 1 | public function getSource() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Get image. |
||
| 106 | * |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | 1 | public function getImage() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Get description. |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | 1 | public function getDescription() |
|
| 123 | } |
||
| 124 |
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.