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 |
||
15 | View Code Duplication | trait CategorizableTrait |
|
|
|||
16 | { |
||
17 | /** |
||
18 | * The type of category the object can belong to. |
||
19 | * |
||
20 | * @var string $categoryType |
||
21 | */ |
||
22 | private $categoryType; |
||
23 | |||
24 | /** |
||
25 | * The category the object belongs to. |
||
26 | * |
||
27 | * @var mixed|CategoryInterface $category |
||
28 | */ |
||
29 | protected $category; |
||
30 | |||
31 | /** |
||
32 | * Set the type of category the object can belong to. |
||
33 | * |
||
34 | * @param string $type The category type. |
||
35 | * @throws InvalidArgumentException If the type argument is not a string. |
||
36 | * @return CategorizableInterface Chainable |
||
37 | */ |
||
38 | public function setCategoryType($type) |
||
50 | |||
51 | /** |
||
52 | * Retrieve the type of category the object can belong to. |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | public function categoryType() |
||
60 | |||
61 | /** |
||
62 | * Set the category the object belongs to. |
||
63 | * |
||
64 | * @param mixed $category The object's category. |
||
65 | * @return CategorizableInterface Chainable |
||
66 | */ |
||
67 | public function setCategory($category) |
||
73 | |||
74 | /** |
||
75 | * Retrieve the category the object belongs to. |
||
76 | * |
||
77 | * @return mixed |
||
78 | */ |
||
79 | public function getCategory() |
||
83 | } |
||
84 |
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.