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 UrlOptions |
||
8 | { |
||
9 | /** |
||
10 | * The controller where the laravel router should dispatch the request. |
||
11 | * This is used when a URI is accessed by a user. |
||
12 | * The format of this property should be Full\Namespace\Of\Controller. |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | private $routeController; |
||
17 | |||
18 | /** |
||
19 | * The controller where the laravel router should dispatch the request. |
||
20 | * This is used when a URI is accessed by a user. |
||
21 | * The format of this property should be simply the name of the method residing inside the $routeController. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | private $routeAction; |
||
26 | |||
27 | /** |
||
28 | * The field used to generate the url slug from. |
||
29 | * |
||
30 | * @var string|array|callable |
||
31 | */ |
||
32 | private $fromField; |
||
33 | |||
34 | /** |
||
35 | * The field where to store the generated url slug. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $toField; |
||
40 | |||
41 | /** |
||
42 | * The prefix that should be prepended to the generated url slug. |
||
43 | * |
||
44 | * @var string|array|callable |
||
45 | */ |
||
46 | private $urlPrefix; |
||
47 | |||
48 | /** |
||
49 | * The suffix that should be appended to the generated url slug. |
||
50 | * |
||
51 | * @var string|array|callable |
||
52 | */ |
||
53 | private $urlSuffix; |
||
54 | |||
55 | /** |
||
56 | * The symbol that will be used to glue url segments together. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | private $urlGlue = '/'; |
||
61 | |||
62 | /** |
||
63 | * Flag whether to update children urls on parent url save. |
||
64 | * |
||
65 | * @var bool |
||
66 | */ |
||
67 | private $cascadeUpdate = true; |
||
68 | |||
69 | /** |
||
70 | * Get the value of a property of this class. |
||
71 | * |
||
72 | * @param $name |
||
73 | * @return mixed |
||
74 | * @throws Exception |
||
75 | */ |
||
76 | View Code Duplication | public function __get($name) |
|
86 | |||
87 | /** |
||
88 | * Get a fresh instance of this class. |
||
89 | * |
||
90 | * @return UrlOptions |
||
91 | */ |
||
92 | public static function instance(): self |
||
96 | |||
97 | /** |
||
98 | * Set the $urlRoute to work with in the Neurony\Url\Traits\HasUrl trait. |
||
99 | * |
||
100 | * @param string $controller |
||
101 | * @param string $action |
||
102 | * @return UrlOptions |
||
103 | */ |
||
104 | public function routeUrlTo(string $controller, string $action): self |
||
111 | |||
112 | /** |
||
113 | * Set the $fromField to work with in the Neurony\Url\Traits\HasUrl trait. |
||
114 | * |
||
115 | * @param string|array|callable $field |
||
116 | * @return UrlOptions |
||
117 | */ |
||
118 | public function generateUrlSlugFrom($field): self |
||
124 | |||
125 | /** |
||
126 | * Set the $toField to work with in the Neurony\Url\Traits\HasUrl trait. |
||
127 | * |
||
128 | * @param string $field |
||
129 | * @return UrlOptions |
||
130 | */ |
||
131 | public function saveUrlSlugTo(string $field): self |
||
137 | |||
138 | /** |
||
139 | * Set the $urlPrefix to work with in the Neurony\Url\Traits\HasUrl trait. |
||
140 | * |
||
141 | * @param string|array|callable $prefix |
||
142 | * @return UrlOptions |
||
143 | */ |
||
144 | public function prefixUrlWith($prefix): self |
||
150 | |||
151 | /** |
||
152 | * Set the $urlSuffix to work with in the Neurony\Url\Traits\HasUrl trait. |
||
153 | * |
||
154 | * @param string|array|callable $suffix |
||
155 | * @return UrlOptions |
||
156 | */ |
||
157 | public function suffixUrlWith($suffix): self |
||
163 | |||
164 | /** |
||
165 | * Set the $urlGlue to work with in the Neurony\Url\Traits\HasUrl trait. |
||
166 | * |
||
167 | * @param string $glue |
||
168 | * @return UrlOptions |
||
169 | */ |
||
170 | public function glueUrlWith(string $glue): self |
||
176 | |||
177 | /** |
||
178 | * Set the $cascadeUpdate to work with in the Neurony\Url\Traits\HasUrl trait. |
||
179 | * |
||
180 | * @return UrlOptions |
||
181 | */ |
||
182 | public function doNotUpdateCascading(): self |
||
188 | } |
||
189 |
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.