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 | class Category extends \Xoco70\LaravelTournaments\Models\Category |
||
9 | { |
||
10 | const AGE_CUSTOM = 5; |
||
11 | |||
12 | /** |
||
13 | * Get All Presets for Categories |
||
14 | * @param null $ruleId |
||
15 | * @return array |
||
16 | */ |
||
17 | 3 | public function getCategorieslabelByRule($ruleId = null) |
|
18 | { |
||
19 | 3 | $result = []; |
|
20 | |||
21 | 3 | if ($ruleId == null) { |
|
22 | 3 | $rules = ['ikf', 'ekf', 'lakc']; |
|
23 | 3 | foreach ($rules as $rule) { |
|
24 | 3 | $result[] = static::whereIn('id', array_keys(config('options.' . $rule . '_settings'))) |
|
25 | 3 | ->select('name')->get() |
|
26 | 3 | ->map->name->toArray(); |
|
27 | } |
||
28 | } |
||
29 | 3 | return $result; |
|
30 | } |
||
31 | |||
32 | public function scopeIsTeam($query) |
||
33 | { |
||
34 | return $query->where('isTeam', 1); |
||
35 | } |
||
36 | |||
37 | 2 | public function buildName() |
|
38 | { |
||
39 | |||
40 | 2 | if (Auth::check()) { |
|
41 | 2 | App::setLocale(Auth::user()->locale); |
|
|
|||
42 | } |
||
43 | $genders = [ |
||
44 | 2 | 'M' => trans('categories.male'), |
|
45 | 2 | 'F' => trans('categories.female'), |
|
46 | 2 | 'X' => trans('categories.mixt') |
|
47 | ]; |
||
48 | |||
49 | 2 | $teamText = $this->isTeam() ? trans_choice('core.team', 1) : trans('categories.single'); |
|
50 | 2 | $ageCategoryText = $this->getAgeString(); |
|
51 | 2 | $gradeText = $this->getGradeString(); |
|
52 | |||
53 | 2 | return $teamText . ' ' . $genders[$this->gender] . ' ' . $ageCategoryText . ' ' . $gradeText; |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * Build Age String |
||
58 | * @return string |
||
59 | */ |
||
60 | 2 | public function getAgeString() |
|
61 | { |
||
62 | $ageCategories = [ |
||
63 | 2 | 0 => trans('categories.no_age_restriction'), |
|
64 | 2 | 1 => trans('categories.children'), |
|
65 | 2 | 2 => trans('categories.students'), |
|
66 | 2 | 3 => trans('categories.adults'), |
|
67 | 2 | 4 => trans('categories.masters'), |
|
68 | 2 | 5 => trans('categories.custom') |
|
69 | ]; |
||
70 | |||
71 | 2 | if ($this->ageCategory == self::AGE_CUSTOM) { |
|
72 | 2 | $ageCategoryText = ' - ' . trans('categories.age') . ' : '; |
|
73 | 2 | if ($this->ageMin != 0 && $this->ageMax != 0) { |
|
74 | 2 | return $this->hasAgeMinAndMax($ageCategoryText); |
|
75 | } |
||
76 | View Code Duplication | if ($this->ageMin == 0 && $this->ageMax != 0) { |
|
77 | return $ageCategoryText .= ' < ' . $this->ageMax . ' ' . trans('categories.years'); |
||
78 | } |
||
79 | View Code Duplication | if ($this->ageMin != 0 && $this->ageMax == 0) { |
|
80 | return $ageCategoryText .= ' > ' . $this->ageMin . ' ' . trans('categories.years'); |
||
81 | } |
||
82 | return ''; |
||
83 | } |
||
84 | return $ageCategories[$this->ageCategory]; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param $ageCategoryText |
||
89 | * @return string |
||
90 | */ |
||
91 | 2 | protected function hasAgeMinAndMax($ageCategoryText): string |
|
92 | { |
||
93 | 2 | if ($this->ageMin == $this->ageMax) { |
|
94 | $ageCategoryText .= $this->ageMax . ' ' . trans('categories.years'); |
||
95 | } else { |
||
96 | 2 | $ageCategoryText .= $this->ageMin . ' - ' . $this->ageMax . ' ' . trans('categories.years'); |
|
97 | } |
||
98 | 2 | return $ageCategoryText; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Build Grade String |
||
103 | * @return string |
||
104 | */ |
||
105 | 2 | public function getGradeString() |
|
106 | { |
||
107 | |||
108 | 2 | $grades = Grade::getAll(); |
|
109 | 2 | if ($this->gradeCategory == 3) { // Custom |
|
110 | $gradeText = ' - ' . trans('core.grade') . ' : '; |
||
111 | if ($this->gradeMin != 0 && $this->gradeMax != 0) { |
||
112 | return $this->hasGradeMinAndMax($grades, $gradeText); |
||
113 | } |
||
114 | View Code Duplication | if ($this->gradeMin == 0 && $this->gradeMax != 0) { |
|
115 | return $gradeText . ' < ' . $grades[$this->gradeMax - 1]->name; |
||
116 | } |
||
117 | View Code Duplication | if ($this->gradeMin != 0 && $this->gradeMax == 0) { |
|
118 | return $gradeText . ' > ' . $grades[$this->gradeMin - 1]->name; |
||
119 | } |
||
120 | } |
||
121 | 2 | return ''; |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param $grades |
||
126 | * @param $gradeText |
||
127 | * @return string |
||
128 | */ |
||
129 | protected function hasGradeMinAndMax($grades, $gradeText): string |
||
138 | } |
||
139 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: