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 | trait Common |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var string |
||
| 12 | */ |
||
| 13 | protected $id; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $name; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var Collection |
||
| 22 | */ |
||
| 23 | protected $classes; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var Collection |
||
| 27 | */ |
||
| 28 | protected $attributes; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var Collection |
||
| 32 | */ |
||
| 33 | protected $styles; |
||
| 34 | |||
| 35 | public function id($value): self |
||
| 41 | |||
| 42 | public function name($value): self |
||
| 48 | |||
| 49 | View Code Duplication | public function class(...$classes): self |
|
| 59 | |||
| 60 | View Code Duplication | public function classes(iterable $classes): self |
|
| 70 | |||
| 71 | public function style(string $key, string $value = null): self |
||
| 88 | |||
| 89 | public function styles(iterable $styles): self |
||
| 97 | |||
| 98 | public function attr(string $key, string $value = null): self |
||
| 104 | |||
| 105 | public function attrs(iterable $attributes): self |
||
| 119 | } |
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.