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:
Complex classes like SluggableTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SluggableTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Cviebrock\EloquentSluggable; |
||
| 12 | trait SluggableTrait |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Determines whether the model needs slugging. |
||
| 16 | * |
||
| 17 | * @return bool |
||
| 18 | */ |
||
| 19 | protected function needsSlugging() |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Get the source string for the slug. |
||
| 38 | * |
||
| 39 | * @return string |
||
| 40 | */ |
||
| 41 | protected function getSlugSource() |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get value for slug. |
||
| 57 | * |
||
| 58 | * @param string $key |
||
| 59 | * @return string|null |
||
| 60 | */ |
||
| 61 | protected function generateSource($key) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Generate a slug from the given source string. |
||
| 81 | * |
||
| 82 | * @param string $source |
||
| 83 | * @return string |
||
| 84 | * @throws \UnexpectedValueException |
||
| 85 | */ |
||
| 86 | protected function generateSlug($source) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Return a class that has a `slugify()` method, used to convert |
||
| 113 | * strings into slugs. |
||
| 114 | * |
||
| 115 | * @return Slugify |
||
| 116 | */ |
||
| 117 | protected function getSlugEngine() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Checks that the given slug is not a reserved word. |
||
| 124 | * |
||
| 125 | * @param string $slug |
||
| 126 | * @return string |
||
| 127 | * @throws \UnexpectedValueException |
||
| 128 | */ |
||
| 129 | protected function validateSlug($slug) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Checks if the slug should be unique, and makes it so if needed. |
||
| 156 | * |
||
| 157 | * @param string $slug |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | protected function makeSlugUnique($slug) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Generate a unique suffix for the given slug (and list of existing, "similar" slugs. |
||
| 193 | * |
||
| 194 | * @param string $slug |
||
| 195 | * @param array $list |
||
| 196 | * |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | protected function generateSuffix($slug, $list) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get all existing slugs that are similar to the given slug. |
||
| 225 | * |
||
| 226 | * @param string $slug |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | protected function getExistingSlugs($slug) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Does this model use softDeleting? |
||
| 262 | * |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | protected function usesSoftDeleting() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Set the slug manually. |
||
| 272 | * |
||
| 273 | * @param string $slug |
||
| 274 | */ |
||
| 275 | protected function setSlug($slug) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get the current slug. |
||
| 284 | * |
||
| 285 | * @return mixed |
||
| 286 | */ |
||
| 287 | public function getSlug() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Manually slug the current model. |
||
| 297 | * |
||
| 298 | * @param bool $force |
||
| 299 | * @return $this |
||
| 300 | */ |
||
| 301 | public function sluggify($force = false) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Force slugging of current model. |
||
| 324 | * |
||
| 325 | * @return SluggableTrait |
||
| 326 | */ |
||
| 327 | public function resluggify() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Generate a unique slug for a given string. |
||
| 334 | * |
||
| 335 | * @param string $fromString |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | public static function createSlug($fromString) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Query scope for finding a model by its slug. |
||
| 349 | * |
||
| 350 | * @param $scope |
||
| 351 | * @param $slug |
||
| 352 | * @return mixed |
||
| 353 | */ |
||
| 354 | public function scopeWhereSlug($scope, $slug) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Find a model by slug. |
||
| 363 | * |
||
| 364 | * @param $slug |
||
| 365 | * @param array $columns |
||
| 366 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 367 | */ |
||
| 368 | public static function findBySlug($slug, array $columns = ['*']) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Find a model by slug or fail. |
||
| 375 | * |
||
| 376 | * @param $slug |
||
| 377 | * @param array $columns |
||
| 378 | * @return \Illuminate\Database\Eloquent\Model |
||
| 379 | */ |
||
| 380 | public static function findBySlugOrFail($slug, array $columns = ['*']) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Get the default configuration and merge in any model-specific overrides. |
||
| 387 | * |
||
| 388 | * @return array |
||
| 389 | */ |
||
| 390 | protected function getSluggableConfig() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Simple find by Id if it's numeric or slug if not. Fail if not found. |
||
| 402 | * |
||
| 403 | * @param $slug |
||
| 404 | * @param array $columns |
||
| 405 | * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection |
||
| 406 | */ |
||
| 407 | View Code Duplication | public static function findBySlugOrIdOrFail($slug, array $columns = ['*']) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Simple find by Id if it's numeric or slug if not. |
||
| 418 | * |
||
| 419 | * @param $slug |
||
| 420 | * @param array $columns |
||
| 421 | * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection|null |
||
| 422 | */ |
||
| 423 | View Code Duplication | public static function findBySlugOrId($slug, array $columns = ['*']) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Find a model by slug or create new instance of model. |
||
| 434 | * |
||
| 435 | * @param $slug |
||
| 436 | * @param array $columns |
||
| 437 | * @return \Illuminate\Database\Eloquent\Model |
||
| 438 | */ |
||
| 439 | public static function findBySlugOrNew($slug, array $columns = ['*']) |
||
| 448 | } |
||
| 449 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: