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 |
||
13 | abstract class AliasModel extends UrlModel implements NamedModel |
||
14 | { |
||
15 | /** |
||
16 | * The name of the object |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $name; |
||
20 | |||
21 | /** |
||
22 | * A unique URL-friendly identifier for the object |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $alias; |
||
26 | |||
27 | /** |
||
28 | * Get the name of the object |
||
29 | * @return string |
||
30 | */ |
||
31 | 39 | public function getName() |
|
35 | |||
36 | /** |
||
37 | * Get the name of the team, safe for use in your HTML |
||
38 | * |
||
39 | * @return string The name of the team |
||
40 | */ |
||
41 | 1 | public function getEscapedName() |
|
49 | |||
50 | /** |
||
51 | * Change the object's name |
||
52 | * |
||
53 | * @return self |
||
54 | */ |
||
55 | public function setName($name) |
||
62 | |||
63 | /** |
||
64 | * Get an object's alias |
||
65 | * @return string|int The alias (or ID if the alias doesn't exist) |
||
66 | */ |
||
67 | 5 | public function getAlias() |
|
75 | |||
76 | /** |
||
77 | * Set a model's alias |
||
78 | * @param string $alias The new alias |
||
79 | * @return void |
||
80 | */ |
||
81 | public function setAlias($alias) |
||
85 | |||
86 | /** |
||
87 | * Reset a model's alias based on its name |
||
88 | * @return self |
||
89 | */ |
||
90 | 38 | public function resetAlias() |
|
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | * |
||
100 | 1 | * @todo Redirect models with wrong alias when bzion.site.url_type === 'permalink' |
|
101 | */ |
||
102 | 1 | public function getURL($action = 'show', $absolute = false, $params = array(), $vanity = false) |
|
133 | 3 | ||
134 | /** |
||
135 | 2 | * Gets an entity from the supplied alias |
|
136 | * @param string $alias The object's alias |
||
137 | * @return AliasModel |
||
138 | 2 | */ |
|
139 | public static function fetchFromAlias($alias) |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | * @return AliasModel |
||
147 | */ |
||
148 | public static function fetchFromSlug($slug) |
||
158 | |||
159 | /** |
||
160 | 39 | * Generate a URL-friendly unique alias for an object name |
|
161 | * |
||
162 | 39 | * @param string $name The original object name |
|
163 | * @param int|null $id The ID of the object, if it's being edited and not created |
||
164 | * @return string|null The generated alias, or Null if we couldn't make one |
||
165 | 1 | */ |
|
166 | public static function generateAlias($name, $id = null) |
||
194 | |||
195 | /** |
||
196 | * Make sure that the generated alias provided is unique |
||
197 | * |
||
198 | 39 | * @param string $alias The alias |
|
199 | 39 | * @param int $id The ID of the object, if it's being edited and not created |
|
200 | 39 | * @return string An alias that is guaranteed to be unique |
|
201 | 39 | */ |
|
202 | 39 | private static function getUniqueAlias($alias, $id = 0) |
|
227 | |||
228 | /** |
||
229 | * Get a list of aliases that should not be given to objects |
||
230 | * |
||
231 | * For example, you want to prevent teams from getting the "new" alias. |
||
232 | * Otherwise, the team's link would be http://example.com/bzion/teams/new, |
||
233 | * and the user would go to the team creation page instead of the team's page. |
||
234 | * Disallowed aliases will have a number appended, so the URL would be |
||
235 | * http://example.com/bzion/teams/new2 |
||
236 | * |
||
237 | * @return string[] |
||
238 | */ |
||
239 | protected static function getDisallowedAliases() |
||
243 | } |
||
244 |
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.