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 |
||
7 | class SlugOptions |
||
8 | { |
||
9 | /** |
||
10 | * The field used to generate the slug from. |
||
11 | * |
||
12 | * @var string|array|callable |
||
13 | */ |
||
14 | private $fromField; |
||
15 | |||
16 | /** |
||
17 | * The field where to store the generated slug. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | private $toField; |
||
22 | |||
23 | /** |
||
24 | * Flag whether slugs should be unique or not. |
||
25 | * |
||
26 | * @var bool |
||
27 | */ |
||
28 | private $uniqueSlugs = true; |
||
29 | |||
30 | /** |
||
31 | * The separator used between words in the slug. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | private $slugSeparator = '-'; |
||
36 | |||
37 | /** |
||
38 | * The language used to transform the UTF-8 slug to ASCII. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | private $slugLanguage = 'en'; |
||
43 | |||
44 | /** |
||
45 | * Flag whether to generate slug on model create event or not. |
||
46 | * |
||
47 | * @var bool |
||
48 | */ |
||
49 | private $generateSlugOnCreate = true; |
||
50 | |||
51 | /** |
||
52 | * Flag whether to generate slug on model update event or not. |
||
53 | * |
||
54 | * @var bool |
||
55 | */ |
||
56 | private $generateSlugOnUpdate = true; |
||
57 | |||
58 | /** |
||
59 | * Get the value of a property of this class. |
||
60 | * |
||
61 | * @param $name |
||
62 | * @return mixed |
||
63 | * @throws Exception |
||
64 | */ |
||
65 | View Code Duplication | public function __get($name) |
|
75 | |||
76 | /** |
||
77 | * Get a fresh instance of this class. |
||
78 | * |
||
79 | * @return SlugOptions |
||
80 | */ |
||
81 | public static function instance(): self |
||
85 | |||
86 | /** |
||
87 | * Set the $fromField to work with in the Neurony\Url\Traits\HasSlug trait. |
||
88 | * |
||
89 | * @param string|array|callable $field |
||
90 | * @return SlugOptions |
||
91 | */ |
||
92 | public function generateSlugFrom($field): self |
||
102 | |||
103 | /** |
||
104 | * Set the $toField to work with in the Neurony\Url\Traits\HasSlug trait. |
||
105 | * |
||
106 | * @param string $field |
||
107 | * @return SlugOptions |
||
108 | */ |
||
109 | public function saveSlugTo(string $field): self |
||
115 | |||
116 | /** |
||
117 | * Set the $uniqueSlugs to work with in the Neurony\Url\Traits\HasSlug trait. |
||
118 | * |
||
119 | * @return SlugOptions |
||
120 | */ |
||
121 | public function allowDuplicateSlugs(): self |
||
127 | |||
128 | /** |
||
129 | * Set the $slugSeparator to work with in the Neurony\Url\Traits\HasSlug trait. |
||
130 | * |
||
131 | * @param string $separator |
||
132 | * @return SlugOptions |
||
133 | */ |
||
134 | public function usingSeparator(string $separator): self |
||
140 | |||
141 | /** |
||
142 | * Set the $slugLanguage to work with in the Neurony\Url\Traits\HasSlug trait. |
||
143 | * |
||
144 | * @param string $separator |
||
145 | * @return SlugOptions |
||
146 | */ |
||
147 | public function usingLanguage(string $separator): self |
||
153 | |||
154 | /** |
||
155 | * Set the $generateSlugOnCreate to work with in the Neurony\Url\Traits\HasSlug trait. |
||
156 | * |
||
157 | * @return SlugOptions |
||
158 | */ |
||
159 | public function doNotGenerateSlugOnCreate(): self |
||
165 | |||
166 | /** |
||
167 | * Set the $generateSlugOnUpdate to work with in the Neurony\Url\Traits\HasSlug trait. |
||
168 | * |
||
169 | * @return SlugOptions |
||
170 | */ |
||
171 | public function doNotGenerateSlugOnUpdate(): self |
||
177 | } |
||
178 |
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.