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 |
||
8 | View Code Duplication | class CountryStrict extends Eloquent |
|
|
|||
9 | { |
||
10 | use Translatable; |
||
11 | |||
12 | /** |
||
13 | * Array with the fields translated in the Translation table. |
||
14 | * |
||
15 | * @var array |
||
16 | */ |
||
17 | public $translatedAttributes = ['name']; |
||
18 | |||
19 | /** |
||
20 | * Here we set a custom model for translation. |
||
21 | * The convention would be Dimsav\Translatable\Test\Model\CountryStrictTranslation. |
||
22 | * |
||
23 | * @var string Class containing the translation |
||
24 | */ |
||
25 | public $translationModel = 'Dimsav\Translatable\Test\Model\StrictTranslation'; |
||
26 | |||
27 | /** |
||
28 | * @var string Foreign key for the translation relationship |
||
29 | */ |
||
30 | public $translationForeignKey = 'country_id'; |
||
31 | |||
32 | /** |
||
33 | * Column containing the locale in the translation table. |
||
34 | * Defaults to 'locale'. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | public $localeKey; |
||
39 | |||
40 | public $table = 'countries'; |
||
41 | |||
42 | /** |
||
43 | * Add your translated attributes here if you want |
||
44 | * fill them with mass assignment. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | public $fillable = ['code']; |
||
49 | |||
50 | protected $softDelete = true; |
||
51 | } |
||
52 |
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.